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

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.core.ResultCode;
import cn.com.countrygarden.bi.main.dao.reportCityData.CityCompanyDataMapper;
import cn.com.countrygarden.bi.main.dto.extension.ExtensionPageData;
import cn.com.countrygarden.bi.main.dto.reportCityData.CcdrDto;
import cn.com.countrygarden.bi.main.dto.reportCityData.CclDto;
import cn.com.countrygarden.bi.main.dto.reportCityData.CcsjDto;
import cn.com.countrygarden.bi.main.dto.reportCityData.CcsrDto;
import cn.com.countrygarden.bi.main.dto.reportCityData.CcufDto;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.service.reportCityData.CityCompanyDataService;
import cn.com.countrygarden.bi.main.util.ExcelCompanyDataUtil;
import cn.com.countrygarden.bi.main.util.OSSUtils;
import com.github.pagehelper.PageRowBounds;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CityCompanyDataServiceImpl implements CityCompanyDataService {

    @Resource
    CityCompanyDataMapper cityCompanyDataMapper;
    @Autowired
    private OSSUtils ossUtils;

    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 分页查询城市公司数据上报列表
     * @author Song.xy
     * @authDate 2019/01/16
     * @param ccdrDto
     * @return
     */
    @Override
    public Object queryData(CcdrDto ccdrDto) {
        PageRowBounds pageRowBounds = new PageRowBounds(ccdrDto.getPageIndex(),ccdrDto.getPageSize());
        List<CcdrDto> CcdrDtoList = cityCompanyDataMapper.queryUnionData(ccdrDto, pageRowBounds);
        return new ExtensionPageData(CcdrDtoList, pageRowBounds);
    }

    /**
     * 查询单条数据
     * @param ccdrId
     * @return
     */
    @Override
    public Result getData(Integer ccdrId) {
        return Response.response(cityCompanyDataMapper.queryData(ccdrId));
    }

    /**
     * 新增城市上报数据
     * @author Song.xy
     * @authDate 2019/01/17
     * @param ccdrDto
     * @return
     */
    @Transactional
    @Override
    public Result addData(CcdrDto ccdrDto , AccessToken token) {
        int effect = 0;
        if(ccdrDto!=null){
            if(ccdrDto.getManageTime()==null||"".equals(ccdrDto.getManageTime())){
                ccdrDto.setManageTime(sdf.format(new Date()));
            }
            ccdrDto.setCreateDate(sdf.format(new Date()));
            ccdrDto.setCreateBy(token.getUserCode());
            effect = cityCompanyDataMapper.addData(ccdrDto);
            if(ccdrDto.getCclDtoList() != null && ccdrDto.getCclDtoList().size()!=0){
                for(CclDto ccl : ccdrDto.getCclDtoList()){
                    if(ccl!=null){
                        ccl.setCreateDate(sdf.format(new Date()));
                        ccl.setCreateBy(token.getUserCode());
                    }
                }
                cityCompanyDataMapper.addCclData(ccdrDto);
            }
            if(ccdrDto.getCcsjDtoList() != null && ccdrDto.getCcsjDtoList().size()!=0){
                for(CcsjDto ccsj : ccdrDto.getCcsjDtoList()){
                    ccsj.setCreateDate(sdf.format(new Date()));
                    ccsj.setCreateBy(token.getUserCode());
                }
                cityCompanyDataMapper.addCcsjData(ccdrDto);
            }
            if(ccdrDto.getCcsrDtoList() != null && ccdrDto.getCcsrDtoList().size()!=0){
                for(CcsrDto ccsr : ccdrDto.getCcsrDtoList()){
                    ccsr.setCreateDate(sdf.format(new Date()));
                    ccsr.setCreateBy(token.getUserCode());
                }
                cityCompanyDataMapper.addCcsrData(ccdrDto);
            }
            if(ccdrDto.getCcufDtoList() != null && ccdrDto.getCcufDtoList().size()!=0){
                for(CcufDto ccuf : ccdrDto.getCcufDtoList()){
                    ccuf.setCreateDate(sdf.format(new Date()));
                    ccuf.setCreateBy(token.getUserCode());
                    if (ccuf.getFileName().indexOf(".") > 0) {
                        String substring = ccuf.getFileName().substring(ccuf.getFileName().indexOf(".") + 1);
                        ccuf.setFileType(substring);
                    }
                }
                cityCompanyDataMapper.addCcufData(ccdrDto);
            }

        }
        return new Result().setData(effect>0 ? "操作成功！" : "操作失败");
    }

    /**
     * 修改单条城市上报数据
     * @author Song.xy
     * @authDate 2019/01/16
     * @param ccdrDto
     * @return
     */
    @Transactional
    @Override
    public Result updateData(CcdrDto ccdrDto, AccessToken token) {
        List<CclDto> cclDtoList = ccdrDto.getCclDtoList();
        if (cclDtoList != null && cclDtoList.size() > 0){
            for(CclDto ccl : cclDtoList){
                ccl.setLastUpdateBy(token.getUserCode());
                if (ccl.getId() == null){
                    ccl.setCreateBy(token.getUserCode());
                    ccl.setCityCompanyDataReportId(ccdrDto.getId());
                    cityCompanyDataMapper.addCclDto(ccl);
                }
            }
            cityCompanyDataMapper.updateCclDto(ccdrDto);
        }
        List<CcsjDto> ccsjDtoList = ccdrDto.getCcsjDtoList();
        if (ccsjDtoList != null && ccsjDtoList.size() > 0){
            for(CcsjDto ccsj : ccsjDtoList){
                ccsj.setLastUpdateBy(token.getUserCode());
                if (ccsj.getId() == null ){
                    ccsj.setCityCompanyDataReportId(ccdrDto.getId());
                    ccsj.setCreateBy(token.getUserCode());
                    ccsj.setLastUpdateBy(token.getUserCode());
                    cityCompanyDataMapper.addCcsjDto(ccsj);
                }
            }
            cityCompanyDataMapper.updateCcsjDto(ccdrDto);
        }
        List<CcsrDto> ccsrDtoList = ccdrDto.getCcsrDtoList();
        if (ccsrDtoList != null && ccsrDtoList.size() > 0){
            for(CcsrDto ccsr : ccdrDto.getCcsrDtoList()){
                ccsr.setLastUpdateBy(token.getUserCode());
                if (ccsr.getId() == null){
                    ccsr.setCityCompanyDataReportId(ccdrDto.getId());
                    ccsr.setCreateBy(token.getUserCode());
                    ccsr.setLastUpdateBy(token.getUserCode());
                    cityCompanyDataMapper.addCcsrDto(ccsr);
                }
            }
            cityCompanyDataMapper.updateCcsrDto(ccdrDto);
        }
        List<CcufDto> ccufDtoList = ccdrDto.getCcufDtoList();
        if (ccufDtoList != null && ccufDtoList.size() > 0){
            for(CcufDto ccuf :ccdrDto.getCcufDtoList()){
                ccuf.setLastUpdateBy(token.getUserCode());
                ccuf.setCityCompanyDataReportId(ccdrDto.getId());
                cityCompanyDataMapper.updateCcufDto(ccdrDto);
                if (ccuf.getId() == null){
                    ccuf.setCityCompanyDataReportId(ccdrDto.getId());
                    ccuf.setCreateBy(token.getUserCode());
                    if (ccuf.getFileName().indexOf(".") > 0) {
                        String substring = ccuf.getFileName().substring(ccuf.getFileName().indexOf(".") + 1);
                        ccuf.setFileType(substring);
                    }
                    cityCompanyDataMapper.addCcufDto(ccuf);
                }
            }
        }
        int effect = cityCompanyDataMapper.updateData(ccdrDto);

        return new Result().setData(effect>0 ? "操作成功！" : "操作失败");
    }

    @Override
    public Result deleteData(Integer ccdrId) {
        if (cityCompanyDataMapper.deleteData(ccdrId) > 0){
            return new Result().setResultCode(0);
        }
        return new Result().setResultCode(ResultCode.FAIL);
    }



    /**
     * 获取子对象长度最大值
     * @param ccdrDtos
     * @return
     */
    public Map<String,Integer> getTheMaxSize(List<CcdrDto> ccdrDtos){
        Integer theMaxSizeCcsr = 1;
        Integer theMaxSizeCcsj = 1;
        Integer theMaxSizeCcl = 1;
        for(CcdrDto c : ccdrDtos){
            theMaxSizeCcsr = c.getCcsrDtoList().size()>theMaxSizeCcsr ? c.getCcsrDtoList().size() : theMaxSizeCcsr;
            theMaxSizeCcsj = c.getCcsjDtoList().size()>theMaxSizeCcsj ? c.getCcsjDtoList().size() : theMaxSizeCcsj;
            theMaxSizeCcl = c.getCclDtoList().size()>theMaxSizeCcl ? c.getCclDtoList().size() : theMaxSizeCcl;
        }
        Map<String,Integer> maxMap = new HashMap<>();
        maxMap.put("theMaxSizeCcsr",theMaxSizeCcsr);
        maxMap.put("theMaxSizeCcsj",theMaxSizeCcsj);
        maxMap.put("theMaxSizeCcl",theMaxSizeCcl);
        return maxMap;
    }

    @Override
    public List<CcdrDto> getDataList(CcdrDto ccdrDto) {
        return cityCompanyDataMapper.queryUnionData(ccdrDto);
    }

    @Override
    public Map<String, Object> importExcelForCompanyData(MultipartFile excelFile, AccessToken token) throws IOException {
        //错误信息excel下载信息
        String errorUrl = "";
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String originalFilename = excelFile.getOriginalFilename();
        if(originalFilename==null){
            resultMap.put("returnCode", 500);
            resultMap.put("returnMsg", "请检查文件名！");
            return resultMap;
        }
        String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
        boolean contains = Arrays.asList(new String[]{"xls", "xlsx"}).contains(fileSuffix);
        if (contains == false) {
            resultMap.put("returnCode", 500);
            resultMap.put("returnMsg", "文件类型不符！");
            return resultMap;
        }
        List<ArrayList<String>> importData = null;
        try {
            importData = ExcelCompanyDataUtil.importData(excelFile);
        }catch (Exception e){
             e.printStackTrace();
                resultMap.put("returnCode", 500);
                resultMap.put("returnMsg", "导入失败，请检查您的文件！");
                return resultMap;
        }
        if (importData.isEmpty()) {
            resultMap.put("returnCode", 500);
            resultMap.put("returnMsg", "导入文件数据为空，请检查！");
            return resultMap;
        }
        if (importData.size() < 2) {
            resultMap.put("returnCode", 500);
            resultMap.put("returnMsg", "请检查导入文件是否有数据！");
            return resultMap;
        }

        //第一行标题
        String[] cellTitles = {"城市公司名称","城市公司编号","省级行政区","地级行政区域","县级行政区域","乡级行政区域","营业执照的物业公司全称","NC账套全称",
                "NC账套编码","NC人力组织编码","接管时间","接管时项目数量","接管时在管项目总建筑面积"};
        ArrayList<String> titleRow1 = importData.get(0);
        //获取股东占比数据个数
        Map<String,Integer> sizeMap = new HashMap<>();
        int begin = 0;
        int end = 0;
        int cclBegin = 0;
        int cclEnd = 0;
        for (int i = 0; i < titleRow1.size(); i++) {
            if(i<cellTitles.length){
                if (!cellTitles[i].equals(titleRow1.get(i))) {
                    resultMap.put("returnCode", 500);
                    resultMap.put("returnMsg", "请检查文件一级标题行是否正确，建议下载导入模板！");
                    return resultMap;
                }
            }else{
                if("股东占比情况".equals(titleRow1.get(i))){
                    begin=i;
                }else if("股东方对接人信息".equals(titleRow1.get(i))){
                    end=i;
                    break;
                }
            }
        }
        sizeMap.put("theMaxSizeCcsr",end-begin);
        int t1 = cellTitles.length;
        int t2 = sizeMap.get("theMaxSizeCcsr");
        int theMaxSizeCcsj = 0;
        for(int i = 0;i<titleRow1.size();i++){
            if((i+t1+t2)>=titleRow1.size()){
                break;
            }
            if("股东方对接人信息".equals(titleRow1.get(i+t1+t2))){
                theMaxSizeCcsj+=1;
            }
            if("联系方式".equals(titleRow1.get(i+t1+t2))){
                theMaxSizeCcsj+=1;
            }
        }
        if(theMaxSizeCcsj<2||theMaxSizeCcsj%2!=0){
            resultMap.put("returnCode", 500);
            resultMap.put("returnMsg", "股东方对接人信息标题有误！");
            return resultMap;
        }
        sizeMap.put("theMaxSizeCcsj",theMaxSizeCcsj);



        //第二行标题
        ArrayList<String> titleRow2 = importData.get(1);
        int t3 = sizeMap.get("theMaxSizeCcsj");
        String[] cellTitles2 = {"对赌基数","第一年","第二年","第三年","第四年","第五年"};
        for(int i = 0;i<cellTitles2.length;i++){
            if((i*4+t1+t2+t3)>=titleRow2.size()){
                break;
            }
            if (!cellTitles2[i].equals(titleRow2.get(i*4+t1+t2+t3))) {
                resultMap.put("returnCode", 500);
                resultMap.put("returnMsg", "请检查文件二级标题行是否正确！");
                return resultMap;
            }
        }
        cclBegin=t1+t2+t3+24+3;
        cclEnd=titleRow1.size();
        sizeMap.put("theMaxSizeCcl",cclEnd-cclBegin);

        //去掉title
        importData.remove(0);
        importData.remove(0);
        importData.remove(0);
        if (importData.isEmpty()) {
            resultMap.put("returnCode", 500);
            resultMap.put("returnMsg", "导入文件数据数据行为空，请检查！");
            return resultMap;
        }

        //所有数据
        List<CcdrDto> allList = new ArrayList<>();
        //错误行
        List<ArrayList<String>> errorList = new ArrayList<>();
        Map<Integer, ArrayList<String>> dataMap = new HashMap<>();

        //错误标记  如果有一条数据错误  则不导入数据
        boolean remark=true;

        for (int i = 0; i < importData.size(); i++) {
            ArrayList<String> rowDatas = importData.get(i);
            dataMap.put(i + 1, rowDatas);
        }
        if (remark) {
            for (Integer key : dataMap.keySet()) {
                CcdrDto dto=new CcdrDto();
                if(StringUtils.isNotBlank(dataMap.get(key).get(0))){
                    dto.setCityCompanyName(dataMap.get(key).get(0));
                }else{
                    resultMap.put("returnCode", 500);
                    resultMap.put("returnMsg", "城市公司数据不能为空，请检查！");
                    return resultMap;
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(1))){
                    dto.setCityCompanyCode(dataMap.get(key).get(1));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(2))){
                    dto.setProvince(dataMap.get(key).get(2));
                }else{
                    resultMap.put("returnCode", 500);
                    resultMap.put("returnMsg", "省级行政区域数据不能为空，请检查！");
                    return resultMap;
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(3))){
                    dto.setCity(dataMap.get(key).get(3));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(4))){
                    dto.setBorough(dataMap.get(key).get(4));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(5))){
                    dto.setRegion(dataMap.get(key).get(5));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(6))){
                    dto.setPropertyName(dataMap.get(key).get(6));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(7))){
                    dto.setNcSystemAccount(dataMap.get(key).get(7));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(8))){
                    dto.setNcSystemNumber(dataMap.get(key).get(8));
                }else{
                    resultMap.put("returnCode", 500);
                    resultMap.put("returnMsg", "NC账套编码不能为空，请检查！");
                    return resultMap;
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(9))){
                    dto.setNcPersonNumber(dataMap.get(key).get(9));
                }else{
                    resultMap.put("returnCode", 500);
                    resultMap.put("returnMsg", "NC人力编码不能为空，请检查！");
                    return resultMap;
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(10))){
                    dto.setManageTime(dataMap.get(key).get(10));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(11))){
                    dto.setManageProjectNumber(Integer.parseInt(dataMap.get(key).get(11)));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(12))){
                    dto.setManageProjectArea(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(12))));
                }

                List<CcsrDto> ccsrList = new ArrayList<>();
                for(int i = 0;i<sizeMap.get("theMaxSizeCcsr")/2;i++){
                    CcsrDto ccsrDto = new CcsrDto();
                    if(StringUtils.isNotBlank(dataMap.get(key).get(13+i*2))){
                        ccsrDto.setShareholderName(dataMap.get(key).get(13+i*2));
                    }
                    if(StringUtils.isNotBlank(dataMap.get(key).get(14+i*2))){
                        ccsrDto.setShareholderRatio(Float.valueOf(dataMap.get(key).get(14+i*2)));
                    }
                    if(ccsrDto.getShareholderName()!=null&&ccsrDto.getShareholderRatio()!=null){
                        ccsrList.add(ccsrDto);
                    }
                }
                dto.setCcsrDtoList(ccsrList);

                List<CcsjDto> ccsjList = new ArrayList<>();
                for(int i=0;i<sizeMap.get("theMaxSizeCcsj")/2;i++){
                    CcsjDto ccsjDto = new CcsjDto();
                    if(StringUtils.isNotBlank(dataMap.get(key).get(i*2+13+sizeMap.get("theMaxSizeCcsr")))){
                        ccsjDto.setJointName(dataMap.get(key).get(i*2+13+sizeMap.get("theMaxSizeCcsr")));
                    }
                    if(StringUtils.isNotBlank(dataMap.get(key).get(i*2+14+sizeMap.get("theMaxSizeCcsr")))){
                        ccsjDto.setJointTel(dataMap.get(key).get(i*2+14+sizeMap.get("theMaxSizeCcsr")));
                    }
                    if(ccsjDto.getJointName()!=null){
                        ccsjList.add(ccsjDto);
                    }
                }
                dto.setCcsjDtoList(ccsjList);

                List<CclDto> cclList = new ArrayList<>();
                for(int i =0;i<sizeMap.get("theMaxSizeCcl")/2;i++) {
                    CclDto cclDto = new CclDto();
                    if(StringUtils.isNotBlank(dataMap.get(key).get(i*2 + 13 + sizeMap.get("theMaxSizeCcsr") + sizeMap.get("theMaxSizeCcsj") + 24))){
                        cclDto.setLeaderName(dataMap.get(key).get(i*2 + 13 + sizeMap.get("theMaxSizeCcsr") + sizeMap.get("theMaxSizeCcsj") + 24));
                    }if(StringUtils.isNotBlank(dataMap.get(key).get(i*2 + 14 + sizeMap.get("theMaxSizeCcsr") + sizeMap.get("theMaxSizeCcsj") + 24))){
                        cclDto.setLeaderTel(dataMap.get(key).get(i*2 + 14 + sizeMap.get("theMaxSizeCcsr") + sizeMap.get("theMaxSizeCcsj") + 24));
                    }
                    if(cclDto.getLeaderName()!=null){
                        cclList.add(cclDto);
                    }
                }
                dto.setCclDtoList(cclList);

                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3))){
                    dto.setValuationChargeArea(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+1))){
                    dto.setValuationNetProfit(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+1))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+2))){
                    dto.setValuationSalesRevenue(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+2))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+3))){
                    dto.setValuationOther(dataMap.get(key).get(t1+t2+t3+3));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+4))){
                    dto.setFirstChargeArea(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+4))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+5))){
                    dto.setFirstNetPofit(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+5))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+6))){
                    dto.setFirstSalesRevenue(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+6))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+7))){
                    dto.setFirstOther(dataMap.get(key).get(t1+t2+t3+7));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+8))){
                    dto.setSecondChargeArea(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+8))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+9))){
                    dto.setSecondNetPofit(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+9))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+10))){
                    dto.setSecondSalesRevenue(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+10))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+11))){
                    dto.setSecondOther(dataMap.get(key).get(t1+t2+t3+11));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+12))){
                    dto.setThirdlyChargeArea(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+12))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+13))){
                    dto.setThirdlyNetPofit(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+13))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+14))){
                    dto.setThirdlySalesRevenue(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+14))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+15))){
                    dto.setThirdlyOther(dataMap.get(key).get(t1+t2+t3+15));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+16))){
                    dto.setFourthlyChargeArea(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+16))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+17))){
                    dto.setFourthlyNetPofit(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+17))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+18))){
                    dto.setFourthlySalesRevenue(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+18))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+19))){
                    dto.setFourthlyOther(dataMap.get(key).get(t1+t2+t3+19));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+20))){
                    dto.setFifthChargeArea(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+20))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+21))){
                    dto.setFifthNetPofit(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+21))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+22))){
                    dto.setFifthSalesRevenue(BigDecimal.valueOf(Double.parseDouble(dataMap.get(key).get(t1+t2+t3+22))));
                }
                if(StringUtils.isNotBlank(dataMap.get(key).get(t1+t2+t3+23))){
                    dto.setFifthOther(dataMap.get(key).get(t1+t2+t3+23));
                }

                allList.add(dto);
            }
            try {
                for(CcdrDto c : allList){
                    addData(c,token);
                }
            } catch (Exception e) {
                resultMap.put("returnCode", 500);
                resultMap.put("returnMsg", "模版数据导入错误！");
                return resultMap;
            }
            resultMap.put("returnCode", 0);
            resultMap.put("returnMsg", "导入" + (allList.size()) + "个成功。");
        }else {
            //标题列+最后的错误列
            String[] excelTitleError = {"城市公司名称","城市公司编号","省级行政区","地级行政区域","县级行政区域","乡级行政区域","营业执照的物业公司全称","NC账套全称",
                    "NC账套编码","NC人力组织编码","接管时间","接管时项目数量","接管时在管项目总建筑面积","错误信息"};
            String[] column = new String[]{"cityCompanyName", "cityCompanyCode", "province", "city", "borough", "region"
                    , "propertyName", "ncSystemAccount", "ncSystemNumber", "ncPersonNumber", "manageTime", "manageProjectNumber", "manageProjectArea","msg"};
            List<Map<String, String>> errorLastList = ExcelCompanyDataUtil.addErrorList(errorList, column);
            resultMap.put("returnCode", 500);
            resultMap.put("returnMsg", "导入失败！");
            resultMap.put("returnData", errorUrl);
        }
        return resultMap;
    }

    @Override
    public Result deleteCcsr(Long ccsrId) {
        if (cityCompanyDataMapper.deleteCcsr(ccsrId) > 0){
            return new Result().setResultCode(0);
        }
        return new Result().setResultCode(ResultCode.FAIL);
    }

    @Override
    public Result deleteCcsj(Long ccsjId) {
        if (cityCompanyDataMapper.deleteCcsj(ccsjId) > 0){
            return new Result().setResultCode(0);
        }
        return new Result().setResultCode(ResultCode.FAIL);
    }

    @Override
    public Result deleteCcl(Long cclId) {
        if (cityCompanyDataMapper.deleteCcl(cclId) > 0){
            return new Result().setResultCode(0);
        }
        return new Result().setResultCode(ResultCode.FAIL);
    }

    @Override
    public Result deleteCcuf(Long fileId) {
        CcufDto ccufDto = cityCompanyDataMapper.getCcufDtoListById(fileId);
        ossUtils.delFile(ccufDto.getFileRealName());
        if (cityCompanyDataMapper.deleteCcuf(fileId) > 0){
            return new Result().setResultCode(0);
        }
        return new Result().setResultCode(ResultCode.FAIL);
    }
}
