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

import cn.hutool.core.bean.BeanUtil;
import com.energy.manage.common.base.BaseServiceImpl;
import com.energy.manage.common.base.Page;
import com.energy.manage.common.constant.Constants;
import com.energy.manage.common.constant.redis.ManagerRedisKeyConstant;
import com.energy.manage.common.enums.DeleteStatusEnum;
import com.energy.manage.common.enums.IdPrefixEnum;
import com.energy.manage.common.po.anemometertower.AnemometerTowerHeightPO;
import com.energy.manage.common.po.anemometertower.AnemometerTowerPO;
import com.energy.manage.common.po.anemometertower.AnemometerTowerRelationPO;
import com.energy.manage.common.po.company.WindCompanyPO;
import com.energy.manage.common.po.windenginemill.WindEngineMillPO;
import com.energy.manage.common.util.IdGeneratorUtil;
import com.energy.manage.service.domain.dto.anemometertower.*;
import com.energy.manage.service.domain.dto.company.WindCompanyDto;
import com.energy.manage.service.domain.vo.anemometertower.*;
import com.energy.manage.service.domain.vo.windenginemill.WindEngineMillPageVo;
import com.energy.manage.service.mappers.anemometertower.AnemometerTowerDataMapper;
import com.energy.manage.service.mappers.anemometertower.AnemometerTowerHeightMapper;
import com.energy.manage.service.mappers.anemometertower.AnemometerTowerMapper;
import com.energy.manage.service.mappers.anemometertower.AnemometerTowerRelationMapper;
import com.energy.manage.service.service.anemometertower.AnemometerTowerService;
import com.energy.manage.service.service.cache.CacheService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.context.annotation.Bean;
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 AnemometerTowerServiceImpl extends BaseServiceImpl<AnemometerTowerPO> implements AnemometerTowerService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private AnemometerTowerMapper anemometerTowerMapper;

    @Autowired
    private AnemometerTowerHeightMapper anemometerTowerHeightMapper;

    @Autowired
    private AnemometerTowerDataMapper anemometerTowerDataMapper;

    @Autowired
    private AnemometerTowerRelationMapper anemometerTowerRelationMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createAnemometerTower(AnemometerTowerCreateDto anemometerTowerCreateDto) {
        AnemometerTowerPO anemometerTowerPO = new AnemometerTowerPO();
        BeanUtil.copyProperties(anemometerTowerCreateDto, anemometerTowerPO);
        anemometerTowerPO.setDelState(DeleteStatusEnum.NODELETE.getCode());
        anemometerTowerPO.setState(Constants.GLOBAL_IN_USE);
        anemometerTowerPO.setCreateTime(new Date());
        anemometerTowerPO.setUpdateTime(new Date());
        anemometerTowerPO.setAnemometerCode(IdPrefixEnum.WIND_TOWER_NUMBER.getCode().concat(IdGeneratorUtil.zeroFillUtil(cacheService.incr(ManagerRedisKeyConstant.build(ManagerRedisKeyConstant.IDGENERATOR_CONSTANTS_KEY, IdPrefixEnum.WIND_TOWER_NUMBER.getCode())))));
        int count = anemometerTowerMapper.insertUseGeneratedKeys(anemometerTowerPO);

        if (!CollectionUtils.isEmpty(anemometerTowerCreateDto.getAnemometerTowerHeightDtoList())) {
            AnemometerTowerHeightPO anemometerTowerHeightPO = null;
            List<AnemometerTowerHeightPO> anemometerTowerHeightPOS = Lists.newArrayList();
            for (AnemometerTowerCreateDto.AnemometerTowerHeightListDto anemometerTowerHeight : anemometerTowerCreateDto.getAnemometerTowerHeightDtoList()) {
                anemometerTowerHeightPO = new AnemometerTowerHeightPO();
                BeanUtil.copyProperties(anemometerTowerHeight, anemometerTowerHeightPO);
                anemometerTowerHeightPO.setAnemometerCode(anemometerTowerPO.getAnemometerCode());
                anemometerTowerHeightPO.setCreateTime(new Date());
                anemometerTowerHeightPO.setUpdateTime(new Date());
                anemometerTowerHeightPOS.add(anemometerTowerHeightPO);
            }
            anemometerTowerHeightMapper.insertList(anemometerTowerHeightPOS);
        }

        return count > 0 ? true : false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delAnemometerTower(AnemometerTowerDelDto anemometerTowerDelDto) {

        AnemometerTowerPO po = new AnemometerTowerPO();
        po.setDelState(DeleteStatusEnum.DELETE.getCode());
        Example queryExample = new Example(AnemometerTowerPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("anemometerCode", anemometerTowerDelDto.getAnemometerCode());

        return anemometerTowerMapper.updateByExampleSelective(po, queryExample) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAnemometerTower(AnemometerTowerUpdateDto anemometerTowerUpdateDto) {

        AnemometerTowerPO po = new AnemometerTowerPO();
        BeanUtil.copyProperties(anemometerTowerUpdateDto, po);
        po.setUpdateTime(new Date());
        Example queryExample = new Example(AnemometerTowerPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("anemometerCode", anemometerTowerUpdateDto.getAnemometerCode());
        int count = anemometerTowerMapper.updateByExampleSelective(po, queryExample);

        if (!CollectionUtils.isEmpty(anemometerTowerUpdateDto.getAnemometerTowerHeightListDtos())) {
            Example queryHExample = new Example(AnemometerTowerHeightPO.class);
            Example.Criteria criteriaH = queryHExample.createCriteria();
            criteriaH.andEqualTo("anemometerCode", anemometerTowerUpdateDto.getAnemometerCode());
            anemometerTowerHeightMapper.deleteByExample(queryHExample);

            AnemometerTowerHeightPO anemometerTowerHeightPO = null;
            List<AnemometerTowerHeightPO> anemometerTowerHeightPOS = Lists.newArrayList();
            for (AnemometerTowerUpdateDto.AnemometerTowerHeightListDto anemometerTowerHeightListDto : anemometerTowerUpdateDto.getAnemometerTowerHeightListDtos()) {
                anemometerTowerHeightPO = new AnemometerTowerHeightPO();
                BeanUtil.copyProperties(anemometerTowerHeightListDto, anemometerTowerHeightPO);
                anemometerTowerHeightPO.setAnemometerCode(anemometerTowerUpdateDto.getAnemometerCode());
                anemometerTowerHeightPO.setCreateTime(new Date());
                anemometerTowerHeightPO.setUpdateTime(new Date());
                anemometerTowerHeightPOS.add(anemometerTowerHeightPO);
            }
            anemometerTowerHeightMapper.insertList(anemometerTowerHeightPOS);
        }

        return count > 0 ? true : false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAnemometerTowerState(AnemometerTowerStateDto anemometerTowerStateDto) {

        AnemometerTowerPO po = new AnemometerTowerPO();
        po.setState(anemometerTowerStateDto.getState());
        Example queryExample = new Example(AnemometerTowerPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("anemometerCode", anemometerTowerStateDto.getAnemometerCode());
        criteria.andEqualTo("delState", DeleteStatusEnum.NODELETE.getCode());
        return anemometerTowerMapper.updateByExampleSelective(po, queryExample) > 0;

    }

    @Override
    public Page<AnemometerTowerPageVo> getAnemometerTowerPage(AnemometerTowerPageDto anemometerTowerPageDto) {
        PageHelper.startPage(anemometerTowerPageDto.getPageNum(), anemometerTowerPageDto.getPageSize());
        List<AnemometerTowerPageVo> list = anemometerTowerMapper.selectAnemometerTowerPage(anemometerTowerPageDto);
        Long total = org.apache.commons.collections.CollectionUtils.isEmpty(list) ? 0L : new PageInfo<>(list).getTotal();
        return Page.build(anemometerTowerPageDto.getPageNum(), anemometerTowerPageDto.getPageSize(), total, list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean bathAnemometerTowerHeight(AnemometerTowerHeightDto anemometerTowerHeightDto) {
        if (CollectionUtils.isEmpty(anemometerTowerHeightDto.getList())) {
            return false;
        }
        AnemometerTowerHeightPO anemometerTowerHeightPO = null;
        List<AnemometerTowerHeightPO> anemometerTowerHeightPOS = Lists.newArrayList();
        for (AnemometerTowerHeightDto anemometerTowerHeight : anemometerTowerHeightDto.getList()) {
            anemometerTowerHeightPO = new AnemometerTowerHeightPO();
            BeanUtil.copyProperties(anemometerTowerHeight, anemometerTowerHeightPO);
            anemometerTowerHeightPOS.add(anemometerTowerHeightPO);
        }
        return anemometerTowerHeightMapper.insertList(anemometerTowerHeightPOS) > 0;
    }

    @Override
    public AnemometerTowerVo getAnemometerTowerVo(AnemometerTowerDto anemometerTowerDto) {
        AnemometerTowerPO anemometerTowerPO = new AnemometerTowerPO();
        anemometerTowerPO.setAnemometerCode(anemometerTowerDto.getAnemometerCode());
        anemometerTowerPO.setDelState(DeleteStatusEnum.NODELETE.getCode());
        AnemometerTowerPO po = anemometerTowerMapper.selectOne(anemometerTowerPO);
        if (null == po) {
            return null;
        }
        AnemometerTowerVo anemometerTowerVo = new AnemometerTowerVo();
        BeanUtil.copyProperties(po, anemometerTowerVo);

        AnemometerTowerHeightPO anemometerTowerHeightPO = new AnemometerTowerHeightPO();
        anemometerTowerHeightPO.setAnemometerCode(anemometerTowerDto.getAnemometerCode());
        List<AnemometerTowerHeightPO> list = anemometerTowerHeightMapper.select(anemometerTowerHeightPO);

        // 处理高度集合
        AnemometerTowerVo.AnemometerTowerHeightList anemometerTowerHeightList = null;
        List<AnemometerTowerVo.AnemometerTowerHeightList> anemometerTowerHeightLists = Lists.newArrayList();
        for (AnemometerTowerHeightPO anemometerTowerHeightPO1 : list) {
            anemometerTowerHeightList = new AnemometerTowerVo.AnemometerTowerHeightList();
            BeanUtil.copyProperties(anemometerTowerHeightPO1, anemometerTowerHeightList);
            anemometerTowerHeightLists.add(anemometerTowerHeightList);
        }
        anemometerTowerVo.setList(anemometerTowerHeightLists);

        return anemometerTowerVo;
    }

    @Override
    public AnemometerTowerHeightVo getAnemometerTowerHeight(AnemometerTowerDto anemometerTowerDto) {

        Example queryExample = new Example(AnemometerTowerHeightPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("anemometerCode", anemometerTowerDto.getAnemometerCode());
        List<AnemometerTowerHeightPO> list = anemometerTowerHeightMapper.selectByExample(queryExample);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        AnemometerTowerHeightVo.AnemometerTowerHeightList dto = null;
        List<AnemometerTowerHeightVo.AnemometerTowerHeightList> towerHeightLists = Lists.newArrayList();

        for (AnemometerTowerHeightPO anemometerTowerHeightPO : list) {
            dto = new AnemometerTowerHeightVo.AnemometerTowerHeightList();
            BeanUtil.copyProperties(anemometerTowerHeightPO, dto);
            towerHeightLists.add(dto);
        }

        AnemometerTowerHeightVo anemometerTowerHeightVo = new AnemometerTowerHeightVo();
        anemometerTowerHeightVo.setList(towerHeightLists);
        return anemometerTowerHeightVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean bathAnemometerTowerRelation(List<AnemometerTowerRelationDto> anemometerTowerRelationDtos) {
        if (CollectionUtils.isEmpty(anemometerTowerRelationDtos)) {
            return false;
        }
        AnemometerTowerRelationPO po = null;
        List<AnemometerTowerRelationPO> list = Lists.newLinkedList();
        for (AnemometerTowerRelationDto dto : anemometerTowerRelationDtos) {
            po = new AnemometerTowerRelationPO();
            BeanUtil.copyProperties(dto, po);
            po.setCreateTime(new Date());
            po.setUpdateTime(new Date());
            list.add(po);
        }
        return anemometerTowerRelationMapper.insertList(list) > 0;
    }

    @Override
    public List<AnemometerTowerByFieldVo> getAnemometerTowerByField(String fieldCode) {
        if (StringUtils.isEmpty(fieldCode)) {
            return null;
        }
       return anemometerTowerMapper.selectAnemometerTowerByfieldCode(fieldCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deltAnemometerTowerRelationByField(String fieldCode) {

        Example queryExample = new Example(AnemometerTowerRelationPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("fieldCode", fieldCode);
        return anemometerTowerRelationMapper.deleteByExample(queryExample) > 0;
    }

    @Override
    public AnemometerTowerPO getAnemometerTowerPOByanemometerTowerName(String anemometerTowerName) {
        Example queryExample = new Example(AnemometerTowerPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("anemometerName", anemometerTowerName);
        return anemometerTowerMapper.selectOneByExample(queryExample);
    }

    @Override
    public List<AnemometerTowerNameVo> getAnemometerTowerNameList() {

        AnemometerTowerNameVo anemometerTowerNameVo = null;
        List<AnemometerTowerNameVo> list = Lists.newArrayList();
        Example queryExample = new Example(AnemometerTowerPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("delState", DeleteStatusEnum.NODELETE.getCode());
        criteria.andEqualTo("state",Constants.GLOBAL_IN_USE);
        List<AnemometerTowerPO> anemometerTowerPOList =  anemometerTowerMapper.selectByExample(queryExample);
        for(AnemometerTowerPO po : anemometerTowerPOList){
            anemometerTowerNameVo = new AnemometerTowerNameVo();
            BeanUtil.copyProperties(po,anemometerTowerNameVo);
            list.add(anemometerTowerNameVo);
        }
        return list;
    }


}
