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


import cn.hutool.core.bean.BeanUtil;
import com.energy.manage.common.base.BaseServiceImpl;
import com.energy.manage.common.enums.TypeRelationEnum;
import com.energy.manage.common.po.windfield.WindFieldPO;
import com.energy.manage.common.po.windrelation.WindRelationPO;
import com.energy.manage.service.domain.dto.windrelation.WindRelationDelDto;
import com.energy.manage.service.domain.dto.windrelation.WindRelationDto;
import com.energy.manage.service.domain.vo.company.WindCompanyPageVo;
import com.energy.manage.service.domain.vo.windrelation.WindRelationVo;
import com.energy.manage.service.mappers.windrelation.WindRelationMapper;
import com.energy.manage.service.service.windrelation.WindRelationService;
import com.energy.manage.service.util.BeanMapUtils;
import com.energy.manage.service.util.TreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

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

@Slf4j
@Service
public class WindRelationServiceImpl extends BaseServiceImpl<WindRelationPO> implements WindRelationService {

    @Autowired
    private WindRelationMapper windRelationMapper;

    @Override
    public boolean createWindRelation(WindRelationDto windRelationDto) {
        if (windRelationDto == null) {
            return false;
        }
        WindRelationPO windRelationPO = new WindRelationPO();
        BeanUtil.copyProperties(windRelationDto, windRelationPO);
        windRelationPO.setCreateTime(new Date());
        windRelationPO.setUpdateTime(new Date());
        return windRelationMapper.insertUseGeneratedKeys(windRelationPO) > 0;
    }


    @Override
    public boolean createWindRelationList(List<WindRelationDto> windRelationListDto) {
        if (CollectionUtils.isEmpty(windRelationListDto)) {
            return false;
        }
        WindRelationPO windRelationPO = null;
        List<WindRelationPO> list = Lists.newArrayList();
        for (WindRelationDto windRelationDto : windRelationListDto) {
            windRelationPO = new WindRelationPO();
            BeanUtil.copyProperties(windRelationDto, windRelationPO);
            windRelationPO.setCreateTime(new Date());
            windRelationPO.setUpdateTime(new Date());
            list.add(windRelationPO);
        }
        return windRelationMapper.insertList(list) > 0;
    }


    @Override
    public boolean delWindRelation(WindRelationDelDto windRelationDelDto) {
        Example queryExample = new Example(WindRelationPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("codeNumber", windRelationDelDto.getCodeNumber());
        criteria.andEqualTo("type", windRelationDelDto.getType());
        return windRelationMapper.deleteByExample(queryExample) > 0;
    }

    @Override
    public boolean updateWindRelationName(WindRelationDto windRelationDto) {
        WindRelationPO windRelationPO = new WindRelationPO();
        windRelationPO.setCodeName(windRelationDto.getCodeName());

        Example queryExample = new Example(WindRelationPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("codeNumber", windRelationDto.getCodeNumber());

        return windRelationMapper.updateByExampleSelective(windRelationPO, queryExample) > 0;
    }


    @Override
    public  List<Map<String, Object>> getWindRelationCompany(Integer state,String companyName,Integer roleId,Integer userId) {

        List<WindCompanyPageVo> windCompanyPageVos = windRelationMapper.selectWindRelationCompanyByType(TypeRelationEnum.COMPANY_NUMBER.getCode(),state,companyName,roleId,userId);
        // 转化为Map集合
        List<Map<String, Object>> mapList = BeanMapUtils.listBeanToListMap(windCompanyPageVos);
        // 获取树形结构
        List<Map<String, Object>> java8ResultTree = TreeUtil.getJava8ResultTree(mapList);
        // map转list
//        List<WindCompanyPageVo> poList = BeanMapUtils.listMapToListBean(java8ResultTree, WindCompanyPageVo.class);

        return java8ResultTree;
    }

    @Override
    public  List<Map<String, Object>> getWindRelationTree() {

        WindRelationVo windRelationVo = null;
        List<WindRelationVo> windRelationVos = Lists.newArrayList();
        List<WindRelationPO> list =  windRelationMapper.selectAll();
        for(WindRelationPO po : list){
            windRelationVo = new WindRelationVo();
            BeanUtil.copyProperties(po,windRelationVo);
            windRelationVos.add(windRelationVo);
        }
        // 转化为Map集合
        List<Map<String, Object>> mapList = BeanMapUtils.listBeanToListMap(windRelationVos);
        // 获取树形结构
        List<Map<String, Object>> java8ResultTree = TreeUtil.getJava8ResultTree(mapList);
        // map转list
//        List<WindRelationVo> voList = BeanMapUtils.listMapToListBean(java8ResultTree, WindRelationVo.class);

        return java8ResultTree;
    }


    @Override
    public int getCountParentCode(String fieldCode) {
        Example queryExample = new Example(WindRelationPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("parentCode",fieldCode);
        criteria.andEqualTo("type",TypeRelationEnum.COMPANY_NUMBER.getCode());
        return  windRelationMapper.selectCountByExample(queryExample);
    }


}
