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.CkPublicBenefitsMapper;
import cn.com.countrygarden.bi.main.dao.ck.CkPublicBenefitsZationMapper;
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.dto.advise.ProjectListVO;
import cn.com.countrygarden.bi.main.dto.advise.RegionListVO;
import cn.com.countrygarden.bi.main.dto.ck.CkPublicBenefitsZationVo;
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.CkPublicBenefits;
import cn.com.countrygarden.bi.main.model.ck.CkPublicBenefitsZation;
import cn.com.countrygarden.bi.main.model.sys.UserRoles;
import cn.com.countrygarden.bi.main.service.ck.CkPublicBenefitsZationService;
import cn.hutool.core.util.NumberUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
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 tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

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

/**
 * <p>
 *   公共收益初始化数据表业务处理
 * </p>
 *
 * @author 吕伟峰
 * @date 2021-05-07
 * @version V1.0
 */
@Transactional(rollbackFor =Exception.class)
@Service
public class CkPublicBenefitsZationServiceImpl implements CkPublicBenefitsZationService {
    @Autowired
    private CkPublicBenefitsZationMapper ckPublicBenefitsZationMapper;

    @Autowired
    private CkPublicBenefitsMapper ckPublicBenefitsMapper;

    @Autowired
    private TbSysOrganMapper tbSysOrganMapper;

    @Autowired
    private UserRolesMapper userRolesMapper;

    @Override
    public Result one(CkPublicBenefitsZation ckPublicBenefits, AccessToken accessToken) {
        ckPublicBenefits.setStatus(1);
        CkPublicBenefitsZation ckPunefits = ckPublicBenefitsZationMapper.selectOne(ckPublicBenefits);
        return Response.response(ckPunefits);
    }

    @Override
    public Result addOrEdit(CkPublicBenefitsZation ckPublicBenefits, AccessToken accessToken) throws ParseException {
        String[] yeas = ckPublicBenefits.getBookTime().split("-");
        CkPublicBenefitsZation ck = new CkPublicBenefitsZation();
        ck.setStatus(1);
        ck.setOrgCode(ckPublicBenefits.getOrgCode());
        ck.setCommCode(ckPublicBenefits.getCommCode());
        CkPublicBenefitsZation ckPunefits = ckPublicBenefitsZationMapper.selectOne(ck);
        if (ckPunefits != null) {
            throw new ServiceException("数据不能重复添加！！！");
           /* int indexNum = ckPublicBenefitsZationMapper.deleteByIds(String.valueOf(ckPunefits.getId()));
            if (indexNum > 0) {
                ckPublicBenefits.setYear(Integer.valueOf(yeas[0]));
                ckPublicBenefits.setStatus(1);
                ckPublicBenefits.setCreateBy(accessToken.getUserCode());
                ckPublicBenefits.setUpdateBy(accessToken.getUserCode());
                ckPublicBenefits.setCreateTime(new Date());
                ckPublicBenefits.setUpdateTime(new Date());
                int index = ckPublicBenefitsZationMapper.insert(ckPublicBenefits);
                if (index > 0) {
                    add(ckPublicBenefits,accessToken);
                }
            }else {
                ckPublicBenefits.setYear(Integer.valueOf(yeas[0]));
                ckPublicBenefits.setStatus(1);
                ckPublicBenefits.setCreateBy(accessToken.getUserCode());
                ckPublicBenefits.setUpdateBy(accessToken.getUserCode());
                ckPublicBenefits.setCreateTime(new Date());
                ckPublicBenefits.setUpdateTime(new Date());
                int index = ckPublicBenefitsZationMapper.insert(ckPublicBenefits);
                if (index > 0) {
                    add(ckPublicBenefits,accessToken);
                }
            }*/
        }else {
            ckPublicBenefits.setYear(Integer.valueOf(yeas[0]));
            ckPublicBenefits.setStatus(1);
            ckPublicBenefits.setCreateBy(accessToken.getUserCode());
            ckPublicBenefits.setUpdateBy(accessToken.getUserCode());
            ckPublicBenefits.setCreateTime(new Date());
            ckPublicBenefits.setUpdateTime(new Date());
            int index = ckPublicBenefitsZationMapper.insert(ckPublicBenefits);
            if (index > 0) {
                add(ckPublicBenefits,accessToken);
            }
        }
        return Response.response();
    }

        private void add(CkPublicBenefitsZation ckPublicBenefits,AccessToken accessToken) throws ParseException {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Calendar a = Calendar.getInstance();
            List<CkPublicBenefits> addlist=new ArrayList<>();
            String[] tiyue=ckPublicBenefits.getBookTime().split("-");

            Integer year = a.get(Calendar.YEAR);

            if(year.equals(Integer.valueOf(tiyue[0])) || Integer.valueOf(tiyue[0])>year){
                for (int j = Integer.valueOf(tiyue[1]); j <=12 ; j++) {
                    CkPublicBenefits publicBenefits=new CkPublicBenefits();
                    publicBenefits.setOrgCode(ckPublicBenefits.getOrgCode());
                    publicBenefits.setOrgName(ckPublicBenefits.getOrgName());
                    publicBenefits.setCommCode(ckPublicBenefits.getCommCode());
                    publicBenefits.setCommName(ckPublicBenefits.getCommName());
                    publicBenefits.setYear(Integer.valueOf(tiyue[0]));
                    publicBenefits.setMonth(j);
                    publicBenefits.setStatus(1);

                    CkPublicBenefits benefits=ckPublicBenefitsMapper.selectOne(publicBenefits);
                    if(benefits!=null){
                        continue;
                    }
                    publicBenefits.setIsAuto(1);
                    a.setTime(sdf.parse(publicBenefits.getYear()+"-"+publicBenefits.getMonth()));
                    publicBenefits.setYearday(LocalDate.of(Integer.valueOf(publicBenefits.getYear()),1,1).lengthOfYear());
                    publicBenefits.setMonthday(a.getActualMaximum(Calendar.DAY_OF_MONTH));
                    publicBenefits.setUct(ckPublicBenefits.getUct());
                    publicBenefits.setEdutax(ckPublicBenefits.getEdutax());
                    publicBenefits.setLedtax(ckPublicBenefits.getLedtax());
                    publicBenefits.setOther(ckPublicBenefits.getOther());
                    publicBenefits.setBacirate(ckPublicBenefits.getBacirate());
                    publicBenefits.setStampduty(ckPublicBenefits.getStampduty());
                    publicBenefits.setCreateBy(accessToken.getUserCode());
                    publicBenefits.setCreateTime(new Date());
                    publicBenefits.setUpdateTime(new Date());
                    publicBenefits.setUpdateBy(accessToken.getUserCode());
                    publicBenefits.setCreateName(ckPublicBenefits.getCreateName());
                    publicBenefits.setUpdateName(ckPublicBenefits.getUpdateName());
                    addlist.add(publicBenefits);
                }
                if(addlist.size()!=0){
                    ckPublicBenefitsMapper.insertList(addlist);
                }
            }else {
                Integer yeaindexc=year-Integer.valueOf(tiyue[0]);
                for (int i = 0; i <= yeaindexc; i++) {
                    List<CkPublicBenefits> addlist2=new ArrayList<>();
                    int index2=0;
                    if(i==0){
                        index2=Integer.valueOf(tiyue[1]);
                    }else {
                        index2=1;
                    }
                    for (int j = index2; j <=12 ; j++) {
                        CkPublicBenefits publicBenefits=new CkPublicBenefits();
                        publicBenefits.setOrgCode(ckPublicBenefits.getOrgCode());
                        publicBenefits.setOrgName(ckPublicBenefits.getOrgName());
                        publicBenefits.setCommCode(ckPublicBenefits.getCommCode());
                        publicBenefits.setCommName(ckPublicBenefits.getCommName());
                        publicBenefits.setYear(Integer.valueOf(tiyue[0])+i);
                        publicBenefits.setMonth(j);
                        publicBenefits.setStatus(1);

                        CkPublicBenefits benefits=ckPublicBenefitsMapper.selectOne(publicBenefits);
                        if(benefits!=null){
                            continue;
                        }
                        publicBenefits.setIsAuto(1);
                        a.setTime(sdf.parse(publicBenefits.getYear()+"-"+publicBenefits.getMonth()));
                        publicBenefits.setYearday(LocalDate.of(Integer.valueOf(publicBenefits.getYear()),1,1).lengthOfYear());
                        publicBenefits.setMonthday(a.getActualMaximum(Calendar.DAY_OF_MONTH));
                        publicBenefits.setUct(ckPublicBenefits.getUct());
                        publicBenefits.setEdutax(ckPublicBenefits.getEdutax());
                        publicBenefits.setLedtax(ckPublicBenefits.getLedtax());
                        publicBenefits.setOther(ckPublicBenefits.getOther());
                        publicBenefits.setBacirate(ckPublicBenefits.getBacirate());
                        publicBenefits.setStampduty(ckPublicBenefits.getStampduty());
                        publicBenefits.setCreateBy(accessToken.getUserCode());
                        publicBenefits.setCreateTime(new Date());
                        publicBenefits.setUpdateTime(new Date());
                        publicBenefits.setUpdateBy(accessToken.getUserCode());
                        publicBenefits.setCreateName(ckPublicBenefits.getCreateName());
                        publicBenefits.setUpdateName(ckPublicBenefits.getUpdateName());
                        addlist2.add(publicBenefits);
                    }
                    if(addlist2.size()!=0){
                        ckPublicBenefitsMapper.insertList(addlist2);
                    }
                }
            }
        }


    @Override
    public Result oneZationAd(CkPublicBenefitsZation ckPublicBenefits, AccessToken accessToken) {
        ckPublicBenefits.setStatus(1);
        CkPublicBenefitsZation ckPunefits=ckPublicBenefitsZationMapper.selectOne(ckPublicBenefits);
        return Response.response(ckPunefits);
    }

    @Override
    public Result oneExhibition(CkPublicBenefitsZation ckPublicBenefits, AccessToken accessToken) {
        ckPublicBenefits.setStatus(1);
        ckPublicBenefits.setBookTime(null);
        List<CkPublicBenefitsZation> ckPunefits=ckPublicBenefitsZationMapper.selectCher(ckPublicBenefits);
        DecimalFormat df = new DecimalFormat("#,###.00");
        CkPublicBenefitsZationVo ckPublicBe=new CkPublicBenefitsZationVo();
        if(CollectionUtils.isNotEmpty(ckPunefits)){
            ckPublicBe.setOrgCode(ckPunefits.get(0).getOrgCode());
            ckPublicBe.setOrgName(ckPunefits.get(0).getOrgName());
            ckPublicBe.setCommCode(ckPunefits.get(0).getCommCode());
            ckPublicBe.setCommName(ckPunefits.get(0).getCommName());
            String[] timdh=ckPunefits.get(0).getBookTime().split("-");
            ckPublicBe.setBookTime(timdh[0]+"年"+timdh[1]+"月");
            if(ckPunefits.get(0).getAobamount()!=0){
                if(Double.valueOf(ckPunefits.get(0).getAobamount())<1){
                    ckPublicBe.setAobamount(ckPunefits.get(0).getAobamount().toString());
                }else {
                    ckPublicBe.setAobamount(df.format(ckPunefits.get(0).getAobamount()));
                }
            }else {
                ckPublicBe.setAobamount(ckPunefits.get(0).getAobamount().toString());
            }
           if(ckPunefits.get(0).getPayablebut()!=0){
               if(Double.valueOf(ckPunefits.get(0).getPayablebut())<1){
                   ckPublicBe.setPayablebut(ckPunefits.get(0).getPayablebut().toString());
               }else {
                   ckPublicBe.setPayablebut(df.format(ckPunefits.get(0).getPayablebut()));
               }
           }else {
               ckPublicBe.setPayablebut(ckPunefits.get(0).getPayablebut().toString());
           }
            if(ckPunefits.get(0).getUncollected()!=0){
                if(Double.valueOf(ckPunefits.get(0).getUncollected())<1){
                    ckPublicBe.setUncollected(ckPunefits.get(0).getUncollected().toString());
                }else {
                    ckPublicBe.setUncollected(df.format(ckPunefits.get(0).getUncollected()));
                }
            }else {
                ckPublicBe.setUncollected(ckPunefits.get(0).getUncollected().toString());
            }
            return Response.response(ckPublicBe);
        }else {
            ckPublicBe.setOrgCode(ckPublicBenefits.getOrgCode());
            ckPublicBe.setOrgName(ckPublicBenefits.getOrgName());
            ckPublicBe.setCommCode(ckPublicBenefits.getCommCode());
            ckPublicBe.setCommName(ckPublicBenefits.getCommName());
            return Response.response(ckPublicBe);
        }
    }

    @Override
    public Result queryPageList(CkPublicBenefitsZation ckPublicBenefitsZation, int size, int page, AccessToken accessToken) {
        DecimalFormat df = new DecimalFormat("#,###.00");

        List<String> organCodeString=new ArrayList<>();
        List<String> commIdString=new ArrayList<>();
        List<String> rolesList = queryRoleCodes(null, accessToken.getUserCode());
         List<String> comtr=new ArrayList<>();
        List<CkPublicBenefitsZationVo> zationVoList=new ArrayList<>();
        if (rolesList.contains("ck_public_benefits_region") || rolesList.contains("ck_public_benefits_admin") || rolesList.contains("admin")){
            if(rolesList.contains("ck_public_benefits_region")){
                List<RegionListVO> areaList = tbSysOrganMapper.findArea(null, accessToken.getUserCode());
                for (int i = 0; i < areaList.size(); i++) {
                    organCodeString.add(areaList.get(i).getOrganCode());
                    List<ProjectListVO> projectList = tbSysOrganMapper.findProject(areaList.get(i).getOrganCode(),accessToken.getUserCode(),null,null,null);
                    for (int j = 0; j <projectList.size() ; j++) {
                        commIdString.add(projectList.get(j).getCommId());
                    }
                }
            }
        }else {
            throw new ServiceException("此账号没有权限查看");
        }

        Page<Object> pageList = PageHelper.startPage(page, size);
        List<CkPublicBenefitsZationVo> list=ckPublicBenefitsZationMapper.queryPageList(ckPublicBenefitsZation,organCodeString,commIdString);
        if(CollectionUtils.isNotEmpty(list)){
            for (int i = 0; i < list.size(); i++) {
                comtr.add(list.get(i).getCommCode());
                if(StringUtils.isNotBlank(list.get(i).getAobamount())){
                    if(!list.get(i).getAobamount().equals("0")){
                        if(Double.valueOf(list.get(i).getAobamount())<1){
                            list.get(i).setAobamount(list.get(i).getAobamount());
                        }else {
                            list.get(i).setAobamount(df.format(Double.valueOf(list.get(i).getAobamount())));
                        }
                    }
                }
                if(StringUtils.isNotBlank(list.get(i).getUncollected())){
                    if(!list.get(i).getUncollected().equals("0")){
                        if(Double.valueOf(list.get(i).getUncollected())<1){
                            list.get(i).setUncollected(list.get(i).getUncollected());
                        }else {
                            list.get(i).setUncollected(df.format(Double.valueOf(list.get(i).getUncollected())));
                        }
                    }
                }
              if (StringUtils.isNotBlank(list.get(i).getPayablebut())){
                  if(!list.get(i).getPayablebut().equals("0")){
                      if(Double.valueOf(list.get(i).getPayablebut())<1){
                          list.get(i).setPayablebut(list.get(i).getPayablebut());
                      }else {
                          list.get(i).setPayablebut(df.format(Double.valueOf(list.get(i).getPayablebut())));
                      }
                  }
              }
            }
            List<ProjectListVO> projectListVOS=tbSysOrganMapper.findProjectBenefitsZation(ckPublicBenefitsZation.getOrgCode(),ckPublicBenefitsZation.getCommCode(),comtr);
            for (int j = 0; j < projectListVOS.size(); j++) {
                CkPublicBenefitsZationVo publicBenefitsZationVo=new CkPublicBenefitsZationVo();
                publicBenefitsZationVo.setOrgCode(projectListVOS.get(j).getOrganCode());
                publicBenefitsZationVo.setOrgName(projectListVOS.get(j).getOrganName());
                publicBenefitsZationVo.setCommCode(projectListVOS.get(j).getCommId());
                publicBenefitsZationVo.setCommName(projectListVOS.get(j).getCommName());
                zationVoList.add(publicBenefitsZationVo);
            }
            list.addAll(zationVoList);

            List<CkPublicBenefitsZationVo> list2= startPage(list, page, size);
            int Pages = pageCountPage(list, page, size);
            PageData data = new PageData(pageList);
            data.setData(list2);
            data.setPages(Pages);
            return Response.response(data);
        }else {
                 List<ProjectListVO> projectListVOS = tbSysOrganMapper.findProjectBenefitsZation(ckPublicBenefitsZation.getOrgCode(), ckPublicBenefitsZation.getCommCode(), comtr);
                 Page<Object> pageListVo = PageHelper.startPage(page, size);
                 for (int i = 0; i < projectListVOS.size(); i++) {
                     CkPublicBenefitsZationVo publicBenefitsZationVo = new CkPublicBenefitsZationVo();
                     publicBenefitsZationVo.setOrgCode(projectListVOS.get(i).getOrganCode());
                     publicBenefitsZationVo.setOrgName(projectListVOS.get(i).getOrganName());
                     publicBenefitsZationVo.setCommCode(projectListVOS.get(i).getCommId());
                     publicBenefitsZationVo.setCommName(projectListVOS.get(i).getCommName());
                     zationVoList.add(publicBenefitsZationVo);
                 }
                 List<CkPublicBenefitsZationVo> zationVoList2 = startPage(zationVoList, page, size);
                 int Pages = pageCountPage(zationVoList, page, size);
                 PageData dataVo = new PageData(pageListVo);
                 dataVo.setData(zationVoList2);
                 dataVo.setPages(Pages);
                 return Response.response(dataVo);
        }
    }

    public static List<CkPublicBenefitsZationVo>  startPage(List<CkPublicBenefitsZationVo>  list, Integer pageNum,
                                 Integer pageSize) {
        if (list == null) {
            return null;
        }
        if (list.size() == 0) {
            return null;
        }

        Integer count = list.size(); // 记录总数
        Integer pageCount = 0; // 页数
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }

        int fromIndex = 0; // 开始索引
        int toIndex = 0; // 结束索引

        if (pageNum != pageCount) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }

        List pageList = list.subList(fromIndex, toIndex);
        return pageList;
    }

    public static int pageCountPage(List<CkPublicBenefitsZationVo>  list, Integer pageNum,
                                                            Integer pageSize) {
        if (list == null) {
            return 0;
        }
        if (list.size() == 0) {
            return 0;
        }

        Integer count = list.size(); // 记录总数
        Integer pageCount = 0; // 页数
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }

        int fromIndex = 0; // 开始索引
        int toIndex = 0; // 结束索引

        if (pageNum != pageCount) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }
        return pageCount;
    }


    @Override
    public Result addZation(CkPublicBenefitsZation ckPublicBenefitsZation, AccessToken accessToken) throws ParseException {

        List<CkPublicBenefitsZation> list=ckPublicBenefitsZationMapper.selectCher(ckPublicBenefitsZation);
        if (CollectionUtils.isNotEmpty(list)){
            throw new ServiceException("不能添加相同数据！！");
        }

        String[] yeaMo=ckPublicBenefitsZation.getBookTime().split("-");
        ckPublicBenefitsZation.setYear(Integer.valueOf(yeaMo[0]));
        ckPublicBenefitsZation.setStatus(1);
        ckPublicBenefitsZation.setCreateTime(new Date());
        ckPublicBenefitsZation.setCreateBy(accessToken.getUserCode());
        ckPublicBenefitsZation.setUct("0");
        ckPublicBenefitsZation.setEdutax("0");
        ckPublicBenefitsZation.setLedtax("0");
        ckPublicBenefitsZation.setOther("0");
        ckPublicBenefitsZation.setBacirate("0");
        ckPublicBenefitsZation.setStampduty("0");
        int index=ckPublicBenefitsZationMapper.insert(ckPublicBenefitsZation);
        if(index>0){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Calendar a = Calendar.getInstance();
            List<CkPublicBenefits> addlist=new ArrayList<>();
            String[] tiyue=ckPublicBenefitsZation.getBookTime().split("-");

            Integer year = a.get(Calendar.YEAR);

            if(year.equals(Integer.valueOf(tiyue[0])) || Integer.valueOf(tiyue[0])>year){
                for (int j = Integer.valueOf(tiyue[1]); j <=12 ; j++) {
                    CkPublicBenefits publicBenefits=new CkPublicBenefits();
                    publicBenefits.setOrgCode(ckPublicBenefitsZation.getOrgCode());
                    publicBenefits.setOrgName(ckPublicBenefitsZation.getOrgName());
                    publicBenefits.setCommCode(ckPublicBenefitsZation.getCommCode());
                    publicBenefits.setCommName(ckPublicBenefitsZation.getCommName());
                    publicBenefits.setYear(Integer.valueOf(tiyue[0]));
                    publicBenefits.setMonth(j);
                    publicBenefits.setStatus(1);

                    CkPublicBenefits benefits=ckPublicBenefitsMapper.selectOne(publicBenefits);
                    if(benefits!=null){
                        continue;
                    }
                    publicBenefits.setIsAuto(1);
                    a.setTime(sdf.parse(publicBenefits.getYear()+"-"+publicBenefits.getMonth()));
                    publicBenefits.setYearday(LocalDate.of(Integer.valueOf(publicBenefits.getYear()),1,1).lengthOfYear());
                    publicBenefits.setMonthday(a.getActualMaximum(Calendar.DAY_OF_MONTH));
                    publicBenefits.setUct(ckPublicBenefitsZation.getUct());
                    publicBenefits.setEdutax(ckPublicBenefitsZation.getEdutax());
                    publicBenefits.setLedtax(ckPublicBenefitsZation.getLedtax());
                    publicBenefits.setOther(ckPublicBenefitsZation.getOther());
                    publicBenefits.setBacirate(ckPublicBenefitsZation.getBacirate());
                    publicBenefits.setStampduty(ckPublicBenefitsZation.getStampduty());
                    publicBenefits.setCreateBy(accessToken.getUserCode());
                    publicBenefits.setCreateTime(new Date());
                    publicBenefits.setUpdateTime(new Date());
                    publicBenefits.setUpdateBy(accessToken.getUserCode());
                    publicBenefits.setCreateName(ckPublicBenefitsZation.getCreateName());
                    publicBenefits.setUpdateName(ckPublicBenefitsZation.getUpdateName());
                    addlist.add(publicBenefits);
                }
                if(addlist.size()!=0){
                    ckPublicBenefitsMapper.insertList(addlist);
                }
            }else {
                Integer yeaindexc=year-Integer.valueOf(tiyue[0]);
                for (int i = 0; i <= yeaindexc; i++) {
                    List<CkPublicBenefits> addlist2=new ArrayList<>();
                    int index2=0;
                    if(i==0){
                        index2=Integer.valueOf(tiyue[1]);
                    }else {
                        index2=1;
                    }
                    for (int j = index2; j <=12 ; j++) {
                        CkPublicBenefits publicBenefits=new CkPublicBenefits();
                        publicBenefits.setOrgCode(ckPublicBenefitsZation.getOrgCode());
                        publicBenefits.setOrgName(ckPublicBenefitsZation.getOrgName());
                        publicBenefits.setCommCode(ckPublicBenefitsZation.getCommCode());
                        publicBenefits.setCommName(ckPublicBenefitsZation.getCommName());
                        publicBenefits.setYear(Integer.valueOf(tiyue[0])+i);
                        publicBenefits.setMonth(j);
                        publicBenefits.setStatus(1);

                        CkPublicBenefits benefits=ckPublicBenefitsMapper.selectOne(publicBenefits);
                        if(benefits!=null){
                            continue;
                        }
                        publicBenefits.setIsAuto(1);
                        a.setTime(sdf.parse(publicBenefits.getYear()+"-"+publicBenefits.getMonth()));
                        publicBenefits.setYearday(LocalDate.of(Integer.valueOf(publicBenefits.getYear()),1,1).lengthOfYear());
                        publicBenefits.setMonthday(a.getActualMaximum(Calendar.DAY_OF_MONTH));
                        publicBenefits.setUct(ckPublicBenefitsZation.getUct());
                        publicBenefits.setEdutax(ckPublicBenefitsZation.getEdutax());
                        publicBenefits.setLedtax(ckPublicBenefitsZation.getLedtax());
                        publicBenefits.setOther(ckPublicBenefitsZation.getOther());
                        publicBenefits.setBacirate(ckPublicBenefitsZation.getBacirate());
                        publicBenefits.setStampduty(ckPublicBenefitsZation.getStampduty());
                        publicBenefits.setCreateBy(accessToken.getUserCode());
                        publicBenefits.setCreateTime(new Date());
                        publicBenefits.setUpdateTime(new Date());
                        publicBenefits.setUpdateBy(accessToken.getUserCode());
                        publicBenefits.setCreateName(ckPublicBenefitsZation.getCreateName());
                        publicBenefits.setUpdateName(ckPublicBenefitsZation.getUpdateName());
                        addlist2.add(publicBenefits);
                    }
                    if(addlist2.size()!=0){
                        ckPublicBenefitsMapper.insertList(addlist2);
                    }
                }
            }
        }
        return Response.response();
    }

    @Override
    public List<CkPublicBenefitsZationVo> listExport(CkPublicBenefitsZation ckPublicBenefits, AccessToken accessToken) {

        List<String> organCodeString=new ArrayList<>();
        List<String> commIdString=new ArrayList<>();
        List<String> rolesList = queryRoleCodes(null, accessToken.getUserCode());

        if (rolesList.contains("ck_public_benefits_region") || rolesList.contains("ck_public_benefits_admin") || rolesList.contains("admin")){
            if(rolesList.contains("ck_public_benefits_region")){
                List<RegionListVO> areaList = tbSysOrganMapper.findArea(null, accessToken.getUserCode());
                for (int i = 0; i < areaList.size(); i++) {
                    organCodeString.add(areaList.get(i).getOrganCode());
                    List<ProjectListVO> projectList = tbSysOrganMapper.findProject(areaList.get(i).getOrganCode(),accessToken.getUserCode(),null,null,null);
                    for (int j = 0; j <projectList.size() ; j++) {
                        commIdString.add(projectList.get(j).getCommId());
                    }
                }
            }
        }else {
            throw new ServiceException("此账号没有权限查看");
        }
        List<String> comtr=new ArrayList<>();
        List<CkPublicBenefitsZationVo> zationVoList=new ArrayList<>();

        List<CkPublicBenefitsZationVo> list=ckPublicBenefitsZationMapper.queryPageList(ckPublicBenefits,organCodeString,commIdString);
        if(CollectionUtils.isEmpty(list)){
            List<ProjectListVO> projectListVOS = tbSysOrganMapper.findProjectBenefitsZation(ckPublicBenefits.getOrgCode(), ckPublicBenefits.getCommCode(), comtr);

            for (int i = 0; i < projectListVOS.size(); i++) {
                CkPublicBenefitsZationVo publicBenefitsZationVo = new CkPublicBenefitsZationVo();
                publicBenefitsZationVo.setOrgCode(projectListVOS.get(i).getOrganCode());
                publicBenefitsZationVo.setOrgName(projectListVOS.get(i).getOrganName());
                publicBenefitsZationVo.setCommCode(projectListVOS.get(i).getCommId());
                publicBenefitsZationVo.setCommName(projectListVOS.get(i).getCommName());
                zationVoList.add(publicBenefitsZationVo);
            }
            list.addAll(zationVoList);
        }else {
            DecimalFormat df = new DecimalFormat("#,###.00");
            for (int i = 0; i < list.size(); i++) {
                comtr.add(list.get(i).getCommCode());
                if(StringUtils.isNotBlank(list.get(i).getAobamount())){
                    if(!list.get(i).getAobamount().equals("0")){
                        if(Double.valueOf(list.get(i).getAobamount())<1){
                            list.get(i).setAobamount(list.get(i).getAobamount());
                        }else {
                            list.get(i).setAobamount(df.format(Double.valueOf(list.get(i).getAobamount())));
                        }
                    }
                }
                if(StringUtils.isNotBlank(list.get(i).getUncollected())){
                    if(!list.get(i).getUncollected().equals("0")){
                        if(Double.valueOf(list.get(i).getUncollected())<1){
                            list.get(i).setUncollected(list.get(i).getUncollected());
                        }else {
                            list.get(i).setUncollected(df.format(Double.valueOf(list.get(i).getUncollected())));
                        }
                    }
                }
                if (StringUtils.isNotBlank(list.get(i).getPayablebut())){
                    if(!list.get(i).getPayablebut().equals("0")){
                        if(Double.valueOf(list.get(i).getPayablebut())<1){
                            list.get(i).setPayablebut(list.get(i).getPayablebut());
                        }else {
                            list.get(i).setPayablebut(df.format(Double.valueOf(list.get(i).getPayablebut())));
                        }
                    }
                }

            }

            List<ProjectListVO> projectListVOS = tbSysOrganMapper.findProjectBenefitsZation(ckPublicBenefits.getOrgCode(), ckPublicBenefits.getCommCode(), comtr);
            for (int j = 0; j < projectListVOS.size(); j++) {
                CkPublicBenefitsZationVo publicBenefitsZationVo=new CkPublicBenefitsZationVo();
                publicBenefitsZationVo.setOrgCode(projectListVOS.get(j).getOrganCode());
                publicBenefitsZationVo.setOrgName(projectListVOS.get(j).getOrganName());
                publicBenefitsZationVo.setCommCode(projectListVOS.get(j).getCommId());
                publicBenefitsZationVo.setCommName(projectListVOS.get(j).getCommName());
                zationVoList.add(publicBenefitsZationVo);
            }
            list.addAll(zationVoList);

        }
        return list;
    }

    @Override
    public Result listChcki(CkPublicBenefitsZation ckPublicBenefitsZation, AccessToken accessToken) {
        List<String> organCodeString=new ArrayList<>();
        List<String> commIdString=new ArrayList<>();
        List<String> rolesList = queryRoleCodes(null, accessToken.getUserCode());

        if (rolesList.contains("ck_public_benefits_region") || rolesList.contains("ck_public_benefits_admin") || rolesList.contains("admin")){
            if(rolesList.contains("ck_public_benefits_region")){
                List<RegionListVO> areaList = tbSysOrganMapper.findArea(null, accessToken.getUserCode());
                for (int i = 0; i < areaList.size(); i++) {
                    organCodeString.add(areaList.get(i).getOrganCode());
                    List<ProjectListVO> projectList = tbSysOrganMapper.findProject(areaList.get(i).getOrganCode(),accessToken.getUserCode(),null,null,null);
                    for (int j = 0; j <projectList.size() ; j++) {
                        commIdString.add(projectList.get(j).getCommId());
                    }
                }
            }
        }else {
            throw new ServiceException("此账号没有权限查看");
        }

        List<CkPublicBenefitsZationVo> list=ckPublicBenefitsZationMapper.queryPageList(ckPublicBenefitsZation,organCodeString,commIdString);
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException("数据为空，无法导出");
        }
        return Response.response();
    }

    @Override
    public Result editZation(CkPublicBenefitsZation ckPublicBenefitsZation, AccessToken accessToken) throws ParseException {

        CkPublicBenefitsZation ck=new CkPublicBenefitsZation();
        String[] yeaMo=ckPublicBenefitsZation.getBookTime().split("-");
        ck.setBookTime(ckPublicBenefitsZation.getBookTime());
        ck.setOrgCode(ckPublicBenefitsZation.getOrgCode());
        ck.setOrgName(ckPublicBenefitsZation.getOrgName());
        ck.setCommCode(ckPublicBenefitsZation.getCommCode());
        ck.setCommName(ckPublicBenefitsZation.getCommName());
        ck.setYear(Integer.valueOf(yeaMo[0]));
        ck.setUpdateTime(new Date());
        ck.setCreateBy(accessToken.getUserCode());
        if(NumberUtil.isNumber(ckPublicBenefitsZation.getPayablebut().toString())){
            ck.setPayablebut(ckPublicBenefitsZation.getPayablebut());
        }else {
            throw new ServiceException("期初应付未付输入错误");
        }
        if(NumberUtil.isNumber(ckPublicBenefitsZation.getUncollected().toString())){
            ck.setUncollected(ckPublicBenefitsZation.getUncollected());
        }else {
            throw new ServiceException("期初应收未收输入错误");
        }
        if(NumberUtil.isNumber(ckPublicBenefitsZation.getAobamount().toString())){
            ck.setAobamount(ckPublicBenefitsZation.getAobamount());
        }else {
            throw new ServiceException("期初结存金额未收输入错误");
        }

/*        ck.setUct("0");
        ck.setEdutax("0");
        ck.setLedtax("0");
        ck.setOther("0");
        ck.setStatus(1);
        ck.setBacirate("0");
        ck.setStampduty("0");*/
        //int index=ckPublicBenefitsZationMapper.deleteByIds(String.valueOf(ckPublicBenefitsZation.getId()));
        List<CkPublicBenefitsZation> list= ckPublicBenefitsZationMapper.selectByIds(ckPublicBenefitsZation.getId().toString());
        if(CollectionUtils.isNotEmpty(list)){
            ck.setId(list.get(0).getId());
            int index=ckPublicBenefitsZationMapper.updateByPrimaryKeySelective(ck);
            if(index>0) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                Calendar a = Calendar.getInstance();
                List<CkPublicBenefits> addlist = new ArrayList<>();
                String[] tiyue = ck.getBookTime().split("-");

                Integer year = a.get(Calendar.YEAR);

                if (year.equals(Integer.valueOf(tiyue[0])) || Integer.valueOf(tiyue[0]) > year) {
                    for (int j = Integer.valueOf(tiyue[1]); j <= 12; j++) {
                        CkPublicBenefits publicBenefits = new CkPublicBenefits();
                        publicBenefits.setOrgCode(ck.getOrgCode());
                        publicBenefits.setOrgName(ck.getOrgName());
                        publicBenefits.setCommCode(ck.getCommCode());
                        publicBenefits.setCommName(ck.getCommName());
                        publicBenefits.setYear(Integer.valueOf(tiyue[0]));
                        publicBenefits.setMonth(j);
                        publicBenefits.setStatus(1);

                        CkPublicBenefits benefits = ckPublicBenefitsMapper.selectOne(publicBenefits);
                        if (benefits != null) {
                            continue;
                        }
                        publicBenefits.setIsAuto(1);
                        a.setTime(sdf.parse(publicBenefits.getYear() + "-" + publicBenefits.getMonth()));
                        publicBenefits.setYearday(LocalDate.of(Integer.valueOf(publicBenefits.getYear()), 1, 1).lengthOfYear());
                        publicBenefits.setMonthday(a.getActualMaximum(Calendar.DAY_OF_MONTH));
                        publicBenefits.setUct(list.get(0).getUct());
                        publicBenefits.setEdutax(list.get(0).getEdutax());
                        publicBenefits.setLedtax(list.get(0).getLedtax());
                        publicBenefits.setOther(list.get(0).getOther());
                        publicBenefits.setBacirate(list.get(0).getBacirate());
                        publicBenefits.setStampduty(list.get(0).getStampduty());
                        publicBenefits.setCreateBy(accessToken.getUserCode());
                        publicBenefits.setCreateTime(new Date());
                        publicBenefits.setUpdateTime(new Date());
                        publicBenefits.setUpdateBy(accessToken.getUserCode());
                        publicBenefits.setCreateName(ckPublicBenefitsZation.getCreateName());
                        publicBenefits.setUpdateName(ckPublicBenefitsZation.getUpdateName());
                        addlist.add(publicBenefits);
                    }
                    if (addlist.size() != 0) {
                        ckPublicBenefitsMapper.insertList(addlist);
                    }
                }else {
                    Integer yeaindexc=year-Integer.valueOf(tiyue[0]);
                    for (int i = 0; i <= yeaindexc; i++) {
                        List<CkPublicBenefits> addlist2 = new ArrayList<>();
                        int index2 = 0;
                        if (i == 0) {
                            index2 = Integer.valueOf(tiyue[1]);
                        } else {
                            index2 = 1;
                        }
                        for (int j = index2; j <= 12; j++) {
                            CkPublicBenefits publicBenefits = new CkPublicBenefits();
                            publicBenefits.setOrgCode(ckPublicBenefitsZation.getOrgCode());
                            publicBenefits.setOrgName(ckPublicBenefitsZation.getOrgName());
                            publicBenefits.setCommCode(ckPublicBenefitsZation.getCommCode());
                            publicBenefits.setCommName(ckPublicBenefitsZation.getCommName());
                            publicBenefits.setYear(Integer.valueOf(tiyue[0]) + i);
                            publicBenefits.setMonth(j);
                            publicBenefits.setStatus(1);

                            CkPublicBenefits benefits = ckPublicBenefitsMapper.selectOne(publicBenefits);
                            if (benefits != null) {
                                continue;
                            }
                            publicBenefits.setIsAuto(1);
                            a.setTime(sdf.parse(publicBenefits.getYear() + "-" + publicBenefits.getMonth()));
                            publicBenefits.setYearday(LocalDate.of(Integer.valueOf(publicBenefits.getYear()), 1, 1).lengthOfYear());
                            publicBenefits.setMonthday(a.getActualMaximum(Calendar.DAY_OF_MONTH));
                            publicBenefits.setUct(list.get(0).getUct());
                            publicBenefits.setEdutax(list.get(0).getEdutax());
                            publicBenefits.setLedtax(list.get(0).getLedtax());
                            publicBenefits.setOther(list.get(0).getOther());
                            publicBenefits.setBacirate(list.get(0).getBacirate());
                            publicBenefits.setStampduty(list.get(0).getStampduty());
                            publicBenefits.setCreateBy(accessToken.getUserCode());
                            publicBenefits.setCreateTime(new Date());
                            publicBenefits.setUpdateTime(new Date());
                            publicBenefits.setUpdateBy(accessToken.getUserCode());
                            publicBenefits.setCreateName(ckPublicBenefitsZation.getCreateName());
                            publicBenefits.setUpdateName(ckPublicBenefitsZation.getUpdateName());
                            addlist2.add(publicBenefits);
                        }
                        if (addlist2.size() != 0) {
                            ckPublicBenefitsMapper.insertList(addlist2);
                        }
                    }
                }
            }

       /*    int num=ckPublicBenefitsZationMapper.insert(ck);
           if(num>0){
               SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
               Calendar a = Calendar.getInstance();
               List<CkPublicBenefits> addlist=new ArrayList<>();
               String[] tiyue=ckPublicBenefitsZation.getBookTime().split("-");

               Integer year = a.get(Calendar.YEAR);

               if(year.equals(Integer.valueOf(tiyue[0])) || Integer.valueOf(tiyue[0])>year){
                   for (int j = Integer.valueOf(tiyue[1]); j <=12 ; j++) {
                       CkPublicBenefits publicBenefits=new CkPublicBenefits();
                       publicBenefits.setOrgCode(ckPublicBenefitsZation.getOrgCode());
                       publicBenefits.setOrgName(ckPublicBenefitsZation.getOrgName());
                       publicBenefits.setCommCode(ckPublicBenefitsZation.getCommCode());
                       publicBenefits.setCommName(ckPublicBenefitsZation.getCommName());
                       publicBenefits.setYear(Integer.valueOf(tiyue[0]));
                       publicBenefits.setMonth(j);
                       publicBenefits.setStatus(1);

                       CkPublicBenefits benefits=ckPublicBenefitsMapper.selectOne(publicBenefits);
                       if(benefits!=null){
                           continue;
                       }
                       publicBenefits.setIsAuto(1);
                       a.setTime(sdf.parse(publicBenefits.getYear()+"-"+publicBenefits.getMonth()));
                       publicBenefits.setYearday(LocalDate.of(Integer.valueOf(publicBenefits.getYear()),1,1).lengthOfYear());
                       publicBenefits.setMonthday(a.getActualMaximum(Calendar.DAY_OF_MONTH));
                       publicBenefits.setUct(ck.getUct());
                       publicBenefits.setEdutax(ck.getEdutax());
                       publicBenefits.setLedtax(ck.getLedtax());
                       publicBenefits.setOther(ck.getOther());
                       publicBenefits.setBacirate(ck.getBacirate());
                       publicBenefits.setStampduty(ck.getStampduty());
                       publicBenefits.setCreateBy(accessToken.getUserCode());
                       publicBenefits.setCreateTime(new Date());
                       publicBenefits.setUpdateTime(new Date());
                       publicBenefits.setUpdateBy(accessToken.getUserCode());
                       publicBenefits.setCreateName(ckPublicBenefitsZation.getCreateName());
                       publicBenefits.setUpdateName(ckPublicBenefitsZation.getUpdateName());
                       addlist.add(publicBenefits);
                   }
                   if(addlist.size()!=0){
                       ckPublicBenefitsMapper.insertList(addlist);
                   }
               }else {
                   Integer yeaindexc=year-Integer.valueOf(tiyue[0]);
                   for (int i = 0; i <= yeaindexc; i++) {
                       List<CkPublicBenefits> addlist2=new ArrayList<>();
                       int index2=0;
                       if(i==0){
                           index2=Integer.valueOf(tiyue[1]);
                       }else {
                           index2=1;
                       }
                       for (int j = index2; j <=12 ; j++) {
                           CkPublicBenefits publicBenefits=new CkPublicBenefits();
                           publicBenefits.setOrgCode(ckPublicBenefitsZation.getOrgCode());
                           publicBenefits.setOrgName(ckPublicBenefitsZation.getOrgName());
                           publicBenefits.setCommCode(ckPublicBenefitsZation.getCommCode());
                           publicBenefits.setCommName(ckPublicBenefitsZation.getCommName());
                           publicBenefits.setYear(Integer.valueOf(tiyue[0])+i);
                           publicBenefits.setMonth(j);
                           publicBenefits.setStatus(1);

                           CkPublicBenefits benefits=ckPublicBenefitsMapper.selectOne(publicBenefits);
                           if(benefits!=null){
                               continue;
                           }
                           publicBenefits.setIsAuto(1);
                           a.setTime(sdf.parse(publicBenefits.getYear()+"-"+publicBenefits.getMonth()));
                           publicBenefits.setYearday(LocalDate.of(Integer.valueOf(publicBenefits.getYear()),1,1).lengthOfYear());
                           publicBenefits.setMonthday(a.getActualMaximum(Calendar.DAY_OF_MONTH));
                           publicBenefits.setUct(ck.getUct());
                           publicBenefits.setEdutax(ck.getEdutax());
                           publicBenefits.setLedtax(ck.getLedtax());
                           publicBenefits.setOther(ck.getOther());
                           publicBenefits.setBacirate(ck.getBacirate());
                           publicBenefits.setStampduty(ck.getStampduty());
                           publicBenefits.setCreateBy(accessToken.getUserCode());
                           publicBenefits.setCreateTime(new Date());
                           publicBenefits.setUpdateTime(new Date());
                           publicBenefits.setUpdateBy(accessToken.getUserCode());
                           publicBenefits.setCreateName(ckPublicBenefitsZation.getCreateName());
                           publicBenefits.setUpdateName(ckPublicBenefitsZation.getUpdateName());
                           addlist2.add(publicBenefits);
                       }
                       if(addlist2.size()!=0){
                           ckPublicBenefitsMapper.insertList(addlist2);
                       }
                   }
               }
           }*/
        }
        return Response.response();
    }

    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("权限不足");
    }
}
