package com.siwei.rsmonitoring.service.impl;

import com.siwei.common.core.constant.SecurityConstants;
import com.siwei.common.core.domain.R;
import com.siwei.common.core.exception.ServiceException;
import com.siwei.common.core.utils.StringUtils;
import com.siwei.rsmonitoring.domain.TDictData;
import com.siwei.rsmonitoring.domain.vo.DataCountVo;
import com.siwei.rsmonitoring.domain.vo.QkCountVo;
import com.siwei.rsmonitoring.mapper.QkZtMapper;
import com.siwei.rsmonitoring.service.IQkZtService;
import com.siwei.rsmonitoring.service.ITDictDataService;
import com.siwei.system.api.RemoteDeptService;
import com.siwei.system.api.domain.SysDept;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Service
public class QkZtServiceImpl implements IQkZtService {
    @Autowired
    private RemoteDeptService remoteDeptService;
    @Autowired
    private QkZtMapper qkZtMapper;
    @Autowired
    private ITDictDataService itDictDataService;

    @Override
    public QkCountVo selectQkZtCount(QkCountVo vo) {
        Long xzq = null;
        if (StringUtils.isNotEmpty(vo.getXzqdm())) {
            xzq = Long.parseLong(vo.getXzqdm());
        }
        Set<String> xzqSet = selectXzqdmUserList(xzq);
        vo.setXzqdms(xzqSet);
        QkCountVo dq = qkZtMapper.selectQkZtCount(vo);

        QkCountVo hzvo = new QkCountVo();
        hzvo.setAuditflowList(vo.getAuditflowListDb());
        hzvo.setXzqdms(xzqSet);
        QkCountVo hz = qkZtMapper.selectQkZtCount(hzvo);

        QkCountVo retVo = new QkCountVo();
        retVo.setDq(dq);
        retVo.setHz(hz);
        BigDecimal num1 = new BigDecimal(dq.getCountSum().toString());
        BigDecimal num2 = new BigDecimal(hz.getCountSum().toString());
        if (num2.compareTo(BigDecimal.ZERO) == 0) {
            retVo.setZb(0.00);
        } else {
            BigDecimal result = num1.divide(num2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
            retVo.setZb(result.doubleValue());
        }
        return retVo;
    }

    @Override
    public QkCountVo selectypdtbcfQkZtCount(QkCountVo vo) {
        Long xzq = null;
        if (StringUtils.isNotEmpty(vo.getXzqdm())) {
            xzq = Long.parseLong(vo.getXzqdm());
        }
        Set<String> xzqSet = selectXzqdmUserList(xzq);
        vo.setXzqdms(xzqSet);
        QkCountVo dq = qkZtMapper.selectYpdtbcfQkZtCount(vo);

        QkCountVo hzvo = new QkCountVo();
        hzvo.setAuditflowList(vo.getAuditflowListDb());
        hzvo.setXzqdms(xzqSet);
        QkCountVo hz = qkZtMapper.selectYpdtbcfQkZtCount(hzvo);

        QkCountVo retVo = new QkCountVo();
        retVo.setDq(dq);
        retVo.setHz(hz);
        BigDecimal num1 = new BigDecimal(dq.getCountSum().toString());
        BigDecimal num2 = new BigDecimal(hz.getCountSum().toString());
        if (num2.compareTo(BigDecimal.ZERO) == 0) {
            retVo.setZb(0.00);
        } else {
            BigDecimal result = num1.divide(num2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
            retVo.setZb(result.doubleValue());
        }
        return retVo;
    }

    @Override
    public QkCountVo selectpdjgQkZtCount(QkCountVo vo) {
        Long xzq = null;
        if (StringUtils.isNotEmpty(vo.getXzqdm())) {
            xzq = Long.parseLong(vo.getXzqdm());
        }
        Set<String> xzqSet = selectXzqdmUserList(xzq);
        vo.setXzqdms(xzqSet);

        //待举证
        String summary_sql = " ";
        String result_sql = "";
        if (vo.getDecide() == null) {
            summary_sql = " 1 as v1_number ,dkmj::numeric  v1_sum ";

        } else if (0 == vo.getDecide()) {
            summary_sql = " case when decide='0'  then  1  else 0 end v1_number," +
                    "case when decide='0'  then  dkmj::numeric  else 0 end  v1_sum ";
        } else if (1 == vo.getDecide()) {
            summary_sql = " case when decide='1'  then  1  else 0 end v1_number," +
                    "case when decide='1'  then  dkmj::numeric  else 0 end  v1_sum ";
        } else if (2 == vo.getDecide()) {
            summary_sql = " case when decide='2'  then  1  else 0 end v1_number," +
                    "case when decide='2'  then  dkmj::numeric  else 0 end  v1_sum ";
        }
        result_sql = "coalesce(sum(v1_number),0) countNumber,coalesce(sum(v1_sum),0) countSum ";
        QkCountVo dq = new QkCountVo();
        Map<String, Object> dq_v0 = qkZtMapper.selectPdtuSqlCount(vo, summary_sql, result_sql);
        dq.setCountNumber((Long) dq_v0.get("countnumber"));
        dq.setCountSum(((BigDecimal) dq_v0.get("countsum")).doubleValue());

        QkCountVo hz = new QkCountVo();
        QkCountVo hzvo = new QkCountVo();
        hzvo.setXzqdms(xzqSet);
        hzvo.setAuditflowList(vo.getAuditflowListDb());
        Map<String, Object> hz_v0 = qkZtMapper.selectPdtuSqlCount(hzvo, summary_sql, result_sql);
        hz.setCountNumber((Long) hz_v0.get("countnumber"));
        hz.setCountSum(((BigDecimal) dq_v0.get("countsum")).doubleValue());

        QkCountVo retVo = new QkCountVo();
        retVo.setDq(dq);
        retVo.setHz(hz);
        BigDecimal num1 = new BigDecimal(dq.getCountSum().toString());
        BigDecimal num2 = new BigDecimal(hz.getCountSum().toString());
        if (num2.compareTo(BigDecimal.ZERO) == 0) {
            retVo.setZb(0.00);
        } else {
            BigDecimal result = num1.divide(num2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
            retVo.setZb(result.doubleValue());
        }
        return retVo;
    }


    @Override
    public List<QkCountVo> selectQkZtCountList(QkCountVo vo) {
        Long xzq = null;
        if (StringUtils.isNotEmpty(vo.getXzqdm())) {
            xzq = Long.parseLong(vo.getXzqdm());
        }
        List<SysDept> xzqList = selectXzqdmUseTree(xzq);
        if (xzqList.isEmpty()) {
            throw new ServiceException("未查询到行政区县");
        }
        List<QkCountVo> retQkCountVo = new ArrayList<QkCountVo>();

        SysDept xzqhz = xzqList.get(0);
        Set<String> xzqSet = selectXzqdmUserList(xzqhz.getDeptId());
        vo.setXzqdms(xzqSet);
        QkCountVo dq = qkZtMapper.selectQkZtCount(vo);
        dq.setXzqdm(xzqhz.getDistrict());
        dq.setXzqmc("合计");
        dq.setGeom(xzqhz.getGeom());
        retQkCountVo.add(dq);

        List<SysDept> xzqChildrenList = xzqhz.getChildren();
        for (SysDept children : xzqChildrenList) {
            Set<String> xzqSetV1 = selectXzqdmUserList(children.getDeptId());
            vo.setXzqdms(xzqSetV1);
            QkCountVo dqV1 = qkZtMapper.selectQkZtCount(vo);
            dqV1.setXzqdm(children.getDistrict());
            dqV1.setXzqmc(children.getDeptName());
            dqV1.setGeom(children.getGeom());
            retQkCountVo.add(dqV1);
        }

        return retQkCountVo;
    }

    @Override
    public List<QkCountVo> selectQkZtJzCountList(QkCountVo vo) {
        Long xzq = null;
        if (StringUtils.isNotEmpty(vo.getXzqdm())) {
            xzq = Long.parseLong(vo.getXzqdm());
        }
        List<SysDept> xzqList = selectXzqdmUseTree(xzq);
        if (xzqList.isEmpty()) {
            throw new ServiceException("未查询到行政区县");
        }
        List<QkCountVo> retQkCountVo = new ArrayList<QkCountVo>();

        SysDept xzqhz = xzqList.get(0);
        List<SysDept> xzqChildrenList = xzqhz.getChildren();

//        t2.step_code = #{audit.auditflowStep} </if>
//             and t2.success_type = #{audit.successType}</if>

        //待举证
        String summary_sql = " case when step_code='WYFF' and  success_type='0' then  1  else 0 end djz_number," + "case when step_code='WYFF' and  success_type='0' then  dkmj::numeric  else 0 end djz_sum," + " case when (step_code='WYQR' and  success_type='0') or  (step_code='WYDC' ) then  1  else 0 end jzz_number," + "case when (step_code='WYQR' and  success_type='0') or  (step_code='WYDC' ) then  dkmj::numeric  else 0 end jzz_sum," + "case when step_code in ('WYTJ','XJHGX','XJSH','XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') then  1  else 0 end yjz_number," + "case when step_code in ('WYTJ','XJHGX','XJSH','XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') then  dkmj::numeric  else 0 end yjz_sum";

        String result_sql = "sum(djz_sum+jzz_sum+yjz_sum) countNumber,sum(djz_number+jzz_number+yjz_number) countSum," + "sum(djz_sum) djz_sum,sum(djz_number) djz_number," + "sum(jzz_sum) jzz_sum,sum(jzz_number) jzz_number," + "sum(yjz_sum) yjz_sum,sum(yjz_number) yjz_number";


        Set<String> xzqSet = selectXzqdmUserList(xzqhz.getDeptId());
        vo.setXzqdms(xzqSet);
        Map<String, Object> dq_v0 = qkZtMapper.selectSqlCount(vo, summary_sql, result_sql);
        QkCountVo dq = new QkCountVo();
        dq.setXzqdm(xzqhz.getDistrict());
        dq.setXzqmc("合计");
        dq.setGeom(xzqhz.getGeom());
        dq.setHzjg(dq_v0);
        retQkCountVo.add(dq);
        for (SysDept children : xzqChildrenList) {
            Set<String> xzqSetV1 = selectXzqdmUserList(children.getDeptId());
            vo.setXzqdms(xzqSetV1);
            Map<String, Object> dqV1_v0 = qkZtMapper.selectSqlCount(vo, summary_sql, result_sql);
            QkCountVo dqV1 = new QkCountVo();
            dqV1.setXzqdm(children.getDistrict());
            dqV1.setXzqmc(children.getDeptName());
            dqV1.setGeom(children.getGeom());
            dqV1.setHzjg(dqV1_v0);
            retQkCountVo.add(dqV1);
        }

        return retQkCountVo;
    }

    @Override
    public Set<String> selectXzqdmUserList(Long checkXzqdm) {
        R<List<SysDept>> r1 = remoteDeptService.childByDeptId(checkXzqdm, false, SecurityConstants.INNER);
        if (R.isError(r1)) {
            throw new ServiceException(r1.getMsg());
        }
        List<SysDept> list = r1.getData();
        if (list.isEmpty()) {
            throw new ServiceException("用户查询失败");
        }
        Set<String> xzqhdm = new HashSet<>();
        for (SysDept obj : list) {
            xzqhdm.add(obj.getDistrict());
        }
        return xzqhdm;
    }

    @Override
    public List<QkCountVo> selectQkZtXjshCountList(QkCountVo vo) {
        Long xzq = null;
        if (StringUtils.isNotEmpty(vo.getXzqdm())) {
            xzq = Long.parseLong(vo.getXzqdm());
        }
        List<SysDept> xzqList = selectXzqdmUseTree(xzq);
        if (xzqList.isEmpty()) {
            throw new ServiceException("未查询到行政区县");
        }
        List<QkCountVo> retQkCountVo = new ArrayList<QkCountVo>();

        SysDept xzqhz = xzqList.get(0);
        List<SysDept> xzqChildrenList = xzqhz.getChildren();

//        t2.step_code = #{audit.auditflowStep} </if>
//             and t2.success_type = #{audit.successType}</if>

        //待举证
        String summary_sql = " " + "case when step_code in ('WYTJ','XJHGX','XJSH') then  1  else 0 end v1_number," + "case when step_code in ('WYTJ','XJHGX','XJSH') then  dkmj::numeric  else 0 end v1_sum," + "case when (step_code='XJTJ' and  success_type='0') or  ( step_code in ('SJSH','SJTJ','SJJSH','SJJTJ','-999') ) then  1  else 0 end v2_number," + "case when (step_code='XJTJ' and  success_type='0') or  ( step_code in ('SJSH','SJTJ','SJJSH','SJJTJ','-999') ) then  dkmj::numeric  else 0 end v2_sum," + "case when step_code='XJTJ' and  success_type='1' then  1  else 0 end v3_number," + "case when step_code='XJTJ' and  success_type='1' then  dkmj::numeric  else 0 end v3_sum";

        String result_sql = "sum(v1_sum+v2_sum+v3_sum) countNumber,sum(v1_number+v2_number+v3_number) countSum," + "sum(v1_sum) v1_sum,sum(v1_number) v1_number," + "sum(v2_sum) v2_sum,sum(v2_number) v2_number," + "sum(v3_sum) v3_sum,sum(v3_number) v3_number";


        Set<String> xzqSet = selectXzqdmUserList(xzqhz.getDeptId());
        vo.setXzqdms(xzqSet);
        Map<String, Object> dq_v0 = qkZtMapper.selectSqlCount(vo, summary_sql, result_sql);
        QkCountVo dq = new QkCountVo();
        dq.setXzqdm(xzqhz.getDistrict());
        dq.setXzqmc("合计");
        dq.setGeom(xzqhz.getGeom());
        dq.setHzjg(dq_v0);
        retQkCountVo.add(dq);
        for (SysDept children : xzqChildrenList) {
            Set<String> xzqSetV1 = selectXzqdmUserList(children.getDeptId());
            vo.setXzqdms(xzqSetV1);
            Map<String, Object> dqV1_v0 = qkZtMapper.selectSqlCount(vo, summary_sql, result_sql);
            QkCountVo dqV1 = new QkCountVo();
            dqV1.setXzqdm(children.getDistrict());
            dqV1.setXzqmc(children.getDeptName());
            dqV1.setGeom(children.getGeom());
            dqV1.setHzjg(dqV1_v0);
            retQkCountVo.add(dqV1);
        }

        return retQkCountVo;
    }

    @Override
    public List<QkCountVo> selectQkZtSjshCountList(QkCountVo vo) {
        Long xzq = null;
        if (StringUtils.isNotEmpty(vo.getXzqdm())) {
            xzq = Long.parseLong(vo.getXzqdm());
        }
        List<SysDept> xzqList = selectXzqdmUseTree(xzq);
        if (xzqList.isEmpty()) {
            throw new ServiceException("未查询到行政区县");
        }
        List<QkCountVo> retQkCountVo = new ArrayList<QkCountVo>();

        SysDept xzqhz = xzqList.get(0);
        List<SysDept> xzqChildrenList = xzqhz.getChildren();

//        t2.step_code = #{audit.auditflowStep} </if>
//             and t2.success_type = #{audit.successType}</if>

        //待举证
        String summary_sql = " " + "case when step_code in ('XJTJ','SJSH') then  1  else 0 end v1_number," + "case when step_code in ('XJTJ','SJSH') then  dkmj::numeric  else 0 end v1_sum," + "case when (step_code='SJTJ' and  success_type='0') or  ( step_code in ('SJJSH','SJJTJ','-999') ) then  1  else 0 end v2_number," + "case when (step_code='SJTJ' and  success_type='0') or  ( step_code in ('SJJSH','SJJTJ','-999') ) then  dkmj::numeric  else 0 end v2_sum," + "case when step_code='SJTJ' and  success_type='1' then  1  else 0 end v3_number," + "case when step_code='SJTJ' and  success_type='1' then  dkmj::numeric  else 0 end v3_sum";

        String result_sql = "sum(v1_sum+v2_sum+v3_sum) countNumber,sum(v1_number+v2_number+v3_number) countSum," + "sum(v1_sum) v1_sum,sum(v1_number) v1_number," + "sum(v2_sum) v2_sum,sum(v2_number) v2_number," + "sum(v3_sum) v3_sum,sum(v3_number) v3_number";


        Set<String> xzqSet = selectXzqdmUserList(xzqhz.getDeptId());
        vo.setXzqdms(xzqSet);
        Map<String, Object> dq_v0 = qkZtMapper.selectSqlCount(vo, summary_sql, result_sql);
        QkCountVo dq = new QkCountVo();
        dq.setXzqdm(xzqhz.getDistrict());
        dq.setXzqmc("合计");
        dq.setGeom(xzqhz.getGeom());
        dq.setHzjg(dq_v0);
        retQkCountVo.add(dq);
        for (SysDept children : xzqChildrenList) {
            Set<String> xzqSetV1 = selectXzqdmUserList(children.getDeptId());
            vo.setXzqdms(xzqSetV1);
            Map<String, Object> dqV1_v0 = qkZtMapper.selectSqlCount(vo, summary_sql, result_sql);
            QkCountVo dqV1 = new QkCountVo();
            dqV1.setXzqdm(children.getDistrict());
            dqV1.setXzqmc(children.getDeptName());
            dqV1.setGeom(children.getGeom());
            dqV1.setHzjg(dqV1_v0);
            retQkCountVo.add(dqV1);
        }

        return retQkCountVo;
    }

    @Override
    public List<QkCountVo> selectQkZtSJJSHCountList(QkCountVo vo) {
        Long xzq = null;
        if (StringUtils.isNotEmpty(vo.getXzqdm())) {
            xzq = Long.parseLong(vo.getXzqdm());
        }
        List<SysDept> xzqList = selectXzqdmUseTree(xzq);
        if (xzqList.isEmpty()) {
            throw new ServiceException("未查询到行政区县");
        }
        List<QkCountVo> retQkCountVo = new ArrayList<QkCountVo>();

        SysDept xzqhz = xzqList.get(0);
        List<SysDept> xzqChildrenList = xzqhz.getChildren();

//        t2.step_code = #{audit.auditflowStep} </if>
//             and t2.success_type = #{audit.successType}</if>

        //待举证
        String summary_sql = " " + "case when step_code in ('SJTJ','SJJSH') then  1  else 0 end v1_number," + "case when step_code in ('SJTJ','SJJSH') then  dkmj::numeric  else 0 end v1_sum," + "case when (step_code='SJJTJ' and  success_type='0') or  ( step_code in ('-999') ) then  1  else 0 end v2_number," + "case when (step_code='SJJTJ' and  success_type='0') or  ( step_code in ('-999') ) then  dkmj::numeric  else 0 end v2_sum," + "case when step_code='SJJTJ' and  success_type='1' then  1  else 0 end v3_number," + "case when step_code='SJJTJ' and  success_type='1' then  dkmj::numeric  else 0 end v3_sum";

        String result_sql = "sum(v1_sum+v2_sum+v3_sum) countNumber,sum(v1_number+v2_number+v3_number) countSum," + "sum(v1_sum) v1_sum,sum(v1_number) v1_number," + "sum(v2_sum) v2_sum,sum(v2_number) v2_number," + "sum(v3_sum) v3_sum,sum(v3_number) v3_number";


        Set<String> xzqSet = selectXzqdmUserList(xzqhz.getDeptId());
        vo.setXzqdms(xzqSet);
        Map<String, Object> dq_v0 = qkZtMapper.selectSqlCount(vo, summary_sql, result_sql);
        QkCountVo dq = new QkCountVo();
        dq.setXzqdm(xzqhz.getDistrict());
        dq.setXzqmc("合计");
        dq.setGeom(xzqhz.getGeom());
        dq.setHzjg(dq_v0);
        retQkCountVo.add(dq);
        for (SysDept children : xzqChildrenList) {
            Set<String> xzqSetV1 = selectXzqdmUserList(children.getDeptId());
            vo.setXzqdms(xzqSetV1);
            Map<String, Object> dqV1_v0 = qkZtMapper.selectSqlCount(vo, summary_sql, result_sql);
            QkCountVo dqV1 = new QkCountVo();
            dqV1.setXzqdm(children.getDistrict());
            dqV1.setXzqmc(children.getDeptName());
            dqV1.setGeom(children.getGeom());
            dqV1.setHzjg(dqV1_v0);
            retQkCountVo.add(dqV1);
        }

        return retQkCountVo;
    }

    @Override
    public List<QkCountVo> selectpdjdQkZtCountList(QkCountVo vo) {
        Long xzq = null;
        if (StringUtils.isNotEmpty(vo.getXzqdm())) {
            xzq = Long.parseLong(vo.getXzqdm());
        }

        List<SysDept> xzqList = selectXzqdmUseTree(xzq);
        if (xzqList.isEmpty()) {
            throw new ServiceException("未查询到行政区县");
        }

        List<QkCountVo> retQkCountVo = new ArrayList<QkCountVo>();

        SysDept xzqhz = xzqList.get(0);
        List<SysDept> xzqChildrenList = xzqhz.getChildren();

//        t2.step_code = #{audit.auditflowStep} </if>
//             and t2.success_type = #{audit.successType}</if>

        //待举证
        String summary_sql = "1 as v1_number, dkmj::numeric v1_sum," +
                "case when step_code not in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') then  1  else 0 end v2_number," +
                "case when step_code not in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') then  dkmj::numeric  else 0 end v2_sum," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') then  1  else 0 end v3_number," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') then  dkmj::numeric  else 0 end v3_sum ";

        String result_sql = "coalesce(sum(v1_sum),0) v1_sum,coalesce(sum(v1_number),0) v1_number," +
                "coalesce(sum(v2_sum),0) v2_sum,coalesce(sum(v2_number),0) v2_number," +
                "coalesce(sum(v3_sum),0) v3_sum,coalesce(sum(v3_number),0) v3_number";


        Set<String> xzqSet = selectXzqdmUserList(xzqhz.getDeptId());
        vo.setXzqdms(xzqSet);
        Map<String, Object> dq_v0 = qkZtMapper.selectSqlCount(vo, summary_sql, result_sql);

        String query = "XJTJ,SJSH,SJTJ,SJJSH,SJJTJ,-999";
        QkCountVo vo_d1 = vo;
        List<QkCountVo.AuditflowVo> auditflowList = new ArrayList<>();
        for (String q : query.split(",")) {
            QkCountVo.AuditflowVo vvo = new QkCountVo.AuditflowVo();
            vvo.setAuditflowStep(q);
            auditflowList.add(vvo);
        }
        vo_d1.setAuditflowList(auditflowList);
        QkCountVo vv0 = qkZtMapper.selectYpdtbcfQkZtCount(vo_d1);
        dq_v0.put("v4_number", vv0.getCountNumber());
        dq_v0.put("v4_sum", vv0.getCountSum());

        QkCountVo dq = new QkCountVo();
        dq.setXzqdm(xzqhz.getDistrict());
        dq.setXzqmc("合计");
        dq.setGeom(xzqhz.getGeom());
        dq.setHzjg(dq_v0);
        retQkCountVo.add(dq);
        for (SysDept children : xzqChildrenList) {
            Set<String> xzqSetV1 = selectXzqdmUserList(children.getDeptId());
            vo.setXzqdms(xzqSetV1);
            Map<String, Object> dqV1_v0 = qkZtMapper.selectSqlCount(vo, summary_sql, result_sql);

            vo_d1.setXzqdms(xzqSetV1);
            QkCountVo vv1 = qkZtMapper.selectYpdtbcfQkZtCount(vo_d1);
            dqV1_v0.put("v4_number", vv1.getCountNumber());
            dqV1_v0.put("v4_sum", vv1.getCountSum());

            QkCountVo dqV1 = new QkCountVo();
            dqV1.setXzqdm(children.getDistrict());
            dqV1.setXzqmc(children.getDeptName());
            dqV1.setGeom(children.getGeom());
            dqV1.setHzjg(dqV1_v0);
            retQkCountVo.add(dqV1);
        }
        return retQkCountVo;
    }

    @Override
    public List<QkCountVo> selectpdjgQkZtCountList(QkCountVo vo) {
        Long xzq = null;
        if (StringUtils.isNotEmpty(vo.getXzqdm())) {
            xzq = Long.parseLong(vo.getXzqdm());
        }

        List<SysDept> xzqList = selectXzqdmUseTree(xzq);
        if (xzqList.isEmpty()) {
            throw new ServiceException("未查询到行政区县");
        }

        List<QkCountVo> retQkCountVo = new ArrayList<QkCountVo>();

        SysDept xzqhz = xzqList.get(0);
        List<SysDept> xzqChildrenList = xzqhz.getChildren();

//        t2.step_code = #{audit.auditflowStep} </if>
//             and t2.success_type = #{audit.successType}</if>

        //待举证
        String summary_sql = "1 as v1_number, dkmj::numeric v1_sum," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') then  1  else 0 end v2_number," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') then  dkmj::numeric  else 0 end v2_sum," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='0' then  1  else 0 end v3_number," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='0' then  dkmj::numeric  else 0 end v3_sum," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='1' then  1  else 0 end v4_number," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='1' then  dkmj::numeric  else 0 end v4_sum," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='2' then  1  else 0 end v5_number," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='2' then  dkmj::numeric  else 0 end v5_sum ";

        String result_sql = " coalesce(sum(v1_sum),0) v1_sum,coalesce(sum(v1_number),0) v1_number," +
                "coalesce(sum(v2_sum),0) v2_sum,coalesce(sum(v2_number),0) v2_number," +
                "coalesce(sum(v3_sum),0) v3_sum,coalesce(sum(v3_number),0) v3_number," +
                "coalesce(sum(v4_sum),0) v4_sum,coalesce(sum(v4_number),0) v4_number," +
                "coalesce(sum(v5_sum),0) v5_sum,coalesce(sum(v5_number),0) v5_number ";


        Set<String> xzqSet = selectXzqdmUserList(xzqhz.getDeptId());
        vo.setXzqdms(xzqSet);
        Map<String, Object> dq_v0 = qkZtMapper.selectPdtuSqlCount(vo, summary_sql, result_sql);

        QkCountVo dq = new QkCountVo();
        dq.setXzqdm(xzqhz.getDistrict());
        dq.setXzqmc("合计");
        dq.setGeom(xzqhz.getGeom());
        dq.setHzjg(dq_v0);
        retQkCountVo.add(dq);

        for (SysDept children : xzqChildrenList) {
            Set<String> xzqSetV1 = selectXzqdmUserList(children.getDeptId());
            vo.setXzqdms(xzqSetV1);
            Map<String, Object> dqV1_v0 = qkZtMapper.selectPdtuSqlCount(vo, summary_sql, result_sql);

            QkCountVo dqV1 = new QkCountVo();
            dqV1.setXzqdm(children.getDistrict());
            dqV1.setXzqmc(children.getDeptName());
            dqV1.setGeom(children.getGeom());
            dqV1.setHzjg(dqV1_v0);
            retQkCountVo.add(dqV1);
        }
        return retQkCountVo;
    }

    @Override
    public List<QkCountVo> selectjghzQkZtCountList(QkCountVo vo) {
        Long xzq = null;
        if (StringUtils.isNotEmpty(vo.getXzqdm())) {
            xzq = Long.parseLong(vo.getXzqdm());
        }

        List<SysDept> xzqList = selectXzqdmUseTree(xzq);
        if (xzqList.isEmpty()) {
            throw new ServiceException("未查询到行政区县");
        }
        List<TDictData> hgx_decide_0 = itDictDataService.selectTDictDataTree("hgx_decide:0", null);
        List<TDictData> hgx_decide_1 = itDictDataService.selectTDictDataTree("hgx_decide:1", null);
        List<TDictData> hgx_decide_2 = itDictDataService.selectTDictDataTree("hgx_decide:2", null);

        Map<String, String> key_sm = new HashMap<>();
        List<QkCountVo> retQkCountVo = new ArrayList<QkCountVo>();

        SysDept xzqhz = xzqList.get(0);
        List<SysDept> xzqChildrenList = xzqhz.getChildren();

//        t2.step_code = #{audit.auditflowStep} </if>
//             and t2.success_type = #{audit.successType}</if>

        //待举证
        String summary_sql = "1 as v1_number, dkmj::numeric v1_sum," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') then  1  else 0 end v2_number," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') then  dkmj::numeric  else 0 end v2_sum," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='0' then  1  else 0 end v3_number," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='0' then  dkmj::numeric  else 0 end v3_sum," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='1' then  1  else 0 end v4_number," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='1' then  dkmj::numeric  else 0 end v4_sum," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='2' then  1  else 0 end v5_number," +
                "case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='2' then  dkmj::numeric  else 0 end v5_sum ";
        key_sm.put("v1", "检测图斑");
        key_sm.put("v2", "已判定图斑");
        key_sm.put("v3", "合法用地");
        key_sm.put("v4", "违法用地");
        key_sm.put("v5", "其他用地");
        String result_sql = " coalesce(sum(v1_sum),0) v1_sum,coalesce(sum(v1_number),0) v1_number," +
                "coalesce(sum(v2_sum),0) v2_sum,coalesce(sum(v2_number),0) v2_number," +
                "coalesce(sum(v3_sum),0) v3_sum,coalesce(sum(v3_number),0) v3_number," +
                "coalesce(sum(v4_sum),0) v4_sum,coalesce(sum(v4_number),0) v4_number," +
                "coalesce(sum(v5_sum),0) v5_sum,coalesce(sum(v5_number),0) v5_number ";
        for (TDictData decide : hgx_decide_0) {
            summary_sql += ",case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='0' " +
                    " and decide_type ='" + decide.getDictValue().trim() + "' " +
                    "then  1  else 0 end v3_" + decide.getDictValue().trim() + "_number" +
                    ",case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='0' " +
                    "and decide_type ='" + decide.getDictValue().trim() + "' " +
                    "then  dkmj::numeric  else 0 end v3_" + decide.getDictValue().trim() + "_sum ";
            result_sql += ",coalesce(sum(v3_" + decide.getDictValue().trim() + "_number),0) v3_" + decide.getDictValue().trim() + "_number," +
                    "coalesce(sum(v3_" + decide.getDictValue().trim() + "_sum),0) v3_" + decide.getDictValue().trim() + "_sum";
            key_sm.put("v3_" + decide.getDictValue().trim(), "合法用地情况_" + decide.getDictLabel());
        }
        for (TDictData decide : hgx_decide_1) {
            summary_sql += ",case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='1' " +
                    " and decide_type ='" + decide.getDictValue().trim() + "' " +
                    "then  1  else 0 end v4_" + decide.getDictValue().trim() + "_number" +
                    ",case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='1' " +
                    "and decide_type ='" + decide.getDictValue().trim() + "' " +
                    "then  dkmj::numeric  else 0 end v4_" + decide.getDictValue().trim() + "_sum ";
            result_sql += ",coalesce(sum(v4_" + decide.getDictValue().trim() + "_number),0) v4_" + decide.getDictValue().trim() + "_number," +
                    "coalesce(sum(v4_" + decide.getDictValue().trim() + "_sum),0) v4_" + decide.getDictValue().trim() + "_sum";
            key_sm.put("v4_" + decide.getDictValue().trim(), "违法用地情况_" + decide.getDictLabel());
        }
        for (TDictData decide : hgx_decide_2) {
            summary_sql += ",case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='2' " +
                    " and decide_type ='" + decide.getDictValue().trim() + "' " +
                    "then  1  else 0 end v5_" + decide.getDictValue().trim() + "_number" +
                    ",case when step_code in ('XJTJ','SJSH','SJTJ','SJJSH','SJJTJ','-999') and decide ='2' " +
                    "and decide_type ='" + decide.getDictValue().trim() + "' " +
                    "then  dkmj::numeric  else 0 end v5_" + decide.getDictValue().trim() + "_sum ";
            result_sql += ",coalesce(sum(v5_" + decide.getDictValue().trim() + "_number),0) v5_" + decide.getDictValue().trim() + "_number," +
                    "coalesce(sum(v5_" + decide.getDictValue().trim() + "_sum),0) v5_" + decide.getDictValue().trim() + "_sum";
            key_sm.put("v5_" + decide.getDictValue().trim(), "其他用地情况_" + decide.getDictLabel());
        }

        Set<String> xzqSet = selectXzqdmUserList(xzqhz.getDeptId());
        vo.setXzqdms(xzqSet);
        Map<String, Object> dq_v0 = qkZtMapper.selectPdtuSqlCount(vo, summary_sql, result_sql);
        dq_v0.put("key", key_sm);

        QkCountVo dq = new QkCountVo();
        dq.setXzqdm(xzqhz.getDistrict());
        dq.setXzqmc(xzqhz.getDeptName());
        dq.setGeom(xzqhz.getGeom());
        dq.setHzjg(dq_v0);
        retQkCountVo.add(dq);

        for (SysDept children : xzqChildrenList) {
            Set<String> xzqSetV1 = selectXzqdmUserList(children.getDeptId());
            vo.setXzqdms(xzqSetV1);
            Map<String, Object> dqV1_v0 = qkZtMapper.selectPdtuSqlCount(vo, summary_sql, result_sql);
            dqV1_v0.put("key", key_sm);
            QkCountVo dqV1 = new QkCountVo();
            dqV1.setXzqdm(children.getDistrict());
            dqV1.setXzqmc(children.getDeptName());
            dqV1.setGeom(children.getGeom());
            dqV1.setHzjg(dqV1_v0);
            retQkCountVo.add(dqV1);
        }
        return retQkCountVo;
    }

    @Override
    public List<SysDept> selectXzqdmUseTree(Long checkXzqdm) {
        R<List<SysDept>> r1 = remoteDeptService.childByDeptId(checkXzqdm, true, SecurityConstants.INNER);
        if (R.isError(r1)) {
            throw new ServiceException(r1.getMsg());
        }
        List<SysDept> list = r1.getData();
        if (list.isEmpty()) {
            throw new ServiceException("用户查询失败");
        }
        return list;
    }
}
