package cn.com.countrygarden.bi.main.service.districtReport.impl;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.districtReport.RdDistrictInfoMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.ManagerMapForErpMapper;
import cn.com.countrygarden.bi.main.dto.districtReport.RdClosedBuildingProjectDTO;
import cn.com.countrygarden.bi.main.dto.districtReport.RdDistrictInfoDTO;
import cn.com.countrygarden.bi.main.dto.districtReport.RdprojectInformationSummaryDTO;
import cn.com.countrygarden.bi.main.dto.districtReport.RdprojectInformationSummaryVO;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.service.districtReport.RdProjectpermissionService;
import cn.com.countrygarden.bi.main.service.districtReport.RdprojectInformationSummaryService;
import cn.com.countrygarden.bi.main.util.ExportExcelUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RdprojectInformationSummaryServiceImpl implements RdprojectInformationSummaryService {

    @Autowired
    private ManagerMapForErpMapper managerMapForErpMapper;

    @Autowired
    private RdDistrictInfoMapper rdDistrictInfoMapper;

    @Autowired
    private RdProjectpermissionService rdProjectpermissionService;

    @Override
    public Result queryPageList(RdClosedBuildingProjectDTO rdClosedBuildingProjectDTO, int page, int size, AccessToken accessToken) {
        try {
        // 获取登录人的角色授权
        Map<String, Object> rolesMap = rdProjectpermissionService.findRoleInfoMap(accessToken.getUserCode());

        List<String> organCodeString=new ArrayList<>();
        List<String> commIdString=new ArrayList<>();
        List<String> principaString=new ArrayList<>();
        List<Integer> paixu=rdProjectpermissionService.roleInfoList(rolesMap);

        for (int i=0;i<paixu.size();i++) {
            if (paixu.get(i)==1||paixu.get(i)==2){
                break;
            }else if (paixu.get(i)==3){
                principaString=rdProjectpermissionService.principalList(accessToken.getUserCode());
                break;
            }else if(paixu.get(i)==4||paixu.get(i)==5){
                organCodeString=managerMapForErpMapper.organCodeString(accessToken.getUserCode(),null);
                if(organCodeString==null||organCodeString.size()==0){
                    return null;
                }
                break;
            }else if(paixu.get(i)==6){
                commIdString=managerMapForErpMapper.commIdString(accessToken.getUserCode());
                if(commIdString==null||commIdString.size()==0){
                    return null;
                }
                break;
            }
        }

             List<String> commId=new ArrayList<>();
            List<RdDistrictInfoDTO> rdDistrictProjectDTO=rdDistrictInfoMapper.rdDistrictProjectDTO(rdClosedBuildingProjectDTO.getDistrictCode(),principaString);
            if(rdDistrictProjectDTO.size()==0||rdDistrictProjectDTO==null){
                return null;
            }else {
                for (int i=0;i<rdDistrictProjectDTO.size();i++){
                    commId.add(rdDistrictProjectDTO.get(i).getCommid());
                }
            }
         Page<Object> pageList = PageHelper.startPage(page, size);
        List<RdprojectInformationSummaryDTO> rdprojectInformationSummaryList=managerMapForErpMapper.rdprojectInformationSummaryList(rdClosedBuildingProjectDTO,organCodeString,commIdString,commId);

        if (rdprojectInformationSummaryList.size()>=rdDistrictProjectDTO.size()){
            for (int i=0;i<rdprojectInformationSummaryList.size();i++){
                for (int y=0;y<rdDistrictProjectDTO.size();y++){
                    if (rdprojectInformationSummaryList.get(i).getCommId().equals(rdDistrictProjectDTO.get(y).getCommid())){
                        rdprojectInformationSummaryList.get(i).setDistrictName(rdDistrictProjectDTO.get(y).getDistrictName());
                    }
                }
            }
        }else {
                for (int y=0;y<rdDistrictProjectDTO.size();y++){
                    for (int i=0;i<rdprojectInformationSummaryList.size();i++){
                    if (rdprojectInformationSummaryList.get(i).getCommId().equals(rdDistrictProjectDTO.get(y).getCommid())){
                        rdprojectInformationSummaryList.get(i).setDistrictName(rdDistrictProjectDTO.get(y).getDistrictName());
                    }
                }
            }
        }
         PageData data=new PageData(pageList);
         data.setData(pageList.getResult());
         return Response.response(data);


        }catch (Exception e){
               e.printStackTrace();
        }
        return null;
    }

    @Override
    public HSSFWorkbook exportAllRdprojectInformationSummary(AccessToken accessToken, HttpServletResponse response) throws IOException {

        // 获取登录人的角色授权
        Map<String, Object> rolesMap = rdProjectpermissionService.findRoleInfoMap(accessToken.getUserCode());

        List<String> organCodeString=new ArrayList<>();
        List<String> commIdString=new ArrayList<>();
        List<String> principaString=new ArrayList<>();
        List<Integer> paixu=rdProjectpermissionService.roleInfoList(rolesMap);

        for (int i=0;i<paixu.size();i++) {
            if (paixu.get(i)==1||paixu.get(i)==2){
                break;
            }else if (paixu.get(i)==3){
                principaString=rdProjectpermissionService.principalList(accessToken.getUserCode());
                break;
            }else if(paixu.get(i)==4||paixu.get(i)==5){
                organCodeString=managerMapForErpMapper.organCodeString(accessToken.getUserCode(),null);
                if(organCodeString==null||organCodeString.size()==0){
                    return null;
                }
                break;
            }else if(paixu.get(i)==6){
                commIdString=managerMapForErpMapper.commIdString(accessToken.getUserCode());
                if(commIdString==null||commIdString.size()==0){
                    return null;
                }
                break;
            }
        }
        ExportExcelUtil util = new ExportExcelUtil();
        String[] columnNameszong = { "区域名称" ,"片区名称","项目名称" ,"规划区域" , "项目开发主体" , "住宅用地面积","公建用地面积","道路用地面积",
                "绿地用地面积" ,"住宅数量","商业数量","车位数量","其他数量","住宅总面积","商业总面积","车位总面积",
                "其他总面积","容积率"};
        List<String> commId=new ArrayList<>();
        List<RdDistrictInfoDTO> rdDistrictProjectDTO=rdDistrictInfoMapper.rdDistrictProjectDTO(new RdClosedBuildingProjectDTO().getDistrictCode(),principaString);
        if(rdDistrictProjectDTO.size()==0||rdDistrictProjectDTO==null){

        }else {
            for (int i=0;i<rdDistrictProjectDTO.size();i++){
                commId.add(rdDistrictProjectDTO.get(i).getCommid());
            }
        }
        List<RdprojectInformationSummaryVO> rdprojectInformationSummaryList= managerMapForErpMapper.evaluationrdprojectInformationSummary(new  RdClosedBuildingProjectDTO(),organCodeString,commIdString,commId);
        if (rdprojectInformationSummaryList.size()>=rdDistrictProjectDTO.size()){
            for (int i=0;i<rdprojectInformationSummaryList.size();i++){
                for (int y=0;y<rdDistrictProjectDTO.size();y++){
                    if (rdprojectInformationSummaryList.get(i).getCommId().equals(rdDistrictProjectDTO.get(y).getCommid())){
                        rdprojectInformationSummaryList.get(i).setDistrictName(rdDistrictProjectDTO.get(y).getDistrictName());
                    }
                }
            }
        }else {
            for (int y=0;y<rdDistrictProjectDTO.size();y++){
                for (int i=0;i<rdprojectInformationSummaryList.size();i++){
                    if (rdprojectInformationSummaryList.get(i).getCommId().equals(rdDistrictProjectDTO.get(y).getCommid())){
                        rdprojectInformationSummaryList.get(i).setDistrictName(rdDistrictProjectDTO.get(y).getDistrictName());
                    }
                }
            }
        }

        Collection<RdprojectInformationSummaryVO> listzong=rdprojectInformationSummaryList;
        HSSFWorkbook resultzong = util.getWorkbook2003("项目信息汇总", columnNameszong,listzong, response.getOutputStream(), "yyyy-MM-dd HH:mm:ss", null);
        return resultzong;
    }
}
