package com.energy.manage.service.service.windfield.impl;


import cn.hutool.core.bean.BeanUtil;
import com.energy.manage.common.base.BaseServiceImpl;
import com.energy.manage.common.po.windfield.WindFieldPO;
import com.energy.manage.common.po.windfield.WindFieldResourcePO;
import com.energy.manage.service.domain.dto.windfield.*;
import com.energy.manage.service.domain.vo.windfield.WindFieldResourceVo;
import com.energy.manage.service.mappers.windfield.WindFieldResourceMapper;
import com.energy.manage.service.service.windfield.WindFieldResourceService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class WindFieldResourceServiceImpl extends BaseServiceImpl<WindFieldResourcePO> implements WindFieldResourceService {

    @Autowired
    private WindFieldResourceMapper windFieldResourceMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveWindFieldResource(WindFieldResourceCreateDto windFieldResourceCreateDto) {

        WindFieldResourcePO windFieldResourcePO = new WindFieldResourcePO();
        BeanUtil.copyProperties(windFieldResourceCreateDto, windFieldResourcePO);
        if (windFieldResourcePO == null) {
            return false;
        }
        windFieldResourcePO.setCreateTime(new Date());
        windFieldResourcePO.setUpdateTime(new Date());
        return windFieldResourceMapper.insertUseGeneratedKeys(windFieldResourcePO) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delWindFieldResource(WindFieldResourceDelDto windFieldResourceDelDto) {
        if (windFieldResourceDelDto.getId() == null) {
            return false;
        }
        WindFieldResourcePO windFieldResourcePO = new WindFieldResourcePO();
        windFieldResourcePO.setId(windFieldResourceDelDto.getId());
        return windFieldResourceMapper.delete(windFieldResourcePO) > 0;
    }


    @Override
    public List<WindFieldResourceVo> getWindFieldResourceByFieldCode(String FieldCode) {
        if (StringUtils.isEmpty(FieldCode)) {
            return null;
        }
        Example queryExample = new Example(WindFieldResourcePO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("fieldCode", FieldCode);
        List<WindFieldResourcePO> list = windFieldResourceMapper.selectByExample(queryExample);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        WindFieldResourceVo windFieldResourceVo = null;
        List<WindFieldResourceVo> windFieldResourceVos = Lists.newArrayList();
        for (WindFieldResourcePO windFieldResourcePO : list) {
            windFieldResourceVo = new WindFieldResourceVo();
            BeanUtil.copyProperties(windFieldResourcePO,windFieldResourceVo);
            windFieldResourceVos.add(windFieldResourceVo);
        }
        return windFieldResourceVos;
    }


}
