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.RdClosedBuildingProjectMapper;
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.RdClosedBuildingProjectVO;
import cn.com.countrygarden.bi.main.dto.districtReport.repossessedERP;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.districtReport.RdClosedBuildingProject;
import cn.com.countrygarden.bi.main.model.districtReport.RdDistrictInfo;
import cn.com.countrygarden.bi.main.service.districtReport.RdClosedBuildingProjectService;
import cn.com.countrygarden.bi.main.service.districtReport.RdProjectpermissionService;
import cn.com.countrygarden.bi.main.util.ExportExcelUtil;
import cn.com.countrygarden.bi.main.util.erp.StringUtil;
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.*;

/**
 * 已收楼项目-业务逻辑层实现
 *
 * @author 吕伟峰
 * @email www.justintoforest@gamail.com
 * @url 
 * @date 2019-08-14
 */
@Service
@Transactional
public class RdClosedBuildingProjectServiceImpl implements RdClosedBuildingProjectService{
    @Autowired
    private RdClosedBuildingProjectMapper rdClosedBuildingProjectMapper;

    @Autowired
    private ManagerMapForErpMapper managerMapForErpMapper;

    @Autowired
    private RdDistrictInfoMapper rdDistrictInfoMapper;

    @Autowired
    private RdProjectpermissionService rdProjectpermissionService;

    @Override
    public Result queryPageList(RdClosedBuildingProjectDTO rdClosedBuildingProjectDTO, int page, int size,AccessToken accessToken) {

        // 获取登录人的角色授权
        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){
                rdClosedBuildingProjectDTO.setCreateBy(accessToken.getUserCode());
                commIdString=null;
                organCodeString=null;
                break;
                /*commIdString=managerMapForErpMapper.commIdString(accessToken.getUserCode());
                if(commIdString==null||commIdString.size()==0){
                    return null;
                }
                break;*/
            }
        }

        if(StringUtil.isNotBlank(rdClosedBuildingProjectDTO.getFirstPayDateList())){
            String[] firstPayDate=rdClosedBuildingProjectDTO.getFirstPayDateList().split(",");
            rdClosedBuildingProjectDTO.setFirstPayDateList1(firstPayDate[0]);
            rdClosedBuildingProjectDTO.setFirstPayDateList2(firstPayDate[1]);
        }
        Page<Object> pageList = PageHelper.startPage(page, size);
		List<RdClosedBuildingProjectDTO> queryPageList=rdClosedBuildingProjectMapper.queryPageList(rdClosedBuildingProjectDTO,organCodeString,commIdString,principaString);
		if (queryPageList!=null){
            for (int i=0;i<queryPageList.size();i++){
                if (StringUtil.isNotBlank(queryPageList.get(i).getIndustryComposition())){
                    String[] industryComposition=queryPageList.get(i).getIndustryComposition().split(",");
                    List<String> industryCompo = Arrays.asList(industryComposition);
                    queryPageList.get(i).setIndustryCompositionList(industryCompo);
                }
                if (StringUtil.isNotBlank(queryPageList.get(i).getPropertyPrice())){
                    String[] propertyPrice=queryPageList.get(i).getPropertyPrice().split(",");
                    List<String> property = Arrays.asList(propertyPrice);
                    queryPageList.get(i).setPropertyPriceList(property);
                }
                if (StringUtil.isNotBlank(queryPageList.get(i).getCarPrice())){
                    String[] carPrice=queryPageList.get(i).getCarPrice().split(",");
                    List<String> car = Arrays.asList(carPrice);
                    queryPageList.get(i).setCarPriceList(car);
                }

                if(StringUtil.isNotBlank(queryPageList.get(i).getCommitteeProject())){
                    String[] committeeProject=queryPageList.get(i).getCommitteeProject().split(",");
                    List<String> committeeProjectLi = Arrays.asList(committeeProject);
                    queryPageList.get(i).setCommitteeProjectList(committeeProjectLi);
                }

                if(StringUtil.isNotBlank(queryPageList.get(i).getCommitteeUnit())){
                    String[] committeeUnit=queryPageList.get(i).getCommitteeUnit().split(",");
                    List<String> committeeUnitLi = Arrays.asList(committeeUnit);
                    queryPageList.get(i).setCommitteeUnitList(committeeUnitLi);
                }

                if(StringUtil.isNotBlank(queryPageList.get(i).getCommitteeRate())){
                    String[] committeeRate=queryPageList.get(i).getCommitteeRate().split(",");
                    List<String> committeeRateLi = Arrays.asList(committeeRate);
                    queryPageList.get(i).setCommitteeRateList(committeeRateLi);
                }
            }
		}
        PageData data=new PageData(pageList);
        data.setData(pageList.getResult());
        return Response.response(data);
    }

    @Override
    public Result queryById(Integer id) {
        return Response.response(rdClosedBuildingProjectMapper.queryById(id));
    }

    @Override
    public Result closedBuildingProjectAddOne(String regionCode, String projectCode,AccessToken accessToken) {
        Map<String, Object> rolesMap = rdProjectpermissionService.findRoleInfoMap(accessToken.getUserCode());
        List<Integer> paixu=new ArrayList<>();
        for (Map.Entry<String, Object> entry : rolesMap.entrySet()) {
            if(entry.getKey().equals("project_manager_admin")){
                paixu.add(1);
            }
            if(entry.getKey().equals("project_manager_regionAdmin")){
                paixu.add(2);
            }
            if(entry.getKey().equals("project_manager_regionUser")){
                paixu.add(3);
            }
            if(entry.getKey().equals("project_manager_projectUser")){
                paixu.add(4);
            }
            if(entry.getKey().equals("role")){
                throw new ServiceException("权限不足,无法使用此模块");
            }
        }
        Collections.sort(paixu);
        String createdBy=null;
        if(paixu.get(0)==4){
            createdBy=accessToken.getUserCode();
        }
        return Response.response(rdClosedBuildingProjectMapper.closedBuildingProjectAddOne(regionCode,projectCode,createdBy));
    }

    @Override
    public Result add(RdClosedBuildingProjectDTO rdClosedBuildingProjectDTO, AccessToken accessToken) {
        int count=rdClosedBuildingProjectMapper.BuildingProjectsAddone(rdClosedBuildingProjectDTO);
        if(count>0){
            return Response.responseFail("项目名不能重复，请核实");
        }
        List<RdDistrictInfo> rdDistrictInfoBown=rdDistrictInfoMapper.rdDistrictInfoBown(rdClosedBuildingProjectDTO.getRegionCode(),null);
        List<repossessedERP> repossessedERPList=managerMapForErpMapper.repossessedERPList(rdClosedBuildingProjectDTO.getRegionCode(),rdClosedBuildingProjectDTO.getProjectCode());
         if (repossessedERPList!=null){
             try {
                /* if(repossessedERPList.get(0).getPlanPlaceArea()!=null||repossessedERPList.get(0).getPlanBuildArea()!=null||repossessedERPList.get(0).getManagerArea()!=null||repossessedERPList.get(0).getPlanCarArea()!=null
                 ||repossessedERPList.get(0).getHouseChargesArea()!=null||repossessedERPList.get(0).getBusinessChargesArea()!=null||repossessedERPList.get(0).getPlanHouseholds()!=null||repossessedERPList.get(0).getManagerHouseholds()!=null
                 ||repossessedERPList.get(0).getManagerCarHouseholds()!=null||repossessedERPList.get(0).getIndustryComposition()!=null||repossessedERPList.get(0).getFirstPayDate()!=null||repossessedERPList.get(0).getPropertyPrice()!=null
                 ||repossessedERPList.get(0).getCarPrice()!=null||repossessedERPList.get(0).getPropertyCollectionRate()!=null||repossessedERPList.get(0).getLastYearRecoveryRate()!=null||repossessedERPList.get(0).getGreenArea()!=null|| repossessedERPList.get(0).getVolumeRatio()!=null
                 ||repossessedERPList.get(0).getBuildingDensity()!=null||repossessedERPList.get(0).getProjectPlace()!=null||repossessedERPList.get(0).getOfficePlace()!=null||repossessedERPList.get(0).getContact()!=null||repossessedERPList.get(0).getProjectResponsibler()!=null){*/

                     RdClosedBuildingProject rdClosedBuildingProject=new RdClosedBuildingProject();
                     rdClosedBuildingProject.setDistrictCode(rdClosedBuildingProjectDTO.getDistrictCode());
                     rdClosedBuildingProject.setProjectCode(repossessedERPList.get(0).getCommid());
                     rdClosedBuildingProject.setProjectName(repossessedERPList.get(0).getCommName());
                     if(repossessedERPList.get(0).getPlanPlaceArea()!=null){
                         rdClosedBuildingProject.setPlanPlaceArea(repossessedERPList.get(0).getPlanPlaceArea());
                     }
                     if (repossessedERPList.get(0).getPlanBuildArea()!=null){
                         rdClosedBuildingProject.setPlanBuildArea(repossessedERPList.get(0).getPlanBuildArea());
                     }
                     if(repossessedERPList.get(0).getManagerArea()!=null){
                         rdClosedBuildingProject.setManagerArea(repossessedERPList.get(0).getManagerArea());
                     }
                     if(repossessedERPList.get(0).getPlanCarArea()!=null){
                         rdClosedBuildingProject.setPlanCarArea(repossessedERPList.get(0).getPlanCarArea());
                     }
                     if(repossessedERPList.get(0).getHouseChargesArea()!=null){
                         rdClosedBuildingProject.setHouseChargesArea(repossessedERPList.get(0).getHouseChargesArea());
                     }
                     if (repossessedERPList.get(0).getBusinessChargesArea()!=null){
                         rdClosedBuildingProject.setBusinessChargesArea(repossessedERPList.get(0).getBusinessChargesArea());
                     }
                     if(repossessedERPList.get(0).getPlanHouseholds()!=null){
                         rdClosedBuildingProject.setPlanHouseholds(repossessedERPList.get(0).getPlanHouseholds());
                     }
                     if(repossessedERPList.get(0).getManagerHouseholds()!=null){
                         rdClosedBuildingProject.setManagerHouseholds(repossessedERPList.get(0).getManagerHouseholds());
                     }
                     if(repossessedERPList.get(0).getManagerCarHouseholds()!=null){
                         rdClosedBuildingProject.setManagerCarHouseholds(repossessedERPList.get(0).getManagerCarHouseholds());
                     }
                     if(repossessedERPList.get(0).getIndustryComposition()!=null){
                         rdClosedBuildingProject.setIndustryComposition(repossessedERPList.get(0).getIndustryComposition());
                     }
                     if(repossessedERPList.get(0).getFirstPayDate()!=null){
                         rdClosedBuildingProject.setFirstPayDate(repossessedERPList.get(0).getFirstPayDate());
                     }
                     rdClosedBuildingProject.setPropertyPrice(repossessedERPList.get(0).getPropertyPrice());
                      if(repossessedERPList.get(0).getCarPrice()!=null){
                        rdClosedBuildingProject.setCarPrice(repossessedERPList.get(0).getCarPrice());
                     }
                     rdClosedBuildingProject.setPropertyCollectionRate(repossessedERPList.get(0).getPropertyCollectionRate());
                     rdClosedBuildingProject.setLastYearRecoveryRate(repossessedERPList.get(0).getLastYearRecoveryRate());
                 if(repossessedERPList.get(0).getGreenArea()!=null){
                     rdClosedBuildingProject.setGreenArea(repossessedERPList.get(0).getGreenArea());
                 }
                 if(repossessedERPList.get(0).getVolumeRatio()!=null){
                     rdClosedBuildingProject.setVolumeRatio(repossessedERPList.get(0).getVolumeRatio());
                 }
                 if(repossessedERPList.get(0).getBuildingDensity()!=null){
                     rdClosedBuildingProject.setBuildingDensity(repossessedERPList.get(0).getBuildingDensity());
                 }
                     rdClosedBuildingProject.setProjectPlace(repossessedERPList.get(0).getProjectPlace());
                     rdClosedBuildingProject.setOfficePlace(repossessedERPList.get(0).getOfficePlace());
                     rdClosedBuildingProject.setContact(repossessedERPList.get(0).getContact());
                     rdClosedBuildingProject.setProjectResponsibler(repossessedERPList.get(0).getProjectResponsibler());
                     rdClosedBuildingProject.setElevatorCount(rdClosedBuildingProjectDTO.getElevatorCount());
                     rdClosedBuildingProject.setCommitteeRate(rdClosedBuildingProjectDTO.getCommitteeRate());
                     rdClosedBuildingProject.setCommitteeProject(rdClosedBuildingProjectDTO.getCommitteeProject());
                     rdClosedBuildingProject.setCommitteeUnit(rdClosedBuildingProjectDTO.getCommitteeUnit());
                     rdClosedBuildingProject.setCompilingCustomer(rdClosedBuildingProjectDTO.getCompilingCustomer());
                     rdClosedBuildingProject.setCompilingOrder(rdClosedBuildingProjectDTO.getCompilingOrder());
                     rdClosedBuildingProject.setCompilingEngineers(rdClosedBuildingProjectDTO.getCompilingEngineers());
                     rdClosedBuildingProject.setCompilingCleaner(rdClosedBuildingProjectDTO.getCompilingCleaner());
                     rdClosedBuildingProject.setCompilingCase(rdClosedBuildingProjectDTO.getCompilingCase());
                     rdClosedBuildingProject.setCompilingOther(rdClosedBuildingProjectDTO.getCompilingOther());
                     rdClosedBuildingProject.setDescs(rdClosedBuildingProjectDTO.getDescs());
                     rdClosedBuildingProject.setEnable(1);
                     rdClosedBuildingProject.setCreateTime(new Date());
                     rdClosedBuildingProject.setCreateBy(accessToken.getUserCode());
                     rdClosedBuildingProject.setCreateName(rdClosedBuildingProjectDTO.getCreateName());
                     rdClosedBuildingProjectMapper.insert(rdClosedBuildingProject);
                 //}

             }catch (Exception e){
                 e.printStackTrace();
                 return Response.responseFail("该项目没有相应信息，请检查ERP库");
             }
         }else {
             return Response.responseFail("该项目没有相应信息，请检查ERP库");
         }
        return Response.response("添加成功");
    }

    @Override
    public Result edit(RdClosedBuildingProject rdClosedBuildingProject, AccessToken accessToken) {
        rdClosedBuildingProject.setUpdateTime(new Date());
        rdClosedBuildingProjectMapper.update(rdClosedBuildingProject);
        return Response.response();
    }

    @Override
    public Result del(Integer id,AccessToken accessToken,String updateName) {
        int count = rdClosedBuildingProjectMapper.deleteUpdate(id,accessToken.getUserCode(),updateName);
        return Response.response(count);
    }

    @Override
    public Result delpiliang(String ids,AccessToken accessToken,String updateName) {
        String[] idString=ids.split(",");
        List<String> rids =  Arrays.asList(idString);
        rdClosedBuildingProjectMapper.delpingliangtag(rids,accessToken.getUserCode(),updateName);
        return Response.response("删除成功");
    }

    @Override
    public Result synchronousData(Integer id, String regionCode, String projectCode, String syncUsercode, AccessToken accessToken) {
        List<repossessedERP> repossessedERPList=managerMapForErpMapper.repossessedERPList(regionCode,projectCode);
        if (repossessedERPList!=null){
            try {
                RdClosedBuildingProject rdClosedBuildingProject=new RdClosedBuildingProject();
                rdClosedBuildingProject.setId(id);
                rdClosedBuildingProject.setProjectCode(repossessedERPList.get(0).getCommid());
                rdClosedBuildingProject.setProjectName(repossessedERPList.get(0).getCommName());
                if(repossessedERPList.get(0).getPlanPlaceArea()!=null){
                    rdClosedBuildingProject.setPlanPlaceArea(repossessedERPList.get(0).getPlanPlaceArea());
                }
                if (repossessedERPList.get(0).getPlanBuildArea()!=null){
                    rdClosedBuildingProject.setPlanBuildArea(repossessedERPList.get(0).getPlanBuildArea());
                }
                if(repossessedERPList.get(0).getManagerArea()!=null){
                    rdClosedBuildingProject.setManagerArea(repossessedERPList.get(0).getManagerArea());
                }
                if(repossessedERPList.get(0).getPlanCarArea()!=null){
                    rdClosedBuildingProject.setPlanCarArea(repossessedERPList.get(0).getPlanCarArea());
                }
                if(repossessedERPList.get(0).getHouseChargesArea()!=null){
                    rdClosedBuildingProject.setHouseChargesArea(repossessedERPList.get(0).getHouseChargesArea());
                }
                if (repossessedERPList.get(0).getBusinessChargesArea()!=null){
                    rdClosedBuildingProject.setBusinessChargesArea(repossessedERPList.get(0).getBusinessChargesArea());
                }
                if(repossessedERPList.get(0).getPlanHouseholds()!=null){
                    rdClosedBuildingProject.setPlanHouseholds(repossessedERPList.get(0).getPlanHouseholds());
                }
                if(repossessedERPList.get(0).getManagerHouseholds()!=null){
                    rdClosedBuildingProject.setManagerHouseholds(repossessedERPList.get(0).getManagerHouseholds());
                }
                if(repossessedERPList.get(0).getManagerCarHouseholds()!=null){
                    rdClosedBuildingProject.setManagerCarHouseholds(repossessedERPList.get(0).getManagerCarHouseholds());
                }
                if(repossessedERPList.get(0).getIndustryComposition()!=null){
                    rdClosedBuildingProject.setIndustryComposition(repossessedERPList.get(0).getIndustryComposition());
                }
                if(repossessedERPList.get(0).getFirstPayDate()!=null){
                    rdClosedBuildingProject.setFirstPayDate(repossessedERPList.get(0).getFirstPayDate());
                }
                rdClosedBuildingProject.setPropertyPrice(repossessedERPList.get(0).getPropertyPrice());
                if(repossessedERPList.get(0).getCarPrice()!=null){
                    rdClosedBuildingProject.setCarPrice(repossessedERPList.get(0).getCarPrice());
                }
                rdClosedBuildingProject.setPropertyCollectionRate(repossessedERPList.get(0).getPropertyCollectionRate());
                rdClosedBuildingProject.setLastYearRecoveryRate(repossessedERPList.get(0).getLastYearRecoveryRate());
                if(repossessedERPList.get(0).getGreenArea()!=null){
                    rdClosedBuildingProject.setGreenArea(repossessedERPList.get(0).getGreenArea());
                }
                if(repossessedERPList.get(0).getVolumeRatio()!=null){
                    rdClosedBuildingProject.setVolumeRatio(repossessedERPList.get(0).getVolumeRatio());
                }
                if(repossessedERPList.get(0).getBuildingDensity()!=null){
                    rdClosedBuildingProject.setBuildingDensity(repossessedERPList.get(0).getBuildingDensity());
                }
                rdClosedBuildingProject.setProjectPlace(repossessedERPList.get(0).getProjectPlace());
                rdClosedBuildingProject.setOfficePlace(repossessedERPList.get(0).getOfficePlace());
                rdClosedBuildingProject.setContact(repossessedERPList.get(0).getContact());
                rdClosedBuildingProject.setProjectResponsibler(repossessedERPList.get(0).getProjectResponsibler());
                rdClosedBuildingProject.setSyncDate(new Date());
                rdClosedBuildingProject.setSyncUsercode(syncUsercode);
                rdClosedBuildingProjectMapper.update(rdClosedBuildingProject);

            }catch (Exception e){
                e.printStackTrace();
                return Response.responseFail("该项目没有相应信息，同步失败");
            }
        }else {
            return Response.responseFail("该项目没有相应信息，同步失败");
        }

        return Response.response("同步成功");
    }

    @Override
    public Result synchronousDataAll(String ids, String regionCodeList, String projectCodeList, String syncUsercode, AccessToken accessToken) {

        String[] idsInte=ids.split(",");
        String[] regionCodeInte=regionCodeList.split(",");
        String[] projectCodeInte=projectCodeList.split(",");

        List<String> id = Arrays.asList(idsInte);
        List<String> regionCode = Arrays.asList(regionCodeInte);
        List<String> projectCodeCode = Arrays.asList(projectCodeInte);

        for(int i=0;i<id.size();i++){
            List<repossessedERP> repossessedERPList=managerMapForErpMapper.repossessedERPList(regionCode.get(i),projectCodeCode.get(i));
            if (repossessedERPList!=null){
                try {
                    RdClosedBuildingProject rdClosedBuildingProject=new RdClosedBuildingProject();
                    rdClosedBuildingProject.setId(Integer.parseInt(id.get(i)));
                    rdClosedBuildingProject.setProjectCode(repossessedERPList.get(0).getCommid());
                    rdClosedBuildingProject.setProjectName(repossessedERPList.get(0).getCommName());
                    if(repossessedERPList.get(0).getPlanPlaceArea()!=null){
                        rdClosedBuildingProject.setPlanPlaceArea(repossessedERPList.get(0).getPlanPlaceArea());
                    }
                    if (repossessedERPList.get(0).getPlanBuildArea()!=null){
                        rdClosedBuildingProject.setPlanBuildArea(repossessedERPList.get(0).getPlanBuildArea());
                    }
                    if(repossessedERPList.get(0).getManagerArea()!=null){
                        rdClosedBuildingProject.setManagerArea(repossessedERPList.get(0).getManagerArea());
                    }
                    if(repossessedERPList.get(0).getPlanCarArea()!=null){
                        rdClosedBuildingProject.setPlanCarArea(repossessedERPList.get(0).getPlanCarArea());
                    }
                    if(repossessedERPList.get(0).getHouseChargesArea()!=null){
                        rdClosedBuildingProject.setHouseChargesArea(repossessedERPList.get(0).getHouseChargesArea());
                    }
                    if (repossessedERPList.get(0).getBusinessChargesArea()!=null){
                        rdClosedBuildingProject.setBusinessChargesArea(repossessedERPList.get(0).getBusinessChargesArea());
                    }
                    if(repossessedERPList.get(0).getPlanHouseholds()!=null){
                        rdClosedBuildingProject.setPlanHouseholds(repossessedERPList.get(0).getPlanHouseholds());
                    }
                    if(repossessedERPList.get(0).getManagerHouseholds()!=null){
                        rdClosedBuildingProject.setManagerHouseholds(repossessedERPList.get(0).getManagerHouseholds());
                    }
                    if(repossessedERPList.get(0).getManagerCarHouseholds()!=null){
                        rdClosedBuildingProject.setManagerCarHouseholds(repossessedERPList.get(0).getManagerCarHouseholds());
                    }
                    if(repossessedERPList.get(0).getIndustryComposition()!=null){
                        rdClosedBuildingProject.setIndustryComposition(repossessedERPList.get(0).getIndustryComposition());
                    }
                    if(repossessedERPList.get(0).getFirstPayDate()!=null){
                        rdClosedBuildingProject.setFirstPayDate(repossessedERPList.get(0).getFirstPayDate());
                    }
                    rdClosedBuildingProject.setPropertyPrice(repossessedERPList.get(0).getPropertyPrice());
                    if(repossessedERPList.get(0).getCarPrice()!=null){
                        rdClosedBuildingProject.setCarPrice(repossessedERPList.get(0).getCarPrice());
                    }
                    rdClosedBuildingProject.setPropertyCollectionRate(repossessedERPList.get(0).getPropertyCollectionRate());
                    rdClosedBuildingProject.setLastYearRecoveryRate(repossessedERPList.get(0).getLastYearRecoveryRate());
                    if(repossessedERPList.get(0).getGreenArea()!=null){
                        rdClosedBuildingProject.setGreenArea(repossessedERPList.get(0).getGreenArea());
                    }
                    if(repossessedERPList.get(0).getVolumeRatio()!=null){
                        rdClosedBuildingProject.setVolumeRatio(repossessedERPList.get(0).getVolumeRatio());
                    }
                    if(repossessedERPList.get(0).getBuildingDensity()!=null){
                        rdClosedBuildingProject.setBuildingDensity(repossessedERPList.get(0).getBuildingDensity());
                    }
                    rdClosedBuildingProject.setProjectPlace(repossessedERPList.get(0).getProjectPlace());
                    rdClosedBuildingProject.setOfficePlace(repossessedERPList.get(0).getOfficePlace());
                    rdClosedBuildingProject.setContact(repossessedERPList.get(0).getContact());
                    rdClosedBuildingProject.setProjectResponsibler(repossessedERPList.get(0).getProjectResponsibler());
                    rdClosedBuildingProject.setSyncDate(new Date());
                    rdClosedBuildingProject.setSyncUsercode(syncUsercode);
                    rdClosedBuildingProjectMapper.update(rdClosedBuildingProject);
                }catch (Exception e){
                    e.printStackTrace();
                    return Response.responseFail("该项目没有相应信息，同步失败");
                }
            }else {
                return Response.responseFail("该项目没有相应信息，同步失败");
            }
        }
        return Response.response("同步成功");
    }

    @Override
    public HSSFWorkbook exportAllRdClosedBuildingProject(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[] columnNamesyishou = { "区域名称" ,"片区名称" ,"项目名称"  , "规划占地面积" , "规划建筑面积" , "接管建筑面积","规划车位面积","可收费面积-住宅",
                "可收费面积-商业" ,"规划户数（户）","累积接管户数（户）","接管车位数（户）","业态构成","首批交付时间","物业费标准（元/㎡/月）","车位服务费标准（元/月）",
                "物业费综合收缴率","往年欠费追缴率","绿化面积（㎡）","容积率","建筑密度","项目地址","办公地址","联系方式","项目负责人","电梯数量","外占应收比","委外项目","委外单位",
                "编制数量-客服","编制数量-秩序","编制数量-工程","编制数量-保洁","编制数量-案场","编制数量-其他","备注","同步时间","同步人"};
        Collection<RdClosedBuildingProjectVO> listyishou=rdClosedBuildingProjectMapper.evaluationrdClosedBuildingProject(new RdClosedBuildingProjectDTO(),organCodeString,commIdString,principaString);
        HSSFWorkbook resultyishou = util.getWorkbook2003("已收楼项目信息", columnNamesyishou,listyishou, response.getOutputStream(), "yyyy-MM-dd HH:mm:ss", null);
        return resultyishou;
    }

}