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

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.ck.CkCleanerExternaldisplayMapper;
import cn.com.countrygarden.bi.main.dao.ck.CkCleanerFieldConfigMapper;
import cn.com.countrygarden.bi.main.dao.ck.CkCleanerFileidRoleMapper;
import cn.com.countrygarden.bi.main.dao.ck.CkCleanerRecordingMapper;
import cn.com.countrygarden.bi.main.dao.ck.ksh.CkKshBasicMapper;
import cn.com.countrygarden.bi.main.dao.ck.ksh.CkKshExpandMapper;
import cn.com.countrygarden.bi.main.dao.ck.ksh.CkKshUnlockApplicationMapper;
import cn.com.countrygarden.bi.main.dao.sys.UserRolesMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbSysOrganMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbSysUserMapper;
import cn.com.countrygarden.bi.main.dto.advise.ProjectListVO;
import cn.com.countrygarden.bi.main.dto.advise.RegionListVO;
import cn.com.countrygarden.bi.main.dto.ck.*;
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.ck.*;
import cn.com.countrygarden.bi.main.model.ck.ksh.CkKshBasic;
import cn.com.countrygarden.bi.main.model.ck.ksh.CkKshExpand;
import cn.com.countrygarden.bi.main.model.ck.ksh.CkKshUnlockApplication;
import cn.com.countrygarden.bi.main.model.sys.UserRoles;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser;
import cn.com.countrygarden.bi.main.service.ck.CkKshExpandService;
import cn.com.countrygarden.bi.main.service.erp.impl.ExcelArrearageServiceImpl;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbSysUserService;
import cn.com.countrygarden.bi.main.util.DateUtil;
import cn.com.countrygarden.bi.main.util.PageDataCkCleaner;
import cn.com.countrygarden.bi.main.util.erp.StringUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Transactional(rollbackFor =Exception.class)
@Service
public class CkKshExpandServiceImpl  implements CkKshExpandService {
    @Autowired
    private UserRolesMapper userRolesMapper;
    @Autowired
    private CkCleanerFieldConfigMapper ckCleanerFieldConfigMapper;
    @Autowired
    private TbSysOrganMapper tbSysOrganMapper;
    @Autowired
    private CkKshExpandMapper ckKshExpandMapper;
    @Autowired
    private CkKshBasicMapper ckKshBasicMapper;
    @Autowired
    private TbSysUserMapper tbSysUserMapper;
    @Autowired
    private CkCleanerRecordingMapper ckCleanerRecordingMapper;
    @Autowired
    private CkCleanerExternaldisplayMapper ckCleanerExternaldisplayMapper;
    @Autowired
    private CkKshUnlockApplicationMapper ckKshUnlockApplicationMapper;

    @Autowired
    private CkCleanerFileidRoleMapper ckCleanerFileidRoleMapper;

    @Autowired
    private TbSysUserService tbSysUserService;
    private final org.slf4j.Logger logger = LoggerFactory.getLogger(ExcelArrearageServiceImpl.class);
    @Override
    public Result getDTableConfig(String role, AccessToken accessToken) {
        CkCleanerVo ckCleanerVo = new CkCleanerVo();
        ckCleanerVo.setStatus(1);
        List<String> rolesList = null;
        if (StringUtil.isEmpty(role)) {
            rolesList = queryRoleCodes(null, accessToken.getUserCode());
        } else {
            String[] roles = role.split(",");
            rolesList = Arrays.asList(roles);
        }
        ckCleanerVo.setRolesList(rolesList);
        List<CkCleanerFieldConfigVo> ckCleanerFieldConfigs = formulaConfig(ckCleanerVo, null);
        return Response.response(ckCleanerFieldConfigs);
    }

    @Override
    public Result getaddConfig(Integer basicId,String role, AccessToken accessToken) {
        CkCleanerVo ckCleanerVo = new CkCleanerVo();
        ckCleanerVo.setStatus(1);
        List<String> rolesList = null;
        if (StringUtil.isEmpty(role)) {
            rolesList = queryRoleCodes(null, accessToken.getUserCode());
        } else {
            String[] roles = role.split(",");
            rolesList = Arrays.asList(roles);
        }
        ckCleanerVo.setRolesList(rolesList);
        List<CkCleanerFieldConfigVo> ckCleanerFieldConfigs = addformulaConfig(ckCleanerVo, null);


        for (int i = 0; i <ckCleanerFieldConfigs.size() ; i++) {
            if(CollectionUtils.isNotEmpty(ckCleanerFieldConfigs.get(i).getChildren())){
                for (int j = 0; j <ckCleanerFieldConfigs.get(i).getChildren().size() ; j++) {
                    CkKshExpand ckKshExpand=new CkKshExpand();
                    ckKshExpand.setBasicId(basicId);
                    ckKshExpand.setFieldCode(ckCleanerFieldConfigs.get(i).getChildren().get(j).getFieldCode());
                    List<CkKshExpand> ckKshExpandList=ckKshExpandMapper.select(ckKshExpand);
                    if(CollectionUtils.isNotEmpty(ckKshExpandList)){
                        ckCleanerFieldConfigs.get(i).getChildren().get(j).setValue(ckKshExpandList.get(0).getValue());
                    }
                }
            }
        }

        return Response.response(ckCleanerFieldConfigs);
    }

    @Override
    public Result queryPageList(CkCleanerQueyr cleanerQueyr, AccessToken accessToken) {
        logger.info(accessToken.toString());
        System.out.println(accessToken.toString());
        List<String> rolesList = queryRoleCodes(null, accessToken.getUserCode());
        if (StringUtil.isNotBlank(cleanerQueyr.getSearchStr())) {
            Map<String, String> map = JSON.parseObject(cleanerQueyr.getSearchStr(), Map.class);
            if (map != null && map.size() > 0) {
                cleanerQueyr.setSearchMap(map);
            }
        }
        List<CkRegionVo> ckRegionVoList = null;
        List<CkCleanerTableVo> list = null;
        if (rolesList.contains("admin") || rolesList.contains("ck_admin") ||  rolesList.contains("ck_ksh_admin")) {
            if(rolesList.contains("ck_ksh_admin")){
                String code=null;
                 if(rolesList.contains("ck_ksh_project_admin")){
                     List<ProjectListVO> projectList = tbSysOrganMapper.findProject(null,accessToken.getUserCode(), null,null,null);
                     cleanerQueyr.setCkProjectRole(true);
                     cleanerQueyr.setProjectList(projectList);
                 }else if(rolesList.contains("ck_ksh_region_admin")){
                     List<RegionListVO> areaList = tbSysOrganMapper.findArea(null, accessToken.getUserCode());
                     cleanerQueyr.setCkRegionRole(true);
                     cleanerQueyr.setRegionList(areaList);
                 }
            }
        }else {
            throw new ServiceException("此账号无权限查看");
        }

     /*   LocalDate date = LocalDate.now();
        int YesValue = 0;
        int MonthValue = 0;
        if(StringUtil.isBlank(cleanerQueyr.getYear()) && StringUtil.isBlank(cleanerQueyr.getMonth())
                && StringUtil.isBlank(cleanerQueyr.getOrgCode()) && StringUtil.isBlank(cleanerQueyr.getCommCode())
                && cleanerQueyr.getIsFillin()==null && cleanerQueyr.getIsUnlocking()==null && cleanerQueyr.getIsDisplay()==null){
            if(date.getDayOfMonth()>25 || date.getDayOfMonth()<6){
                YesValue=date.getYear();
                MonthValue=date.getMonthValue();

            }else {
                if(date.getMonthValue()-1==0){
                    YesValue=date.getYear()-1;
                    MonthValue=12;
                }else {
                    YesValue=date.getYear();
                    MonthValue=date.getMonthValue()-1;
                }
            }
            cleanerQueyr.setYear(String.valueOf(YesValue));
            cleanerQueyr.setMonth(String.valueOf(MonthValue));
        }*/

        Long toble=ckKshExpandMapper.selectListDataToble(cleanerQueyr);

        Page<Object> pageList = PageHelper.startPage(cleanerQueyr.getPage(), cleanerQueyr.getSize());
        list = ckKshExpandMapper.selectListData(cleanerQueyr);

        String authority = null;
        if (rolesList.contains("ck_ksh_region_admin")) {
            authority = "ck_ksh_region_admin";
        }
        if (rolesList.contains("ck_data_admin")) {
            authority = "ck_data_admin";
        }
        if (list.size() != 0) {
            list.get(0).setAuthority(authority);
        }
        //构建空数据
        if (CollectionUtils.isEmpty(list)) {
            list = new ArrayList<>();
            CkCleanerFieldConfigVo configVo = new CkCleanerFieldConfigVo();
            CkCleanerTableVo ckCleanerTableVo = new CkCleanerTableVo();
            List<CkCleanerFieldConfigVo> configVoList = new ArrayList<>();
            configVoList.add(configVo);
            ckCleanerTableVo.setChildren(configVoList);
            ckCleanerTableVo.setAuthority(authority);
            list.add(ckCleanerTableVo);
        }
        int i = 0;
        int a = 0;

            for (CkCleanerTableVo vo : list) {
                if(vo.getIsSubmit()!=null){
                    if (vo.getIsSubmit().equals(1)) {
                        vo.setBasicIndex(i++);
                        if (CollectionUtils.isNotEmpty(vo.getChildren())) {
                            for (CkCleanerFieldConfigVo configVo : vo.getChildren()) {
                                configVo.setChildrenIndex(a++);
                            }
                        }
                    } else {
                        CkCleanerFieldConfigVo configVo = new CkCleanerFieldConfigVo();
                        List<CkCleanerFieldConfigVo> configVoList = new ArrayList<>();
                        configVoList.add(configVo);
                        vo.setChildren(configVoList);
                    }
                }
            }



        PageData data = new PageData(pageList);
        data.setData(list);
        data.setTotal(toble);
        return Response.response(data);
    }

    @Override
    public Result listTobeFilledIn(CkCleanerQueyr cleanerQueyr, AccessToken accessToken) {
        logger.info(accessToken.toString());
        System.out.println(accessToken.toString());
        List<String> rolesList = queryRoleCodes(null, accessToken.getUserCode());
        if (StringUtil.isNotBlank(cleanerQueyr.getSearchStr())) {
            Map<String, String> map = JSON.parseObject(cleanerQueyr.getSearchStr(), Map.class);
            if (map != null && map.size() > 0) {
                cleanerQueyr.setSearchMap(map);
            }
        }
        List<CkRegionVo> ckRegionVoList = null;
        List<CkCleanerTableVo> list = null;
        if (rolesList.contains("admin") || rolesList.contains("ck_ksh_region_admin") || rolesList.contains("ck_ksh_project_admin")) {
            if (rolesList.contains("ck_ksh_region_admin") || rolesList.contains("admin")) {
                String code=null;
                if(rolesList.contains("admin")){
                    code=null;
                }else {
                    code=accessToken.getUserCode();
                    List<RegionListVO> areaList = tbSysOrganMapper.findArea(null, code);
                    cleanerQueyr.setCkRegionRole(true);
                    cleanerQueyr.setRegionList(areaList);
                }
            } else {
                List<ProjectListVO> projectList = tbSysOrganMapper.findProject(null,accessToken.getUserCode(), null,null,null);
                cleanerQueyr.setCkProjectRole(true);
                cleanerQueyr.setProjectList(projectList);
            }
        }else {
            throw new ServiceException("此账号无权限查看");
        }

       /* LocalDate date = LocalDate.now();
        int YesValue = 0;
        int MonthValue = 0;
        if(StringUtil.isBlank(cleanerQueyr.getYear()) && StringUtil.isBlank(cleanerQueyr.getMonth())
        && StringUtil.isBlank(cleanerQueyr.getOrgCode()) && StringUtil.isBlank(cleanerQueyr.getCommCode())
        && cleanerQueyr.getIsFillin()==null && cleanerQueyr.getIsUnlocking()==null && cleanerQueyr.getIsDisplay()==null){
            if(date.getDayOfMonth()>25 || date.getDayOfMonth()<6){
                YesValue=date.getYear();
                MonthValue=date.getMonthValue();

            }else {
                 if(date.getMonthValue()-1==0){
                    YesValue=date.getYear()-1;
                    MonthValue=12;
                }else {
                    YesValue=date.getYear();
                    MonthValue=date.getMonthValue()-1;
                }
            }
            cleanerQueyr.setYear(String.valueOf(YesValue));
            cleanerQueyr.setMonth(String.valueOf(MonthValue));
        }*/


        Long toble=ckKshExpandMapper.selectListDataTobleFilledIn(cleanerQueyr);

        Page<Object> pageList = PageHelper.startPage(cleanerQueyr.getPage(), cleanerQueyr.getSize());
        list = ckKshExpandMapper.selectListDataTobeFilledIn(cleanerQueyr);

        String authority = null;
        if (rolesList.contains("ck_ksh_region_admin")) {
            authority = "ck_ksh_region_admin";
        }
        if (list.size() != 0) {
            list.get(0).setAuthority(authority);
        }
        //构建空数据
        if (CollectionUtils.isEmpty(list)) {
            list = new ArrayList<>();
            CkCleanerFieldConfigVo configVo = new CkCleanerFieldConfigVo();
            CkCleanerTableVo ckCleanerTableVo = new CkCleanerTableVo();
            List<CkCleanerFieldConfigVo> configVoList = new ArrayList<>();
            configVoList.add(configVo);
            ckCleanerTableVo.setChildren(configVoList);
            ckCleanerTableVo.setAuthority(authority);
            list.add(ckCleanerTableVo);
        }
        int i = 0;
        int a = 0;
        for (CkCleanerTableVo vo : list) {
            vo.setBasicIndex(i++);
            if (CollectionUtils.isNotEmpty(vo.getChildren())) {
                for (CkCleanerFieldConfigVo configVo : vo.getChildren()) {
                    configVo.setChildrenIndex(a++);
                }
            }
        }


        PageData data = new PageData(pageList);
        data.setData(list);
        data.setTotal(toble);
        return Response.response(data);
    }

    @Override
    public Result addOrEdit(Integer isSubmit,String data, AccessToken accessToken,String selectData) {
        CkCleanerTableVo vo = JSON.parseObject(data, CkCleanerTableVo.class);
//      校验入参对象是否正确
/*        int inde = validateVo(vo);
        if (inde == 1) {
            return Response.responseFail("请勿添加重复区域项目");
        }*/

        TbSysUser tbSysUser = tbSysUserMapper.findByUserCodeNew(accessToken.getUserCode());
        CkCleanerRecording recording = new CkCleanerRecording();
        CkKshBasic ckCleerBas = null;
        int count = 0;
        ckCleerBas = new CkKshBasic();
        ckCleerBas.setOrgCode(vo.getOrgCode());
        ckCleerBas.setOrgName(vo.getOrgName());
        ckCleerBas.setCommCode(StringUtil.isBlank(vo.getCommCode()) ? "empty" : vo.getCommCode());
        ckCleerBas.setCommName(vo.getCommName());
        ckCleerBas.setYear(vo.getYear());
        ckCleerBas.setMonth(vo.getMonth());
        ckCleerBas.setStatus(1);
        ckCleerBas.setCreateTime(new Date());
        ckCleerBas.setCreateBy(accessToken.getUserCode());
        ckCleerBas.setCreateName(tbSysUser.getUserName());
        ckCleerBas.setIsDisplay(0);
        if(isSubmit.equals(1)){
            ckCleerBas.setIsFillin(1);
        }else {
            ckCleerBas.setIsFillin(0);
        }
        ckCleerBas.setIsLocking(0);
        ckCleerBas.setIsUnlocking(0);
        ckCleerBas.setIsSubmit(isSubmit);
        CkKshBasic ckCleanerBasic = new CkKshBasic();
        BeanUtils.copyProperties(vo, ckCleanerBasic);
        List<CkKshBasic> basicList = new ArrayList<>();
        if (vo.getBasicId() == null) {
            basicList = ckKshBasicMapper.statisPr(ckCleerBas);
            if (basicList.size() == 0) {
                ckKshBasicMapper.insert(ckCleerBas);
                ckCleanerBasic.setId(ckCleerBas.getId());
            }
        } else {
            ckCleanerBasic.setId(vo.getBasicId());
        }
 /*       if (vo.getBasicId() != null) {
            ckCleanerBasic.setId(vo.getBasicId());
        }
        ckCleanerBasic = ckCleanerBasicMapper.inheritBasicInfo(ckCleanerBasic);
        if (ckCleanerBasic != null) {
            return Response.responseFail("保存失败：" + vo.getOrgName() + "--" + vo.getCommName() + vo.getYear() + "年" + vo.getMonth() + "月份的数据已存在，");
        }*/

        if (basicList.size() != 0) {
            ckCleanerBasic.setId(basicList.get(0).getId());
        }
        ckCleanerBasic.setCommCode(StringUtil.isBlank(ckCleanerBasic.getCommCode()) ? "" : ckCleanerBasic.getCommCode());
        ckCleanerBasic.setCommName(StringUtil.isBlank(ckCleanerBasic.getCommName()) ? "" : ckCleanerBasic.getCommName());
        ckCleanerBasic.setYear(vo.getYear());
        ckCleanerBasic.setCreateTime(new Date());
        ckCleanerBasic.setCreateBy(accessToken.getUserCode());
        ckCleanerBasic.setCreateName(tbSysUser.getUserName());
        ckCleanerBasic.setUpdateBy(accessToken.getUserCode());
        ckCleanerBasic.setUpdateName(tbSysUser.getUserName());
        ckCleanerBasic.setUpdateTime(new Date());
        ckCleanerBasic.setStatus(1);
        ckCleanerBasic.setIsSubmit(isSubmit);
        if(isSubmit.equals(1)){
            recording.setOperatingType("提交");
            ckCleanerBasic.setIsFillin(1);
        }else {
            recording.setOperatingType("编辑");
            ckCleanerBasic.setIsFillin(0);
        }
        if (selectData.equals("[]")) {
            ckCleanerBasic.setIsDisplay(0);

        } else {
            ckCleanerBasic.setIsDisplay(1);
        }
        count = ckKshBasicMapper.updateByPrimaryKeySelective(ckCleanerBasic);

        recording.setCommCode(ckCleanerBasic.getCommCode());
        recording.setCommName(ckCleanerBasic.getCommName());
        recording.setOrgCode(ckCleanerBasic.getOrgCode());
        recording.setOrgName(ckCleanerBasic.getOrgName());
        recording.setStatus(1);
        recording.setYear(ckCleanerBasic.getYear());
        recording.setMonth(ckCleanerBasic.getMonth());
        String Classification = vo.getDataClassification();
        if (CollectionUtils.isNotEmpty(vo.getChildren())) {
            CkKshExpand expand = null;
            for (CkCleanerFieldConfigVo v : vo.getChildren()) {
                expand = new CkKshExpand();
                expand.setFieldCode(v.getFieldCode());
                expand.setValue(StringUtil.isBlank(v.getValue()) ? "" : v.getValue());
                ckCleanerBasic.setUpdateName(tbSysUser.getUserName());
                expand.setUpdateBy(accessToken.getUserCode());
                expand.setUpdateTime(new Date());
                if (v.getId() == null) {
                    recording.setDataClassification(Classification);
                    expand.setBasicId(ckCleanerBasic.getId());
                    expand.setStatus(1);
                    ckCleanerBasic.setCreateName(tbSysUser.getUserName());
                    expand.setCreateBy(accessToken.getUserCode());
                    expand.setCreateTime(new Date());
                    count += ckKshExpandMapper.insertSelective(expand);
                    recording.setBasicId(ckCleanerBasic.getId());
                } else {
                    recording.setDataClassification(Classification);
                    Condition condition = new Condition(CkKshExpand.class);
                    Criteria criteria = condition.createCriteria();
                    criteria.andEqualTo("basicId", ckCleanerBasic.getId()).andEqualTo("fieldCode", expand.getFieldCode());
                    if (expand.getFieldCode().equals("followustryd")) {
                        System.out.println(expand.getFieldCode());
                    }
                    count += ckKshExpandMapper.updateByConditionSelective(expand, condition);
                    recording.setBasicId(ckCleanerBasic.getId());
                }
            }

        }
        recording.setDataClassification("填报服务可视化数据");
        recording.setOperatingName(tbSysUser.getUserName());
        recording.setOperatingBy(accessToken.getUserCode());
        recording.setOperatingTime(new Date());
        recording.setCreateTime(new Date());
        ckCleanerRecordingMapper.insert(recording);
        return Response.response(count);
    }

    @Override
    public Result addSubmitEdit(Integer basicId,Integer isSubmit, String selectData, String valueData, AccessToken accessToken) {

        addckexpand(basicId,selectData,accessToken);
        addOrEdit(isSubmit,valueData,accessToken,selectData);

        return Response.response();
    }

    @Override
    public Result del(List<Integer> ids, String dataClassification, AccessToken accessToken) {
        TbSysUser tbSysUser = tbSysUserMapper.findByUserCodeNew(accessToken.getUserCode());
        List<CkKshBasic> basicList = ckKshBasicMapper.selectByIds(ids.get(0).toString());
        CkCleanerRecording recording = new CkCleanerRecording();

        recording.setCommCode(basicList.get(0).getCommCode());
        recording.setCommName(basicList.get(0).getCommName());
        recording.setOrgCode(basicList.get(0).getOrgCode());
        recording.setOrgName(basicList.get(0).getOrgName());
        recording.setStatus(1);
        recording.setYear(basicList.get(0).getYear());
        recording.setMonth(basicList.get(0).getMonth());

        CkKshBasic ckCleanerBasic = new CkKshBasic();
        ckCleanerBasic.setStatus(0);
        ckCleanerBasic.setUpdateTime(new Date());
        ckCleanerBasic.setUpdateBy(accessToken.getUserCode());
        Condition condition = new Condition(CkKshBasic.class);
        Criteria criteria = condition.createCriteria();
        criteria.andIn("id", ids);
        int count = ckKshBasicMapper.updateByConditionSelective(ckCleanerBasic, condition);

        condition = new Condition(CkKshExpand.class);
        criteria = condition.createCriteria();
        criteria.andIn("basicId", ids);
        CkKshExpand ckCleanerExpand = new CkKshExpand();
        ckCleanerExpand.setStatus(2);
        ckCleanerExpand.setUpdateBy(accessToken.getUserCode());
        ckCleanerExpand.setUpdateTime(new Date());
        ckKshExpandMapper.updateByConditionSelective(ckCleanerExpand, condition);


        recording.setBasicId(ids.get(0));
        recording.setOperatingType("删除");
        recording.setDataClassification(dataClassification);
        recording.setOperatingName(tbSysUser.getUserName());
        recording.setOperatingBy(accessToken.getUserCode());
        recording.setOperatingTime(new Date());
        recording.setCreateTime(new Date());
        ckCleanerRecordingMapper.insert(recording);
        return Response.response(count);
    }

    @Override
    public CkCleanerTableVo inheritInfo(CkKshBasic ckCleanerBasic) {
        ckCleanerBasic.setCommCode(StringUtil.isBlank(ckCleanerBasic.getCommCode()) ? "empty" : ckCleanerBasic.getCommCode());
        CkKshBasic basic = ckKshBasicMapper.inheritBasicInfo(ckCleanerBasic);
        ;
        if (basic == null) {
            return new CkCleanerTableVo();
        }
        ckCleanerBasic = basic;
        CkCleanerQueyr cleanerQueyr = new CkCleanerQueyr();
        cleanerQueyr.setBasicId(ckCleanerBasic.getId());
        List<CkCleanerTableVo> list = ckKshExpandMapper.selectListData(cleanerQueyr);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return new CkCleanerTableVo();
    }


    @Override
    public int addckexpand(Integer basicId, String data, AccessToken accessToken) {
         /*       JSONArray jsonArray= JSON.parseArray(data);
        if(jsonArray.size()!=0) {*/
        CkCleanerExternaldisplay nExt = new CkCleanerExternaldisplay();
        if (basicId == null) {
            throw new ServiceException("参数有误，请再次确认！");
        }
        nExt.setBasicId(basicId);
        ckCleanerExternaldisplayMapper.delete(nExt);
        CkCleanerExternaldisplay ckCleanExt = new CkCleanerExternaldisplay();
        ckCleanExt.setBasicId(basicId);
        ckCleanExt.setDataConfig(data);
        ckCleanerExternaldisplayMapper.insert(ckCleanExt);

        TbSysUser tbSysUser = tbSysUserMapper.findByUserCodeNew(accessToken.getUserCode());
        List<CkKshBasic> list = ckKshBasicMapper.selectByIds(String.valueOf(basicId));

/*        CkCleanerRecording recording = new CkCleanerRecording();

        recording.setBasicId(basicId);
        recording.setCommCode(list.get(0).getCommCode());
        recording.setCommName(list.get(0).getCommName());
        recording.setOrgCode(list.get(0).getOrgCode());
        recording.setOrgName(list.get(0).getOrgName());
        recording.setStatus(1);
        recording.setYear(list.get(0).getYear());
        recording.setMonth(list.get(0).getMonth());*/

        CkKshBasic ckKshBasic=new CkKshBasic();
        ckKshBasic.setId(basicId);
        if (data.equals("[]")) {
            ckKshBasic.setIsDisplay(0);
        } else {
            ckKshBasic.setIsDisplay(1);
        }
        ckKshBasic.setUpdateTime(new Date());
        ckKshBasic.setUpdateBy(accessToken.getUserCode());
        ckKshBasic.setUpdateName(tbSysUser.getUserName());
/*        recording.setDataClassification("服务可视化对外配置");
        recording.setOperatingName(tbSysUser.getUserName());
        recording.setOperatingBy(accessToken.getUserCode());
        recording.setOperatingTime(new Date());
        recording.setCreateTime(new Date());
        ckCleanerRecordingMapper.insert(recording);*/
        ckKshBasicMapper.updateByPrimaryKisDisplay(ckKshBasic);
         /*   for (int i = 0; i < jsonArray.size(); i++) {

                JSONObject jsonObject=jsonArray.getJSONObject(i);
                ckCleanExt.setFieldCode(jsonObject.getString("fieldCode"));
                ckCleanExt.setFieldName(jsonObject.getString("fieldName"));
                ckCleanExt.setParentId(jsonObject.getInteger("parentId"));
                ckCleanExt.setType(jsonObject.getInteger("type"));
                ckCleanExt.setSoft(jsonObject.getInteger("soft"));

            }*/
        return 1;
        //}
        //return 0;
    }

    @Override
    public List<CkCleanerExternaldisplay> ckexpandkedKeys(Integer basicId) {
        CkCleanerExternaldisplay ckCleanerExtern = new CkCleanerExternaldisplay();
        ckCleanerExtern.setBasicId(basicId);
        List<CkCleanerExternaldisplay> ckexpandkedKeys = ckCleanerExternaldisplayMapper.select(ckCleanerExtern);
        return ckexpandkedKeys;
    }

    @Override
    public List<Map<String, String>> ckexpand(Integer[] commId, Integer year, Integer month) {
        List<String> fieldCodeSrt = new ArrayList<>();
        List<String> numidex = new ArrayList<>();
        List<CkCleanerExternaldisplay> fieldt = new ArrayList<>();
  /*      if (ckexpandkedKeys.size()!=0&&ckexpandkedKeys!=null){
            for (int i = 0; i <ckexpandkedKeys.size() ; i++) {
                fieldCodeSrt.add(ckexpandkedKeys.get(i).getFieldCode());
            }
        }*/
        List<String> list = new ArrayList<>();
        for (int i = 0; i < commId.length; i++) {
            list.add(commId[i].toString());
        }
        CkCleanerBasicDTO basicdto = new CkCleanerBasicDTO();
        basicdto.setCommId(list);
        basicdto.setYear(year);
        basicdto.setMonth(month);

        List<CkKshBasic> ckList = ckCleanerExternaldisplayMapper.ckList(basicdto);
        if (ckList.size() != 0 && ckList != null) {
            List<CkCleanerExternaldisplay> ckexpandkedKeys = ckCleanerExternaldisplayMapper.selectAllSort(ckList.get(0).getId());
            if (ckexpandkedKeys.size() == 0) {
                return null;
            }
            String data = ckexpandkedKeys.get(0).getDataConfig();
            if (!data.equals("[]") || StringUtils.isBlank(data)) {
                JSONArray jsonArray = JSON.parseArray(data);
                for (int i = 0; i < jsonArray.size(); i++) {
                    CkCleanerExternaldisplay ckCleanExt = new CkCleanerExternaldisplay();
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    ckCleanExt.setFieldCode(jsonObject.getString("fieldCode"));
                    ckCleanExt.setFieldName(jsonObject.getString("fieldName"));
                    ckCleanExt.setParentId(jsonObject.getInteger("parentId"));
                    ckCleanExt.setType(jsonObject.getInteger("type"));
                    ckCleanExt.setSoft(jsonObject.getInteger("sort"));
                    fieldt.add(ckCleanExt);
                    numidex.add(String.valueOf(jsonObject.getInteger("sort")));
                    fieldCodeSrt.add(jsonObject.getString("fieldCode"));
                }
            } else {
                return null;
            }
        }


        List<CkCleanerTableVo> cleanerBasicList = ckCleanerExternaldisplayMapper.cleanerBasicList(basicdto);
        List<CkCleanerFieldConfigVo> CleanerFieldCof = ckCleanerExternaldisplayMapper.CleanerFieldCof(fieldCodeSrt, basicdto, null);
        for (int i = 0; i < CleanerFieldCof.size(); i++) {
            for (int j = 0; j < fieldt.size(); j++) {
                if (fieldt.get(j).getFieldCode().equals(CleanerFieldCof.get(i).getFieldCode())) {
                    CleanerFieldCof.get(i).setSoft(fieldt.get(j).getSoft());
                }
            }
        }
        Collections.sort(CleanerFieldCof);
        List<Map<String, String>> mapList = new ArrayList<>();
        for (int i = 0; i < cleanerBasicList.size(); i++) {
            Map<String, String> map = new LinkedHashMap();
            map.put("orgCode", cleanerBasicList.get(i).getOrgCode());
            map.put("orgName", cleanerBasicList.get(i).getOrgName());
            map.put("commCode", cleanerBasicList.get(i).getCommCode());
            map.put("commName", cleanerBasicList.get(i).getCommName());
            for (int j = 0; j < CleanerFieldCof.size(); j++) {
                if (cleanerBasicList.get(i).getId().equals(CleanerFieldCof.get(j).getBasicId())) {
                    if(StringUtils.isNotBlank(CleanerFieldCof.get(j).getValue())){
                        map.put(CleanerFieldCof.get(j).getFieldCode(), CleanerFieldCof.get(j).getValue());
                    }else {
                        map.put(CleanerFieldCof.get(j).getFieldCode(), "0");
                    }

                }
            }
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public Result allData(Integer year, Integer month, int page, int size, String updateDate) {
        SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // HH表示24小时制；
        List<CkCleanerDataConfigDTO> dataConList = new ArrayList<>();

        Page<Object> pageList = PageHelper.startPage(page, size);
        List<CkCleanerTableVo> allBasic= ckKshExpandMapper.allBasic(year, month, updateDate);

        List<Map<String, String>> mapList = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(allBasic)) {
            for (int i = 0; i < allBasic.size(); i++) {
                CkCleanerTableVo tableVo = allBasic.get(i);
//              组装主数据
                CkCleanerBasicDTO basicdto = new CkCleanerBasicDTO();
                basicdto.setYear(year);
                basicdto.setMonth(month);
                basicdto.setBasicId(tableVo.getBasicId());
//              组装可视化配置中的字段
                List<CkCleanerFieldConfigVo> CleanerFieldCof = null;
                CleanerFieldCof=ckCleanerExternaldisplayMapper.CleanerFieldCof2(basicdto, null);
                if(CollUtil.isEmpty(CleanerFieldCof)){
                    continue;
                }
//              组装可视化数据设置
                CkCleanerDataConfigDTO configDTO = new CkCleanerDataConfigDTO();
                configDTO.setBasicId(tableVo.getBasicId());
                configDTO.setOrgCode(tableVo.getOrgCode());
                configDTO.setOrgName(tableVo.getOrgName());
                configDTO.setCommCode(tableVo.getCommCode());
                configDTO.setCommName(tableVo.getCommName());
                configDTO.setYear(tableVo.getYear());
                configDTO.setMonth(tableVo.getMonth());
                configDTO.setUpdateTime(tableVo.getUpdateTime());
                dataConList.add(configDTO);


                //              组装返回值
                Map<String, String> map = new LinkedHashMap();
                map.put("orgCode", tableVo.getOrgCode());
                map.put("orgName", tableVo.getOrgName());
                map.put("commCode", tableVo.getCommCode());
                map.put("commName", tableVo.getCommName());
                map.put("year", tableVo.getYear());
                map.put("month", tableVo.getMonth());
                map.put("updateDate", dFormat.format(tableVo.getUpdateTime()));
                mapList.add(map);
//              组装排序
                JSONArray array = JSON.parseArray(tableVo.getDataConfig());
                for (int b = 0; b < CleanerFieldCof.size(); b++) {
                    CleanerFieldCof.get(b).setSoft(findSoft(array,CleanerFieldCof.get(b).getFieldCode()));
                    if(StringUtils.isNotBlank(CleanerFieldCof.get(b).getValue())){
                        map.put(CleanerFieldCof.get(b).getFieldCode(), CleanerFieldCof.get(b).getValue());
                    }else {
                        map.put(CleanerFieldCof.get(b).getFieldCode(), "0");
                    }

                }
                Collections.sort(CleanerFieldCof);
                configDTO.setDataConfig(mapList);
            }
        }
        // Page<Object> pageList = PageHelper.startPage(page, size);
        // List<Map<String, String>> mapList1 = new ArrayList<>(mapList);

        PageDataCkCleaner data = new PageDataCkCleaner(pageList);
        data.setData(mapList);
        if(mapList.size()==0){
            data.setTotal(0L);
        }
        // data.setTotal(Long.valueOf(String.valueOf(mapList.size())));
        return Response.response(data);
    }

    @Override
    public Result statisticsProject(Integer year, Integer month, String orgCode, AccessToken accessToken) {
        List<String> rolesList = queryRoleCodes(null, accessToken.getUserCode());
         boolean ckRegionRole=false;
         boolean ckProjectRole=false;
         List<ProjectListVO> projectList2=new ArrayList<>();
         List<RegionListVO> regionList=new ArrayList<>();
        if (rolesList.contains("admin") || rolesList.contains("ck_admin") ||  rolesList.contains("ck_ksh_admin") || rolesList.contains("ck_ksh_region_admin")) {
            //if ( rolesList.contains("admin") || rolesList.contains("ck_admin")) {
                String code=null;
                if(rolesList.contains("admin") || rolesList.contains("ck_admin")){
                    code=null;
                }else {
                   code=accessToken.getUserCode();
                    ckRegionRole=true;
                    List<RegionListVO> areaList = tbSysOrganMapper.findArea(null, code);
                    regionList.addAll(areaList);
                }
           // } else {
            //    List<ProjectListVO> projectList = tbSysOrganMapper.findProject(null,accessToken.getUserCode(), null,null,null);
            //    ckProjectRole=true;
             //   projectList2.containsAll(projectList);
            //}
        }else {
            throw new ServiceException("此账号无权限查看");
        }
        LocalDate date = LocalDate.now();
        int YesValue = 0;
        int MonthValue = 0;
 /*       if (date.getMonthValue() - 1 == 0) {
            YesValue = date.getYear() - 1;
            MonthValue = 12;
        } else {
            YesValue = date.getYear();
            MonthValue = date.getMonthValue() - 1;
        }*/
        if (month != null && year != null) {
            YesValue = year;
            MonthValue = month;
        }else {

            if(date.getDayOfMonth()>25  ){
                YesValue=date.getYear();
                MonthValue=date.getMonthValue();

            }else if(date.getDayOfMonth()<6){
                YesValue=date.getYear();
                if(date.getMonthValue()-1==0){
                    YesValue=date.getYear()-1;
                    MonthValue=12;
                }else {
                    YesValue=date.getYear();
                    MonthValue=date.getMonthValue()-1;
                }
            } else {
                if(date.getMonthValue()-1==0){
                    YesValue=date.getYear()-1;
                    MonthValue=12;
                }else {
                    YesValue=date.getYear();
                    MonthValue=date.getMonthValue()-1;
                }
            }
        }
        String nianyue=null;
        if(MonthValue<=9){
            nianyue=YesValue+"-"+"0"+MonthValue+"-25";
        }else {
            nianyue=YesValue+"-"+MonthValue+"-25";
        }

        int pojNum = ckKshExpandMapper.tatisticsProject(YesValue, MonthValue, orgCode,ckRegionRole,ckProjectRole,regionList,projectList2);
        int commNum = ckKshExpandMapper.ckCleanerCont(YesValue, MonthValue, orgCode,ckRegionRole,ckProjectRole,regionList,projectList2,null);
        //int commNumErp=tbSysOrganMapper.findProjectBasicInfoCount();

        int nocommNum = 0;
         nocommNum = commNum - pojNum;
         if(nocommNum<0){
             nocommNum =pojNum-commNum;
         }
        return Response.response(MonthValue + "月已录入项目" + pojNum + "个，未录入项目" + commNum + "个，项目总数"+(pojNum+commNum));
    }

    @Override
    public Result statisProjDetad(Integer year, Integer month, String orgCode, String type, int page, int size,String nianyueStr,AccessToken accessToken) throws ParseException {

        List<String> rolesList = queryRoleCodes(null, accessToken.getUserCode());
        boolean ckRegionRole=false;
        boolean ckProjectRole=false;
        List<ProjectListVO> projectList2=new ArrayList<>();
        List<RegionListVO> regionList=new ArrayList<>();
        SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (rolesList.contains("admin") || rolesList.contains("ck_admin") ||  rolesList.contains("ck_ksh_admin") || rolesList.contains("ck_ksh_region_admin")) {
            //if (rolesList.contains("admin") || rolesList.contains("ck_admin")) {
                String code=null;
                if(rolesList.contains("admin") || rolesList.contains("ck_admin")){
                    code=null;
                }else {
                    code=accessToken.getUserCode();
                    ckRegionRole=true;
                }
                List<RegionListVO> areaList = tbSysOrganMapper.findArea(null, code);
                regionList.addAll(areaList);
            //} else {
               // List<ProjectListVO> projectList = tbSysOrganMapper.findProject(null,accessToken.getUserCode(), null,null,null);
             //   ckProjectRole=true;
             //   projectList2.containsAll(projectList);
           // }
        }else {
            throw new ServiceException("此账号无权限查看");
        }

        LocalDate date = LocalDate.now();
        int YesValue = 0;
        int MonthValue = 0;

        if (month != null && year != null) {
            YesValue = year;
            MonthValue = month;
        }else {
            if(date.getDayOfMonth()>25 || date.getDayOfMonth()<6){
                YesValue=date.getYear();
                MonthValue=date.getMonthValue();

            }else {
                if(date.getMonthValue()-1==0){
                    YesValue=date.getYear()-1;
                    MonthValue=12;
                }else {
                    YesValue=date.getYear();
                    MonthValue=date.getMonthValue()-1;
                }
            }
        }
        List<String> listComm=new ArrayList<>();
        CkRegionalDataDTO ckRegionalDataDTO=new CkRegionalDataDTO();
        String nianyue=null;
        if(MonthValue<=9){
            nianyue=YesValue+"-"+"0"+MonthValue+"-25";
        }else {
            nianyue=YesValue+"-"+MonthValue+"-25";
        }
        if (type.equals("已录入")) {

            String startTime=DateUtil.getMonthStart(YesValue,MonthValue);
            String endTime=DateUtil.getMonthEnd(YesValue,MonthValue);

            Integer statisProjDetadCount = ckKshExpandMapper.statisProjDetadCount(YesValue, MonthValue, orgCode,ckRegionRole,ckProjectRole,regionList,projectList2);
            Integer statisProjDetadNotCount = ckKshBasicMapper.statisProjDetadNotCount(YesValue, MonthValue, orgCode,ckRegionRole,ckProjectRole,regionList,projectList2,null);
            Integer findProjectBasicInfoSelectCount = tbSysOrganMapper.findProjectBasicInfoSelectCount(startTime,endTime,ckRegionRole,regionList,orgCode);
            List<CkCleanerTableVo3> listQuwaComto = ckKshBasicMapper.statisProjDetQuyuWahu(YesValue, MonthValue, orgCode,null,ckRegionRole,regionList);


            Page<Object> pageList = PageHelper.startPage(page, size);
            List<CkCleanerTableVo> list = ckKshExpandMapper.statisProjDetad(YesValue, MonthValue, orgCode,ckRegionRole,regionList);

           for (int i = 0; i < list.size(); i++) {
               TbSysUser user=tbSysUserMapper.findByUserCodeNew(list.get(i).getCreateBy());
               if(user!=null){
                   list.get(i).setMobileTel(user.getMobileTel());
               }
                //listComm.add(list.get(i).getCommCode());
            }
            ckRegionalDataDTO.setList(list);
            ckRegionalDataDTO.setNewMonthProjNum(findProjectBasicInfoSelectCount);
            ckRegionalDataDTO.setEnteredItemsPojNum(statisProjDetadCount);
            ckRegionalDataDTO.setEnteredPojNum(statisProjDetadNotCount);
            ckRegionalDataDTO.setProjectsNum(statisProjDetadCount+statisProjDetadNotCount);
            ckRegionalDataDTO.setListquyutongNum(listQuwaComto.size());

            PageData data = new PageData(pageList);
            data.setTotal(Long.valueOf(statisProjDetadCount));
            data.setData(ckRegionalDataDTO);
            return Response.response(data);
        } else if (type.equals("未录入")) {

            String startTime=DateUtil.getMonthStart(YesValue,MonthValue);
            String endTime=DateUtil.getMonthEnd(YesValue,MonthValue);


            Integer statisProjDetadCount = ckKshExpandMapper.statisProjDetadCount(YesValue, MonthValue, orgCode,ckRegionRole,ckProjectRole,regionList,projectList2);
            Integer statisProjDetadNotCount = ckKshBasicMapper.statisProjDetadNotCount(YesValue, MonthValue, orgCode,ckRegionRole,ckProjectRole,regionList,projectList2,null);
            Integer findProjectBasicInfoSelectCount = tbSysOrganMapper.findProjectBasicInfoSelectCount(startTime,endTime,ckRegionRole,regionList,orgCode);
            List<CkCleanerTableVo3> listQuwaComto = ckKshBasicMapper.statisProjDetQuyuWahu(YesValue, MonthValue, orgCode,null,ckRegionRole,regionList);



            Page<Object> pageList = PageHelper.startPage(page, size);
            List<CkCleanerTableVo2> listNot = ckKshBasicMapper.statisProjDetadNot(YesValue, MonthValue, orgCode,null,ckRegionRole,regionList);

           // List<ProjectListVO> projectList = tbSysOrganMapper.findProjectBasicInfoSelect(listComm);
            for (int i = 0; i < listNot.size(); i++) {
                listNot.get(i).setCreateName("无");
                listNot.get(i).setMobileTel("无");
            }

            ckRegionalDataDTO.setListNot(listNot);
            ckRegionalDataDTO.setNewMonthProjNum(findProjectBasicInfoSelectCount);
            ckRegionalDataDTO.setEnteredItemsPojNum(statisProjDetadCount);
            ckRegionalDataDTO.setEnteredPojNum(statisProjDetadNotCount);
            ckRegionalDataDTO.setProjectsNum(statisProjDetadCount+statisProjDetadNotCount);
            ckRegionalDataDTO.setListquyutongNum(listQuwaComto.size());

            PageData data = new PageData(pageList);
            data.setTotal(Long.valueOf(statisProjDetadNotCount));
            data.setData(ckRegionalDataDTO);
            return Response.response(data);
        }else if(type.equals("本月新增项目")){
            Calendar cal=Calendar.getInstance();
            /*if (month != null && year != null) {
                startTime=getMonthStart(year,month);
                endTime=getMonthEnd(year,month);
            }else {
                cal.add(Calendar.MONTH, 0);
                cal.set(Calendar.DAY_OF_MONTH, 1);
                Date time=cal.getTime();
                startTime=new SimpleDateFormat("yyyy-MM-dd").format(time)+" 00:00:00";

                cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                Date time1=cal.getTime();
                endTime=new SimpleDateFormat("yyyy-MM-dd").format(time1)+" 23:59:59";
            }*/
            String startTime= DateUtil.getMonthStart(YesValue,MonthValue);
            String endTime=DateUtil.getMonthEnd(YesValue,MonthValue);



            String[] tiset=startTime.split("-");

            Integer statisProjDetadCount = ckKshExpandMapper.statisProjDetadCount(YesValue, MonthValue, orgCode,ckRegionRole,ckProjectRole,regionList,projectList2);
            Integer statisProjDetadNotCount = ckKshBasicMapper.statisProjDetadNotCount(YesValue, MonthValue, orgCode,ckRegionRole,ckProjectRole,regionList,projectList2,null);
            Integer findProjectBasicInfoSelectCount = tbSysOrganMapper.findProjectBasicInfoSelectCount(startTime,endTime,ckRegionRole,regionList,orgCode);
            List<CkCleanerTableVo3> listQuwaComto = ckKshBasicMapper.statisProjDetQuyuWahu(YesValue, MonthValue, orgCode,null,ckRegionRole,regionList);


            Page<Object> pageList = PageHelper.startPage(page, size);
            List<ProjectListVO> projectList = tbSysOrganMapper.findProjectBasicInfoSelect(startTime,endTime,ckRegionRole,regionList,orgCode);
            for (int i = 0; i < projectList.size(); i++) {
                projectList.get(i).setCreateName("无");
                projectList.get(i).setYear(String.valueOf(tiset[0]));
                projectList.get(i).setMonth(String.valueOf(tiset[1]));
                projectList.get(i).setMobileTel("无");
                if(StringUtil.isNotBlank(projectList.get(i).getActualEnterTime())){
                    projectList.get(i).setActualEnterTime(projectList.get(i).getActualEnterTime().substring(0,10));
                }
                projectList.get(i).setContractProperty(projectList.get(i).getContractProperty().substring(0,10));
            }

            ckRegionalDataDTO.setProjectList(projectList);
            ckRegionalDataDTO.setNewMonthProjNum(findProjectBasicInfoSelectCount);
            ckRegionalDataDTO.setEnteredItemsPojNum(statisProjDetadCount);
            ckRegionalDataDTO.setEnteredPojNum(statisProjDetadNotCount);
            ckRegionalDataDTO.setProjectsNum(statisProjDetadCount+statisProjDetadNotCount);
            ckRegionalDataDTO.setListquyutongNum(listQuwaComto.size());
            PageData data = new PageData(pageList);
            data.setTotal(Long.valueOf(findProjectBasicInfoSelectCount));
            data.setData(ckRegionalDataDTO);
            return Response.response(data);
        }else if(type.equals("区域完成情况统计表")){

            String startTime= DateUtil.getMonthStart(YesValue,MonthValue);
            String endTime=DateUtil.getMonthEnd(YesValue,MonthValue);


            Integer statisProjDetadCount = ckKshExpandMapper.statisProjDetadCount(YesValue, MonthValue, orgCode,ckRegionRole,ckProjectRole,regionList,projectList2);
            Integer statisProjDetadNotCount = ckKshBasicMapper.statisProjDetadNotCount(YesValue, MonthValue, orgCode,ckRegionRole,ckProjectRole,regionList,projectList2,null);
            Integer findProjectBasicInfoSelectCount = tbSysOrganMapper.findProjectBasicInfoSelectCount(startTime,endTime,ckRegionRole,regionList,orgCode);


            List<CkCleanerTableVo3> listQuwaComto = ckKshBasicMapper.statisProjDetQuyuWahu(YesValue, MonthValue, orgCode,null,ckRegionRole,regionList);
            List<ProjectListVO> projectList = tbSysOrganMapper.findProjectBasicInfoCommIdNum(startTime,endTime,ckRegionRole,regionList,orgCode);

            Page<Object> pageList = PageHelper.startPage(page, size);
            List<CkCleanerTableVo3> listQuwa = ckKshBasicMapper.statisProjDetQuyuWahu(YesValue, MonthValue, orgCode,null,ckRegionRole,regionList);

            NumberFormat nf = NumberFormat.getNumberInstance();
            nf.setMaximumFractionDigits(2);
            for (int i = 0; i <listQuwa.size() ; i++) {
                for (int j = 0; j <projectList.size() ; j++) {
                     if(listQuwa.get(i).getOrgCode().equals(projectList.get(j).getOrganCode())){
                         System.out.println(listQuwa.get(i).getOrgCode()+"  "+listQuwa.get(i).getWeilu()+"   "+projectList.get(j).getCommIdNum());
                         listQuwa.get(i).setMaintained(listQuwa.get(i).getYilu()+listQuwa.get(i).getWeilu()-projectList.get(j).getCommIdNum());
                         System.out.println(listQuwa.get(i).getMaintained());
                         if(listQuwa.get(i).getMaintained()!=0){
                             listQuwa.get(i).setComplete(nf.format( Double.valueOf(listQuwa.get(i).getYilu())/Double.valueOf(listQuwa.get(i).getMaintained())*100)+"%");
                         }else{
                             listQuwa.get(i).setComplete("0%");
                         }
                     }
                }
            }

            for (int i = 0; i <listQuwa.size() ; i++) {
                if (StringUtil.isBlank(listQuwa.get(i).getComplete())) {
                    listQuwa.get(i).setMaintained(listQuwa.get(i).getYilu() + listQuwa.get(i).getWeilu());
                    if (listQuwa.get(i).getMaintained() != 0) {
                        listQuwa.get(i).setComplete(nf.format(Double.valueOf(listQuwa.get(i).getYilu()) / Double.valueOf(listQuwa.get(i).getMaintained()) * 100) + "%");
                    } else {
                        listQuwa.get(i).setComplete("0%");
                    }
                }
            }
                ckRegionalDataDTO.setListquyutong(listQuwa);
                ckRegionalDataDTO.setNewMonthProjNum(findProjectBasicInfoSelectCount);
                ckRegionalDataDTO.setEnteredItemsPojNum(statisProjDetadCount);
                ckRegionalDataDTO.setEnteredPojNum(statisProjDetadNotCount);
                ckRegionalDataDTO.setProjectsNum(statisProjDetadCount + statisProjDetadNotCount);
                ckRegionalDataDTO.setListquyutongNum(listQuwaComto.size());
                PageData data = new PageData(pageList);
                data.setTotal(Long.valueOf(listQuwaComto.size()));
                data.setData(ckRegionalDataDTO);
                return Response.response(data);

        }
        return Response.response();
    }

    /**
     * 获取某年某月开始日期
     * @return String
     * **/
    public static String getMonthStart(int year,int month){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month-1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 00:00:00";
    }

    /**
     * 获取某年某月最后一天
     * @return String
     * **/
    public static String getMonthEnd(int year,int month){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month-1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 23:59:59";
    }

    @Override
    public List<CkRegionFiledValueVo> getRegionData(CkCleanerQueyr cleanerQueyr, AccessToken accessToken) {
        cleanerQueyr.setPage(1);
        cleanerQueyr.setSize(Integer.MAX_VALUE);
        List<CkCleanerTableVo> list = ckKshExpandMapper.selectListDataNoChild(cleanerQueyr);
        List<String> baseIds = new ArrayList<>();
        for (CkCleanerTableVo t : list) {
            baseIds.add(t.getId() + "");
            t.setChildren(new ArrayList<>());
        }
        if(CollectionUtils.isEmpty(baseIds)){
            return null;
        }
//      区域数据分组统计
        List<CkFiledValueVo> configVos = ckKshExpandMapper.selectRegionByBaseIds(baseIds);
        Map<String, List<CkFiledValueVo>> regionData = new HashMap<>();
        for (int i = 0; i < configVos.size(); i++) {
            CkFiledValueVo configVo = configVos.get(i);
            List<CkFiledValueVo> valueVos = regionData.get(configVo.getOrgCode());
            if (CollUtil.isEmpty(valueVos)) {
                valueVos = new ArrayList<>();
            }
            valueVos.add(configVo);
            regionData.put(configVo.getOrgCode(), valueVos);
        }
        List<CkRegionFiledValueVo> ckRegionFiledValueVos = new ArrayList<>();
        for (String region : regionData.keySet()) {
            List<CkFiledValueVo> filedValueVoList = regionData.get(region);
            CkRegionFiledValueVo valueVo = new CkRegionFiledValueVo();
            valueVo.setBasicId(filedValueVoList.get(0).getBasicId());
            valueVo.setOrgCode(filedValueVoList.get(0).getOrgCode());
            valueVo.setOrgName(filedValueVoList.get(0).getOrgName());
            valueVo.setChildren(filedValueVoList);
            ckRegionFiledValueVos.add(valueVo);
        }
        return ckRegionFiledValueVos;
    }

    @Override
    public List<RegionListVO> getRegionDate(AccessToken accessToken) {
        logger.info(accessToken.toString());
        System.out.println(accessToken.toString());
        List<String> rolesList = queryRoleCodes(null, accessToken.getUserCode());
        List<RegionListVO> areaList = null;
        List<CkRegionVo> ckRegionVoList = new ArrayList<>();
        CkRegionVo ckRegionVo = null;
        List<ProjectListVO> projectList = null;
        List<ProjectListVO> projects = null;
        if (rolesList.contains("admin") || rolesList.contains("ck_admin")  || rolesList.contains("ck_ksh_region_admin") || rolesList.contains("ck_ksh_project_admin")){
            String userCode=null;
            if (rolesList.contains("admin") || rolesList.contains("ck_admin")) {
                userCode=null;
            }else {
                userCode=accessToken.getUserCode();
            }
            areaList = tbSysOrganMapper.findAreaBasicInfo(null,null,userCode);
        }
        List<RegionListVO> are=new ArrayList<>();
        if(CollectionUtils.isNotEmpty(areaList)){
            for (int i = 0; i < areaList.size(); i++) {
                RegionListVO reg=new RegionListVO();
                if(areaList.get(i).getOrganName().indexOf("区域")!=-1){
                    reg=areaList.get(i);
                    are.add(reg);
                }
            }
        }
        return are;
    }

    @Override
    public List<ProjectListVO> getPojDate(AccessToken accessToken, String organCode) {
        logger.info(accessToken.toString());
        System.out.println(accessToken.toString());
        String userCode=null;
        List<String> rolesList = queryRoleCodes(null, accessToken.getUserCode());
        if(rolesList.contains("ck_ksh_project_admin")){
            userCode=accessToken.getUserCode();
        }
        List<ProjectListVO> projectList = tbSysOrganMapper.findProjectBasicInfo(organCode,userCode,null,null,null);

        return projectList;
    }

    @Override
    public Result submitByid(String basicIds, AccessToken accessToken) {

        CkKshExpand ckKshEx=new CkKshExpand();
        ckKshEx.setBasicId(Integer.valueOf(basicIds));
        List<CkKshExpand> list=ckKshExpandMapper.select(ckKshEx);
        for (int i = 0; i <list.size() ; i++) {
            if(StringUtil.isBlank(list.get(i).getValue())){
                throw new ServiceException("请添加完善数据再提交！！");
            }
        }

        CkKshBasic ckCleerBas = new CkKshBasic();
        ckCleerBas.setId(Integer.valueOf(basicIds));
        ckCleerBas.setIsSubmit(1);
        ckCleerBas.setIsFillin(1);
        ckKshBasicMapper.updateByPrimaryKeySelective(ckCleerBas);

        TbSysUser tbSysUser = tbSysUserMapper.findByUserCodeNew(accessToken.getUserCode());

        CkCleanerRecording recording = new CkCleanerRecording();


        List<CkKshBasic> ckKshBasicList=ckKshBasicMapper.selectByIds(String.valueOf(basicIds));

        recording.setCommCode(ckKshBasicList.get(0).getCommCode());
        recording.setCommName(ckKshBasicList.get(0).getCommName());
        recording.setOrgCode(ckKshBasicList.get(0).getOrgCode());
        recording.setOrgName(ckKshBasicList.get(0).getOrgName());
        recording.setStatus(1);
        recording.setYear(ckKshBasicList.get(0).getYear());
        recording.setMonth(ckKshBasicList.get(0).getMonth());
        recording.setBasicId(Integer.valueOf(basicIds));
        recording.setOperatingType("提交");
        recording.setDataClassification("填报服务可视化数据");
        recording.setOperatingName(tbSysUser.getUserName());
        recording.setOperatingBy(accessToken.getUserCode());
        recording.setOperatingTime(new Date());
        recording.setCreateTime(new Date());
        ckCleanerRecordingMapper.insert(recording);
        return Response.response();
    }

    @Override
    public Result applyUnlock(CkKshUnlockApplication unlockApplication, AccessToken accessToken) {

        List<CkKshBasic> ckKshBasics=ckKshBasicMapper.selectByIds(unlockApplication.getBasicId().toString());
        TbSysUser user=tbSysUserMapper.findByUserCodeNew(accessToken.getUserCode());
        unlockApplication.setInformation("申请人信息:区域："+ckKshBasics.get(0).getOrgName()+
                "，项目："+ckKshBasics.get(0).getCommName()+"，录入人："+user.getUserName()+
                "，申请解锁"+ckKshBasics.get(0).getYear()+"年"+ckKshBasics.get(0).getMonth()+"月的数据，是否确认解锁？");
        unlockApplication.setCreateName(user.getUserName());
        unlockApplication.setCreateBy(accessToken.getUserCode());
        unlockApplication.setCreateTime(new Date());
        ckKshUnlockApplicationMapper.insert(unlockApplication);

        CkKshBasic ckCleerBas = new CkKshBasic();
        ckCleerBas.setId(unlockApplication.getBasicId());
        ckCleerBas.setIsUnlocking(1);
        ckKshBasicMapper.updateByPrimaryKeySelective(ckCleerBas);

        return Response.response();
    }

    @Override
    public void updateConttPrrty() throws ParseException {
        CkKshBasic ckCleerBas = new CkKshBasic();
        ckCleerBas.setStatus(1);
        List<CkKshBasic> ckKshBasicList=ckKshBasicMapper.select(ckCleerBas);
        for (int i = 0; i <ckKshBasicList.size() ; i++) {
            List<ProjectListVO> projectList = tbSysOrganMapper.findCommPro(null,null,ckKshBasicList.get(i).getCommCode(),null);
             if(CollectionUtils.isNotEmpty(projectList)){
                 if(StringUtil.isNotBlank(projectList.get(0).getContractProperty())){
                     CkKshBasic ckClee = new CkKshBasic();
                     ckClee.setId(ckKshBasicList.get(i).getId());
                     ckClee.setContractProperty(projectList.get(0).getContractProperty().substring(0,10));
                     ckKshBasicMapper.updateByPrimaryKeySelective(ckClee);
                 }
             }
        }

    }

    @Override
    public Result applyUnlockInformation(Integer basicId, AccessToken accessToken) {
        CkKshUnlockApplication application=new CkKshUnlockApplication();
        application.setBasicId(basicId);
        List<CkKshUnlockApplication> applicationList=ckKshUnlockApplicationMapper.selectCkKshUnlockApplica(application);
        if(CollectionUtils.isNotEmpty(applicationList)){
            return Response.response(applicationList.get(0));
        }else {
            return Response.response();
        }

    }

    @Override
    public Result unlocking(Integer basicId, AccessToken accessToken) {
        TbSysUser tbSysUser = tbSysUserMapper.findByUserCodeNew(accessToken.getUserCode());
        CkKshBasic ckClee = new CkKshBasic();
        ckClee.setId(basicId);
        ckClee.setIsLocking(0);
        ckKshBasicMapper.updateByPrimaryKeySelective(ckClee);


        List<CkKshBasic> ckKshBasicList=ckKshBasicMapper.selectByIds(String.valueOf(basicId));
        CkCleanerRecording recording = new CkCleanerRecording();

        recording.setCommCode(ckKshBasicList.get(0).getCommCode());
        recording.setCommName(ckKshBasicList.get(0).getCommName());
        recording.setOrgCode(ckKshBasicList.get(0).getOrgCode());
        recording.setOrgName(ckKshBasicList.get(0).getOrgName());
        recording.setStatus(1);
        recording.setYear(ckKshBasicList.get(0).getYear());
        recording.setMonth(ckKshBasicList.get(0).getMonth());

        recording.setBasicId(basicId);
        recording.setOperatingType("解锁");
        recording.setDataClassification("服务可视化数据管理");
        recording.setOperatingName(tbSysUser.getUserName());
        recording.setOperatingBy(accessToken.getUserCode());
        recording.setOperatingTime(new Date());
        recording.setCreateTime(new Date());
        ckCleanerRecordingMapper.insert(recording);
        return Response.response();
    }

    @Override
    public void updateadd() throws ParseException {


        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;

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

        List<ProjectListVO> projectList = tbSysOrganMapper.findProjectBasicInfoAdddsq(null);


        List<ProjectListVO> prtLinew=new ArrayList<>();

        for (int i = 0; i <projectList.size() ; i++) {
            if(projectList.get(i).getOrganName().indexOf("区域")!=-1){
                prtLinew.add(projectList.get(i));
            }
        }

        List<ProjectListVO> prtLi=new ArrayList<>();
        for (int w = 0; w <prtLinew.size(); w++) {
            ProjectListVO proj=new ProjectListVO();

            if(StringUtil.isBlank(prtLinew.get(w).getActualEnterTime()) ||
                    StringUtil.isBlank(prtLinew.get(w).getActualEnterTime())){
                proj=prtLinew.get(w);
            }else {
                Date dateCp = simpleDateFormat.parse(prtLinew.get(w).getContractProperty());
                Date dateAe = simpleDateFormat.parse(prtLinew.get(w).getActualEnterTime());

                long tsCp = dateCp.getTime();
                long tsAe = dateAe.getTime();
                String actuTime=prtLinew.get(w).getActualEnterTime();
                String[] srig=actuTime.split("-");
                Integer nian = Integer.valueOf(srig[0]);
                Integer yue=Integer.valueOf(srig[1]);
                if(nian.equals(year)){
                    if(yue.equals(month)){
                        if(tsAe<tsCp){
                            proj=prtLinew.get(w);
                        }
                    }else {
                        proj=prtLinew.get(w);
                    }
                }else {
                    proj=prtLinew.get(w);
                }
            }

            prtLi.add(proj);
        }

        CkCleanerFileidRole ckCleanerFileidRole=new CkCleanerFileidRole();
        ckCleanerFileidRole.setRoleCode("ck_ksh_admin");
        ckCleanerFileidRole.setStatus(1);
        List<CkCleanerFileidRole> ckCleanerFileidRoles=ckCleanerFileidRoleMapper.select(ckCleanerFileidRole);
       // for (int a = 1; a <=10; a++) {
            for (int i = 0; i <prtLi.size() ; i++) {
                CkKshBasic ckCleerBas = new CkKshBasic();
                ckCleerBas.setOrgCode(prtLi.get(i).getOrganCode());
                ckCleerBas.setOrgName(prtLi.get(i).getOrganName());
                ckCleerBas.setCommCode(prtLi.get(i).getCommId());
                ckCleerBas.setCommName(prtLi.get(i).getCommName());
                ckCleerBas.setYear(String.valueOf(2021));
                ckCleerBas.setMonth(String.valueOf(11));
                ckCleerBas.setStatus(1);
                List<CkKshBasic> ckKshBasicslist=ckKshBasicMapper.select(ckCleerBas);
                if(org.apache.commons.collections.CollectionUtils.isEmpty(ckKshBasicslist)){
                    ckCleerBas.setIsDisplay(0);
                    if(StringUtil.isNotBlank(prtLi.get(i).getActualEnterTime())){
                        ckCleerBas.setActualEnterTime(prtLi.get(i).getActualEnterTime().substring(0,10));
                    }
                    ckCleerBas.setContractProperty(prtLi.get(i).getContractProperty().substring(0,10));
                    ckCleerBas.setIsFillin(0);
                    ckCleerBas.setIsLocking(0);
                    ckCleerBas.setIsUnlocking(0);
                    ckCleerBas.setIsSubmit(0);
                    ckCleerBas.setCreateTime(new Date());
                    int index=ckKshBasicMapper.insert(ckCleerBas);
                    if(index>0){
                        for (int j = 0; j <ckCleanerFileidRoles.size() ; j++) {
                            CkKshExpand ckKshExpand=new CkKshExpand();
                            ckKshExpand.setStatus(1);
                            ckKshExpand.setBasicId(ckCleerBas.getId());
                            ckKshExpand.setFieldCode(ckCleanerFileidRoles.get(j).getFieldCode());
                            ckKshExpand.setCreateTime(new Date());
                            ckKshExpand.setValue("");
                            ckKshExpandMapper.insert(ckKshExpand);
                        }
                    }
                }
            }
            CkKshBasic ckCleerBas = new CkKshBasic();
            ckCleerBas.setIsLocking(0);
            ckKshBasicMapper.updateByPrimaryLocking(ckCleerBas);
        //}

    }

    @Override
    public Result statisticsProjecttishi(AccessToken accessToken) {
        boolean ckRegionRole=false;
        boolean ckProjectRole=false;
        List<ProjectListVO> projectList2=new ArrayList<>();
        List<RegionListVO> regionList=new ArrayList<>();

        LocalDate date = LocalDate.now();
        int YesValue = 0;
        int MonthValue = 0;
 /*       if (date.getMonthValue() - 1 == 0) {
            YesValue = date.getYear() - 1;
            MonthValue = 12;
        } else {
            YesValue = date.getYear();
            MonthValue = date.getMonthValue() - 1;
        }*/
            if(date.getMonthValue()<25){
                if(date.getMonthValue()-1==0){
                    YesValue=date.getYear()-1;
                    MonthValue=12;
                }else {
                    YesValue=date.getYear();
                    MonthValue=date.getMonthValue()-1;
                }
            }else {
                YesValue = date.getYear();
                MonthValue = date.getMonthValue();
            }

       /*   String nianyue=null;
      if(MonthValue<=9){
            nianyue=YesValue+"-"+"0"+MonthValue+"-25";
        }else {
            nianyue=YesValue+"-"+MonthValue+"-25";
        }*/

       // int pojNum = ckKshExpandMapper.tatisticsProject(YesValue, MonthValue, null,ckRegionRole,ckProjectRole,regionList,projectList2);
        int commNum = ckKshExpandMapper.ckCleanerCont(YesValue, MonthValue, null,ckRegionRole,ckProjectRole,regionList,projectList2,null);
        //int commNumErp=tbSysOrganMapper.findProjectBasicInfoCount();

       /* int nocommNum = 0;
        nocommNum = commNum - pojNum;
        if(nocommNum<0){
            nocommNum =pojNum-commNum;
        }*/
        return Response.response(commNum);

    }

    @Override
    public void roleadd() {
        UserRoles userRoles=new UserRoles();
        userRoles.setRoleCode("ck_ksh_admin");
        List<UserRoles> rolesList=userRolesMapper.select(userRoles);
        List<String> userCodes = new ArrayList<String>();
        for(UserRoles role : rolesList){
            userCodes.add(role.getUserCode());
        }

        List<TbSysUser> users= tbSysUserService.findByUserCodes(userCodes);
        for (int i = 0; i < users.size(); i++) {
            UserRoles usoles=new UserRoles();
            if(users.get(i).getDepName().contains("区域")){
                //ck_ksh_region_admin
                usoles.setRoleCode("ck_ksh_region_admin");
                usoles.setUserCode(users.get(i).getUserCode());
                usoles.setCreatedDate(new Date());
            }else {
                usoles.setRoleCode("ck_ksh_project_admin");
                usoles.setUserCode(users.get(i).getUserCode());
                usoles.setCreatedDate(new Date());
            }
            userRolesMapper.insert(usoles);
        }
    }

    @Override
    public void xiufuadd() {
        CkCleanerFileidRole ckCleanerFileidRole=new CkCleanerFileidRole();
        ckCleanerFileidRole.setRoleCode("ck_ksh_admin");
        ckCleanerFileidRole.setStatus(1);
        List<CkCleanerFileidRole> ckCleanerFileidRoles=ckCleanerFileidRoleMapper.select(ckCleanerFileidRole);
        for (int a = 1; a <=5 ; a++) {
            List<CkKshBasic> ckKshBasicslist=ckKshBasicMapper.selectadd(a);
            for (int i = 0; i <ckKshBasicslist.size() ; i++) {
                for (int j = 0; j <ckCleanerFileidRoles.size() ; j++) {
                    CkKshExpand ckKshExpand=new CkKshExpand();
                    ckKshExpand.setStatus(1);
                    ckKshExpand.setBasicId(ckKshBasicslist.get(i).getId());
                    ckKshExpand.setFieldCode(ckCleanerFileidRoles.get(j).getFieldCode());
                    ckKshExpand.setCreateTime(new Date());
                    ckKshExpand.setValue("");
                    ckKshExpandMapper.insert(ckKshExpand);
                }
            }
        }

    }

    @Override
    public void updateEnterTime() {

        CkKshBasic kshBasic=new CkKshBasic();
        kshBasic.setStatus(1);
        List<CkKshBasic> kshB=ckKshBasicMapper.select(kshBasic);

        for(int i=0;i<kshB.size();i++){
            List<ProjectListVO> projectList = tbSysOrganMapper.findProjectBasicInfoAdddsq(kshB.get(i).getCommCode());
            if(CollectionUtils.isNotEmpty(projectList)){
                if(StringUtil.isNotBlank(projectList.get(0).getActualEnterTime())){
                    CkKshBasic kshBc=new CkKshBasic();
                    kshBc.setId(kshB.get(i).getId());
                    kshBc.setActualEnterTime(projectList.get(0).getActualEnterTime().substring(0,10));
                    ckKshBasicMapper.updateByPrimaryKeySelective(kshBc);
                }
            }
        }
    }

    @Override
    public void updateaddXiugai() throws ParseException {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;

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

        List<ProjectListVO> projectList = tbSysOrganMapper.findProjectBasicInfoAdddsq(null);


        List<ProjectListVO> prtLinew=new ArrayList<>();

        for (int i = 0; i <projectList.size() ; i++) {
            if(projectList.get(i).getOrganName().indexOf("区域")!=-1){
                prtLinew.add(projectList.get(i));
            }
        }

        List<ProjectListVO> prtLi=new ArrayList<>();
        for (int w = 0; w <prtLinew.size(); w++) {
            ProjectListVO proj=new ProjectListVO();

            if(StringUtil.isBlank(prtLinew.get(w).getActualEnterTime()) ||
                    StringUtil.isBlank(prtLinew.get(w).getActualEnterTime())){
                proj=prtLinew.get(w);
            }else {
                Date dateCp = simpleDateFormat.parse(prtLinew.get(w).getContractProperty());
                Date dateAe = simpleDateFormat.parse(prtLinew.get(w).getActualEnterTime());

                long tsCp = dateCp.getTime();
                long tsAe = dateAe.getTime();
                String actuTime=prtLinew.get(w).getActualEnterTime();
                String[] srig=actuTime.split("-");
                Integer nian = Integer.valueOf(srig[0]);
                Integer yue=Integer.valueOf(srig[1]);
                if(nian.equals(year)){
                    if(yue.equals(month)){
                        if(tsAe<tsCp){
                            proj=prtLinew.get(w);
                        }
                    }else {
                        proj=prtLinew.get(w);
                    }
                }else {
                    proj=prtLinew.get(w);
                }
            }

            prtLi.add(proj);
        }

        CkCleanerFileidRole ckCleanerFileidRole=new CkCleanerFileidRole();
        ckCleanerFileidRole.setRoleCode("ck_ksh_admin");
        ckCleanerFileidRole.setStatus(1);
        List<CkCleanerFileidRole> ckCleanerFileidRoles=ckCleanerFileidRoleMapper.select(ckCleanerFileidRole);
        for (int i = 0; i <prtLi.size() ; i++) {
            CkKshBasic ckCleerBas = new CkKshBasic();
            ckCleerBas.setOrgCode(prtLi.get(i).getOrganCode());
            ckCleerBas.setOrgName(prtLi.get(i).getOrganName());
            ckCleerBas.setCommCode(prtLi.get(i).getCommId());
            ckCleerBas.setCommName(prtLi.get(i).getCommName());
            ckCleerBas.setYear(String.valueOf(2021));
            ckCleerBas.setMonth(String.valueOf(11));
            ckCleerBas.setStatus(1);
            List<CkKshBasic> ckKshBasicslist=ckKshBasicMapper.select(ckCleerBas);
            if(org.apache.commons.collections.CollectionUtils.isEmpty(ckKshBasicslist)){
                ckCleerBas.setIsDisplay(0);
                if(StringUtil.isNotBlank(prtLi.get(i).getActualEnterTime())){
                    ckCleerBas.setActualEnterTime(prtLi.get(i).getActualEnterTime().substring(0,10));
                }
                ckCleerBas.setContractProperty(prtLi.get(i).getContractProperty().substring(0,10));
                ckCleerBas.setIsFillin(0);
                ckCleerBas.setIsLocking(1);
                ckCleerBas.setIsUnlocking(0);
                ckCleerBas.setIsSubmit(0);
                ckCleerBas.setCreateTime(new Date());
                int index=ckKshBasicMapper.insert(ckCleerBas);
                if(index>0){
                    for (int j = 0; j <ckCleanerFileidRoles.size() ; j++) {
                        CkKshExpand ckKshExpand=new CkKshExpand();
                        ckKshExpand.setStatus(1);
                        ckKshExpand.setBasicId(ckCleerBas.getId());
                        ckKshExpand.setFieldCode(ckCleanerFileidRoles.get(j).getFieldCode());
                        ckKshExpand.setCreateTime(new Date());
                        ckKshExpand.setValue("");
                        ckKshExpandMapper.insert(ckKshExpand);
                    }
                }
            }
        }
    }

    private int findSoft(JSONArray array, String fieldCode) {
        if(CollUtil.isEmpty(array)){
            return 0;
        }
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = array.getJSONObject(i);
            if(object.getString("fieldCode").equals(fieldCode)){
                return object.getInteger("sort");
            }
        }
        return 0;
    }

    public List<String> queryRoleCodes(String[] roles, String userCode) {
        //保洁模块的权限问题：admin=ck_admin>区域>项目>其他
        Condition condition = new Condition(UserRoles.class);
        condition.selectProperties("roleCode");
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("userCode", userCode);
        if (roles != null && roles.length > 0) {
            criteria.andIn("roleCode", Arrays.asList(roles));
        }

        List<UserRoles> rolesList = userRolesMapper.selectByCondition(condition);
        if (CollectionUtils.isNotEmpty(rolesList)) {
            return rolesList.stream().map(e -> e.getRoleCode()).collect(Collectors.toList());
        }
        // 权限不足
        throw new ServiceException("权限不足");
    }

    public List<CkCleanerFieldConfigVo> addformulaConfig(CkCleanerVo query, List<CkCleanerFieldConfigVo> list) {
        CkCleanerVo ckCleanerVo = null;
        if (CollectionUtils.isEmpty(list)) {
            ckCleanerVo = new CkCleanerVo();
            BeanUtils.copyProperties(query, ckCleanerVo);
            ckCleanerVo.setParentId(query.getParentId() == null ? 0 : query.getParentId());
            list = ckCleanerFieldConfigMapper.selectListaddData(ckCleanerVo);
      /*      for (int i = 0; i <list.size() ; i++) {
                if(list.get(i).getFieldName().equals("月度运营数据") || list.get(i).getFieldName().equals("便民服务")
                || list.get(i).getFieldName().equals("收费管理面积（单位：㎡） （不含地库）")){
                    list.remove(i);
                }
            }*/
        }
        for (CkCleanerFieldConfigVo config : list) {
            ckCleanerVo = new CkCleanerVo();
            BeanUtils.copyProperties(query, ckCleanerVo);
            ckCleanerVo.setParentId(config.getId());
            List<CkCleanerFieldConfigVo> childList = ckCleanerFieldConfigMapper.selectListData(ckCleanerVo);
            if (CollectionUtils.isNotEmpty(childList)) {
                config.setChildren(childList);
                formulaConfig(query, childList);
            } else {
                continue;
            }
        }
        return list;
    }


    public List<CkCleanerFieldConfigVo> formulaConfig(CkCleanerVo query, List<CkCleanerFieldConfigVo> list) {
        CkCleanerVo ckCleanerVo = null;
        if (CollectionUtils.isEmpty(list)) {
            ckCleanerVo = new CkCleanerVo();
            BeanUtils.copyProperties(query, ckCleanerVo);
            ckCleanerVo.setParentId(query.getParentId() == null ? 0 : query.getParentId());
            list = ckCleanerFieldConfigMapper.selectListData(ckCleanerVo);
        }
        for (CkCleanerFieldConfigVo config : list) {
            ckCleanerVo = new CkCleanerVo();
            BeanUtils.copyProperties(query, ckCleanerVo);
            ckCleanerVo.setParentId(config.getId());
            List<CkCleanerFieldConfigVo> childList = ckCleanerFieldConfigMapper.selectListData(ckCleanerVo);
            if (CollectionUtils.isNotEmpty(childList)) {
                config.setChildren(childList);
                formulaConfig(query, childList);
            } else {
                continue;
            }
        }
        return list;
    }

    private int validateVo(CkCleanerTableVo vo) {
        CkKshBasic ceerBas = new CkKshBasic();
        ceerBas.setOrgCode(vo.getOrgCode());
        ceerBas.setCommCode(vo.getCommCode());
        ceerBas.setYear(vo.getYear());
        ceerBas.setMonth(vo.getMonth());
        ceerBas.setStatus(1);
        //编辑状态下排除本身
        if (vo.getBasicId() != null) {
            ceerBas.setId(vo.getBasicId());
        }
        List<CkKshBasic> basList = ckKshBasicMapper.selectBasic(ceerBas);
        if (basList.size() > 0) {
            return 1;
        }
        return 0;
    }
}
