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

import com.energy.manage.common.enums.TypeRelationEnum;
import com.energy.manage.common.po.system.SysOrganizationAuthPO;
import com.energy.manage.service.constant.analysis.AnalysisConstants;
import com.energy.manage.service.constant.analysis.AnalysisStatusConstants;
import com.energy.manage.service.domain.vo.cockpit.CompanyFieldGroupNumberVo;
import com.energy.manage.service.domain.vo.cockpit.CompanyFieldNumberVo;
import com.energy.manage.service.domain.vo.cockpit.CompanyFieldTreeRoleVo;
import com.energy.manage.service.domain.vo.system.SysOrganizationAuthVo;
import com.energy.manage.service.mappers.system.SysOrganizationAuthMapper;
import com.energy.manage.service.service.system.SysOrganizationAuthService;
import com.energy.manage.service.util.BeanMapUtils;
import com.energy.manage.service.util.TreeUtil;
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.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 企业风场关系
 * @author chy
 * @date 2024/5/21 09:36
 * @desc
 */
@Slf4j
@Service
public class SysOrganizationAuthServiceImpl implements SysOrganizationAuthService {

    /**
     * 根据角色查询风场编号列表返回key
     */
    private final static String FILE_CODE_LIST_KEY = "fieldCodeList";

    @Autowired
    private SysOrganizationAuthMapper sysOrganizationAuthMapper;

    /**
     * 根据角色id获取风场编号列表
     * @return
     */
    @Override
    public Map<String, List<SysOrganizationAuthVo>> queryCodeNunByRoleId(Integer roleId) {
        Map<String, List<SysOrganizationAuthVo>> returnMap = new HashMap<>();
        List<SysOrganizationAuthVo> authPoList = sysOrganizationAuthMapper.selectByRoleId(roleId);
        authPoList = authPoList.parallelStream().filter(po -> po.getCodeType().equals(TypeRelationEnum.WIND_FIELD_NUMBER.getCode()))
                .collect(Collectors.toList());
        returnMap.put(FILE_CODE_LIST_KEY, authPoList);
        return returnMap;
    }

    @Override
    public boolean verifySysOrganizationAuthByType(String codeName, String type) {
        Example queryExample = new Example(SysOrganizationAuthPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("codeNumber", codeName);
        criteria.andEqualTo("codeType", type);
        return sysOrganizationAuthMapper.selectCountByExample(queryExample) > 0 ? false : true;
    }

    @Override
    public List<Map<String, Object>> getSysOrganizationAuthTreeByRoleId(Integer roleId) {

        List<CompanyFieldTreeRoleVo> list = sysOrganizationAuthMapper.selectSysOrganizationAuthTreeByRoleId(roleId);
        for (CompanyFieldTreeRoleVo vo : list) {
            if (vo.getLongitude() != null && vo.getLatitude() != null) {
                DecimalFormat df = new DecimalFormat("#.00000");
                String longitude = df.format(vo.getLongitude());
                String latitude = df.format(vo.getLatitude());
                vo.setLongitudeAndLatitudeString(longitude + "," + latitude);
            }
        }
        // 转化为Map集合
        List<Map<String, Object>> mapList = BeanMapUtils.listBeanToListMap(list);
        // 获取树形结构
        List<Map<String, Object>> java8ResultTree = TreeUtil.getJava8ResultTree(mapList);

        return java8ResultTree;
    }

    @Override
    public List<CompanyFieldTreeRoleVo> getSysOrganizationAuthListByRoleId(Integer roleId) {
        List<CompanyFieldTreeRoleVo> list = sysOrganizationAuthMapper.selectSysOrganizationAuthListByRoleId(roleId);
        return list;
    }

    @Override
    public CompanyFieldNumberVo getCompanyFieldNumberVo(Integer roleId) {
        List<CompanyFieldGroupNumberVo> companyFieldGroupNumberVos = sysOrganizationAuthMapper.selectFieldAndGrooupNumberByRoleId(roleId);
        if (CollectionUtils.isEmpty(companyFieldGroupNumberVos)) {
            return null;
        }
        CompanyFieldNumberVo companyFieldNumberVo = new CompanyFieldNumberVo();
        int fieldSumNumber = 0;
        int engineGroupNumber = 0;
        int analysisFinishNumber = 0;
        int analysisUnFinishedNumber = 0;
        for (CompanyFieldGroupNumberVo companyFieldGroupNumberVo : companyFieldGroupNumberVos) {
            // 风场
            if (!StringUtils.isEmpty(companyFieldGroupNumberVo.getFieldCode())) {
                fieldSumNumber += 1;
            }
            // 风机
            if (companyFieldGroupNumberVo.getEngineGroupNumber() != null && companyFieldGroupNumberVo.getEngineGroupNumber() > 0) {
                engineGroupNumber += companyFieldGroupNumberVo.getEngineGroupNumber();
            }
            // 未分析
            if (companyFieldGroupNumberVo.getAnalysisState() != null && companyFieldGroupNumberVo.getAnalysisState() < AnalysisStatusConstants.ANALYSISING_STATE) {
                analysisUnFinishedNumber += 1;
            }
            if (companyFieldGroupNumberVo.getAnalysisState() == null) {
                analysisUnFinishedNumber += 1;
            }
            // 已分析
            if (companyFieldGroupNumberVo.getAnalysisState() != null
                    && companyFieldGroupNumberVo.getAnalysisState() > AnalysisStatusConstants.ANALYSISING_STATE) {
                analysisFinishNumber += 1;
            }
        }
        companyFieldNumberVo.setFieldSumNumber(fieldSumNumber);
        companyFieldNumberVo.setEngineGroupNumber(engineGroupNumber);
        companyFieldNumberVo.setAnalysisFinishNumber(analysisFinishNumber);
        companyFieldNumberVo.setAnalysisUnFinishedNumber(analysisUnFinishedNumber);
        return companyFieldNumberVo;
    }

    @Override
    public int delSysOrganizationAuth(String codeNumber,String type) {
        Example queryExample = new Example(SysOrganizationAuthPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("codeNumber", codeNumber);
        criteria.andEqualTo("codeType", type);
        return sysOrganizationAuthMapper.deleteByExample(queryExample);
    }


}
