package com.ruoyi.web.controller.demo.controller;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IOrhonFgqyService;
import com.ruoyi.system.service.IOrhonGtxxService;
import com.ruoyi.system.service.IOrhonShzzService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.web.controller.demo.domain.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报表控制器
 * @author ebusu
 * @date 2020/08/05
 */
@Controller
@RequestMapping("/report")
public class ReportController extends com.ruoyi.common.core.controller.BaseController{

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private IOrhonFgqyService orhonFgqyService;

    @Autowired
    private IOrhonGtxxService orhonGtxxService;

    @Autowired
    private IOrhonShzzService orhonShzzService;
    /**
     * 第一个报表
     * 非公企业"两个覆盖"专项行动摸底排查情况统计表
     */
    @PostMapping("/one")
    @ResponseBody
    public TableDataInfo reportOne(){
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("orhon_qyfl");
        List<SysDictData> areaList = this.dictDataService.selectDictDataList(sysDictData);
        TableDataInfo rspData = new TableDataInfo();
        List<OrhonFgqy> allFgqy = this.orhonFgqyService.selectOrhonFgqyList(new OrhonFgqy());
        Map<Long, ReportOneResult> qiquMap = new HashMap<>(12);

        for (OrhonFgqy orhonFgqy : allFgqy) {
            ReportOneResult tmpResult = null;
            if ( qiquMap.get(orhonFgqy.getDeptId()) == null) {
                tmpResult = new ReportOneResult();
                this.initResult(tmpResult);
                qiquMap.put(orhonFgqy.getDeptId(), tmpResult);
            } else {
                tmpResult = qiquMap.get(orhonFgqy.getDeptId());
            }
            tmpResult.setDwl(tmpResult.getDwl() + 1);
            if ( orhonFgqy.getType().equals("1")) {
                tmpResult.setGmysfgqy(tmpResult.getGmysfgqy() + 1);
            } else if ( orhonFgqy.getType().equals("2")) {
                tmpResult.setGmyxfgqy(tmpResult.getGmyxfgqy() + 1);
            }

            if ( orhonFgqy.getIsdzz() != null && orhonFgqy.getIsdzz().equals("1")) {
                // 已建立党组织
                tmpResult.setSl(tmpResult.getSl() + 1);
                // tmpResult.setDdjldzzdfgqy(tmpResult.getDdjldzzdfgqy() + 1);
                if("1".equals(orhonFgqy.getIssqlh())) {
                    tmpResult.setDdsqlhdfgqy(tmpResult.getDdsqlhdfgqy() + 1);
                }
                if ( "1".equals(orhonFgqy.getIsgh())) {
                    tmpResult.setYjlghzzdfgqy(tmpResult.getYjlghzzdfgqy() + 1);
                }

                try {
                    tmpResult.setDys(Integer.parseInt(orhonFgqy.getDys()) + tmpResult.getDys());
                } catch (NumberFormatException e ) {
                    // 数据问题先不管
                }

                try {
                    tmpResult.setCyrysl( Integer.parseInt( orhonFgqy.getCyrs()) + tmpResult.getCyrysl());
                } catch (NumberFormatException e ) {
                    // 数据问题先不管
                }

                try {
                    tmpResult.setNnfzdydfgqy ( Integer.parseInt( orhonFgqy.getNnfzdys()) + tmpResult.getNnfzdydfgqy());
                } catch (NumberFormatException e ) {
                    // 数据问题先不管
                }

                try {
//                    tmpResult.setFzdys( Integer.parseInt( orhonFgqy.getFzdxsl()) + tmpResult.getFzdys());
                } catch (NumberFormatException e ) {
                    // 数据问题先不管
                }

                if ( "1".equals(orhonFgqy.getDzzlx()) ) {
                    // 党委
                    tmpResult.setDws(tmpResult.getDws() + 1);
                } else if ( "2".equals(orhonFgqy.getDzzlx()) ) {
                    // 党支部
                    tmpResult.setDzbs(tmpResult.getDzbs() + 1);
                } if ( "3".equals(orhonFgqy.getDzzlx()) ) {
                    // 党总支
                    tmpResult.setDzzs(tmpResult.getDzzs() + 1);
                }

                if ( "1".equals(orhonFgqy.getIshdzd())) {
                    // 单独建立
                    tmpResult.setDdjl(tmpResult.getDdjl() + 1);
                } else if ( "2".equals(orhonFgqy.getIshdzd())) {
                    // 公用阵地  != 联合建立
                    // tmpResult.setG
                }



            } else {
                // 空或者不等于1的统统为未建立
            }

        }
        List<ReportOneResult> reportOneResultList = new ArrayList<>(areaList.size());
        for (SysDictData dictData : areaList) {
            ReportOneResult result = qiquMap.get(Long.parseLong(dictData.getDictValue()));
            result.setAreaName(dictData.getDictLabel());

            reportOneResultList.add(result);
        }

        rspData.setRows(reportOneResultList);
        rspData.setTotal(reportOneResultList.size());
        return rspData;
    }
    /**
     * 第2个报表
     * 社会组织“两个覆盖”专项行动摸底排查情况统计表
     */
    @PostMapping("/two")
    @ResponseBody
    public TableDataInfo reporttwo(){
        OrhonShzz orhonShzz=new OrhonShzz();
        SysUser sysUser = ShiroUtils.getSysUser();
        startPage();
        if ( !sysUser.getLoginName().equals("admin") ) {
            orhonShzz.setDeptId(sysUser.getUserDeptId().longValue());
        }
        java.util.List<com.ruoyi.system.domain.OrhonShzz> list = orhonShzzService.selectOrhonShzzList(orhonShzz);
        List<ReportTwoResult>  listResult=new ArrayList<>();
        //社会组织“两个覆盖”专项行动摸底排查情况统计表  查询条件




        return getDataTable(listResult);
    }

    private void initResult(ReportOneResult tmpResult) {
        tmpResult.setDwl(0);
        tmpResult.setSl(0);
        tmpResult.setDdjldzzdfgqy(0);
        tmpResult.setDdsqlhdfgqy(0);
        tmpResult.setYjlghzzdfgqy(0);
        tmpResult.setYjlgqtzzdfgqy(0);
        tmpResult.setYjlfnjczzdfgqy(0);
        tmpResult.setDjzdyhdjzdzlxdfgqy(0);
        tmpResult.setYgdhdcsdfgqy(0);
        tmpResult.setDdjl(0);
        tmpResult.setLhjl(0);
        tmpResult.setDws(0);
        tmpResult.setDzzs(0);
        tmpResult.setDzbs(0);
        tmpResult.setBndxjldzzdfgqy(0);
        tmpResult.setBndxjlddzzs(0);
        tmpResult.setCyrysl(0);
        tmpResult.setDys(0);
        tmpResult.setNnfzdydfgqy(0);
        tmpResult.setFzdys(0);
        tmpResult.setXpdzgzzdys(0);

        tmpResult.setWeiydydfgqy(0);
        tmpResult.setWeidjzdyhdjzdzlxdfgqy(0);
        tmpResult.setWeiyjlgqtzzdfgqy(0);
        tmpResult.setWeiyjlghzzdfgqy(0);
        tmpResult.setWeiyjlfnjczzdfgqy(0);
        tmpResult.setWeihyzgbmzddfgqy(0);
        tmpResult.setWeismxzhjdsqdzzlxdfgqy(0);
        tmpResult.setWeicyrs(0);
        tmpResult.setWeidys(0);
        tmpResult.setWeinnfzdys(0);
        tmpResult.setWeixpdjgzzdys(0);

        tmpResult.setGmysfgqy(0);
        tmpResult.setGmyxfgqy(0);
    }
    /**
     * 第3个报表
     * 已建立党组织的非公企业台账
     */
    @PostMapping("/three")
    @ResponseBody
    public TableDataInfo reportThree(){
        OrhonFgqy orhonFgqy=new OrhonFgqy();
        SysUser sysUser = ShiroUtils.getSysUser();
        startPage();
        if ( !sysUser.getLoginName().equals("admin") ) {
            orhonFgqy.setDeptId(sysUser.getUserDeptId().longValue());
        }
        //已建立党组织的非公企业台账  查询条件
        orhonFgqy.setIsdzz("1");
        java.util.List<com.ruoyi.system.domain.OrhonFgqy> list = orhonFgqyService.selectOrhonFgqyList(orhonFgqy);
        return getDataTable(list);
    }/**
     * 第4个报表
     * 已建立党组织的社会组织台账
     */
    @PostMapping("/four")
    @ResponseBody
    public TableDataInfo reportFour(){
        OrhonShzz orhonShzz=new OrhonShzz();
        SysUser sysUser = ShiroUtils.getSysUser();
        startPage();
        if ( !sysUser.getLoginName().equals("admin") ) {
            orhonShzz.setDeptId(sysUser.getUserDeptId().longValue());
        }
        //已建立党组织的社会组织台账  查询条件
        orhonShzz.setIsdzz("1");

        java.util.List<com.ruoyi.system.domain.OrhonShzz> list = orhonShzzService.selectOrhonShzzList(orhonShzz);
        return getDataTable(list);
    }/**
     * 第5个报表
     * 已建立党组织的个体户台账
     */
    @PostMapping("/five")
    @ResponseBody
    public TableDataInfo reportFive(){
        OrhonGtxx orhonGtxx=new OrhonGtxx();
        SysUser sysUser = ShiroUtils.getSysUser();
        startPage();
        if ( !sysUser.getLoginName().equals("admin") ) {
            orhonGtxx.setDeptId(sysUser.getUserDeptId());
        }
        //已建立党组织的个体户台账  查询条件
        orhonGtxx.setIsdzz("1");

        List<OrhonGtxx> list = orhonGtxxService.selectOrhonGtxxList(orhonGtxx);
        return getDataTable(list);
    }
    /**
     * 第6个报表
     * 僵尸空壳影子非公企业台账
     */
    @PostMapping("/six")
    @ResponseBody
    public TableDataInfo reportSix(){
        OrhonFgqy orhonFgqy=new OrhonFgqy();
        SysUser sysUser = ShiroUtils.getSysUser();
        startPage();
        if ( !sysUser.getLoginName().equals("admin") ) {
            orhonFgqy.setDeptId(sysUser.getUserDeptId().longValue());
        }
        //僵尸空壳影子非公企业台账  查询条件

        java.util.List<com.ruoyi.system.domain.OrhonFgqy> list = orhonFgqyService.selectOrhonFgqyList(orhonFgqy);
        return getDataTable(list);
    }
    /**
     * 第7个报表
     * 休眠社会组织台账
     */
    @PostMapping("/seven")
    @ResponseBody
    public TableDataInfo reportSeven(){
        OrhonShzz orhonShzz=new OrhonShzz();
        SysUser sysUser = ShiroUtils.getSysUser();
        startPage();
        if ( !sysUser.getLoginName().equals("admin") ) {
            orhonShzz.setDeptId(sysUser.getUserDeptId().longValue());
        }
        //休眠社会组织台账  查询条件


        java.util.List<com.ruoyi.system.domain.OrhonShzz> list = orhonShzzService.selectOrhonShzzList(orhonShzz);
        return getDataTable(list);
    }
    /**
     * 第8个报表
     * 非公企业及个体户党组织情况台账
     */
    @PostMapping("/eight")
    @ResponseBody
    public TableDataInfo reportEight(){
        com.ruoyi.system.domain.OrhonFgqy orhonFgqy=new OrhonFgqy();
        OrhonGtxx orhonGtxx=new OrhonGtxx();
        List<ReportEightResult>  list=new ArrayList<>();
        SysUser sysUser = ShiroUtils.getSysUser();
        startPage();
        if ( !sysUser.getLoginName().equals("admin") ) {
            orhonFgqy.setDeptId(sysUser.getUserDeptId().longValue());
            orhonGtxx.setDeptId(sysUser.getUserDeptId());
        }
        java.util.List<com.ruoyi.system.domain.OrhonFgqy> listOrhonFgqy = orhonFgqyService.selectOrhonFgqyList(orhonFgqy);
        List<ReportEightResult>  list1=new ArrayList<>();
        for(OrhonFgqy f:listOrhonFgqy){
            ReportEightResult r=new ReportEightResult();
            BeanUtils.copyProperties(f, r);
            list1.add(r);
        }
        BeanUtils.copyProperties(listOrhonFgqy, list1);
        List<OrhonGtxx> listOrhonGtxx = orhonGtxxService.selectOrhonGtxxList(orhonGtxx);
        List<ReportEightResult>  list2=new ArrayList<>();
        for(OrhonGtxx gtxx:listOrhonGtxx){
            ReportEightResult r=new ReportEightResult();
            r.setMc(gtxx.getZhmc());
            r.setIsdzz(gtxx.getIsdzz());
            r.setDzzjlsj(gtxx.getDzzjlsj());
            r.setDzzmc(gtxx.getDzzmc());
            r.setDzzlx(gtxx.getDzzlx());
            r.setDzzsdgx(gtxx.getDzzsdgx());
            r.setSjdzz(gtxx.getSjdzz());
            r.setIsgh(gtxx.getIsgh());
            r.setIstzz(gtxx.getIstzz());
            r.setIsjljcfnzz(gtxx.getIsjljcfnzz());
            r.setHjxx(gtxx.getHjxx());
            r.setDfzh(gtxx.getDfzh());
            r.setSzd(gtxx.getJycs());
            r.setLxdhjczh(gtxx.getLxdhjczh());
            list2.add(r);
        }
        list.addAll(list1);
        list.addAll(list2);
        return getDataTable(list);
    }
    /**
     * 第12个报表
     * 非公企业及个体户党建工作保障情况台账
     */
    @PostMapping("/twelve")
    @ResponseBody
    public TableDataInfo reportTwelve(){
        com.ruoyi.system.domain.OrhonFgqy orhonFgqy=new OrhonFgqy();
        OrhonGtxx orhonGtxx=new OrhonGtxx();
        List<ReportTwelveResult>  list=new ArrayList<>();
        SysUser sysUser = ShiroUtils.getSysUser();
        startPage();
        if ( !sysUser.getLoginName().equals("admin") ) {
            orhonFgqy.setDeptId(sysUser.getUserDeptId().longValue());
            orhonGtxx.setDeptId(sysUser.getUserDeptId());
        }
        java.util.List<com.ruoyi.system.domain.OrhonFgqy> listOrhonFgqy = orhonFgqyService.selectOrhonFgqyList(orhonFgqy);
        List<ReportTwelveResult>  list1=new ArrayList<>();
        for(OrhonFgqy f:listOrhonFgqy){
            ReportTwelveResult r=new ReportTwelveResult();
            BeanUtils.copyProperties(f, r);
            list1.add(r);
        }
        List<OrhonGtxx> listOrhonGtxx = orhonGtxxService.selectOrhonGtxxList(orhonGtxx);
        List<ReportTwelveResult>  list2=new ArrayList<>();
        for(OrhonGtxx gtxx:listOrhonGtxx){
            ReportTwelveResult r=new ReportTwelveResult();
            r.setMc(gtxx.getZhmc());
            r.setYwdy(gtxx.getYwdy());
            r.setDys(gtxx.getDys());
            r.setZzdysl(gtxx.getZzdysl());
            r.setYbdysl(gtxx.getYbdysl());
            r.setLddysl(gtxx.getLddysl());
            r.setSlhkddysl(gtxx.getSlhkddysl());
            r.setJjfzsl(gtxx.getJjfzsl());
            r.setFzdxsl(gtxx.getFzdxsl());
            r.setNnfzdys(gtxx.getNnfzdys());
            r.setDfsjbz(gtxx.getDfsjbz());
            list2.add(r);
        }
        list.addAll(list1);
        list.addAll(list2);
        return getDataTable(list);
    }
    /**
     * 第14个报表
     * 非公企业及个体户党建工作保障情况台账
     */
    @PostMapping("/fourteen")
    @ResponseBody
    public TableDataInfo reportFourteen(){
        com.ruoyi.system.domain.OrhonFgqy orhonFgqy=new OrhonFgqy();
        OrhonGtxx orhonGtxx=new OrhonGtxx();
        List<ReportFourteenResult>  list=new ArrayList<>();
        SysUser sysUser = ShiroUtils.getSysUser();
        startPage();
        if ( !sysUser.getLoginName().equals("admin") ) {
            orhonFgqy.setDeptId(sysUser.getUserDeptId().longValue());
            orhonGtxx.setDeptId(sysUser.getUserDeptId());
        }
        java.util.List<com.ruoyi.system.domain.OrhonFgqy> listOrhonFgqy = orhonFgqyService.selectOrhonFgqyList(orhonFgqy);
        List<ReportFourteenResult>  list1=new ArrayList<>();
        for(OrhonFgqy f:listOrhonFgqy){
            ReportFourteenResult r=new ReportFourteenResult();
            BeanUtils.copyProperties(f, r);
            list1.add(r);
        }
        List<OrhonGtxx> listOrhonGtxx = orhonGtxxService.selectOrhonGtxxList(orhonGtxx);
        List<ReportFourteenResult>  list2=new ArrayList<>();
        for(OrhonGtxx gtxx:listOrhonGtxx){
            ReportFourteenResult r=new ReportFourteenResult();
            r.setMc(gtxx.getZhmc());
            r.setIshdzd(gtxx.getIshdzd());
            r.setHdzdmj(gtxx.getHdzdmj());
            r.setDjjf(gtxx.getDjjf());
            r.setSjbfe(gtxx.getSjbfe());
            r.setBjppe(gtxx.getBjppe());
            r.setSfpdjzdy(gtxx.getSfpdjzdy());
            r.setDjzdyxm(gtxx.getDjzdyxm());
            r.setGzdwjzw(gtxx.getGzdwjzw());
            r.setLxdhjczh(gtxx.getLxdhjczh());
            list2.add(r);
        }
        list.addAll(list1);
        list.addAll(list2);
        return getDataTable(list);
    }
    /**
     * 第16个报表
     * 非公企业及个体户党建工作特色情况台账
     */
    @PostMapping("/sixteen")
    @ResponseBody
    public TableDataInfo reportSixteen(){
        com.ruoyi.system.domain.OrhonFgqy orhonFgqy=new OrhonFgqy();
        OrhonGtxx orhonGtxx=new OrhonGtxx();
        List<ReportSixteenResult>  list=new ArrayList<>();
        SysUser sysUser = ShiroUtils.getSysUser();
        startPage();
        if ( !sysUser.getLoginName().equals("admin") ) {
            orhonFgqy.setDeptId(sysUser.getUserDeptId().longValue());
            orhonGtxx.setDeptId(sysUser.getUserDeptId());
        }
        java.util.List<com.ruoyi.system.domain.OrhonFgqy> listOrhonFgqy = orhonFgqyService.selectOrhonFgqyList(orhonFgqy);
        List<ReportSixteenResult>  list1=new ArrayList<>();
        for(OrhonFgqy f:listOrhonFgqy){
            ReportSixteenResult r=new ReportSixteenResult();
            BeanUtils.copyProperties(f, r);
            list1.add(r);
        }
        List<OrhonGtxx> listOrhonGtxx = orhonGtxxService.selectOrhonGtxxList(orhonGtxx);
        List<ReportSixteenResult>  list2=new ArrayList<>();
        for(OrhonGtxx gtxx:listOrhonGtxx){
            ReportSixteenResult r=new ReportSixteenResult();
            r.setMc(gtxx.getZhmc());
            r.setIssqlh(gtxx.getIssqlh());
            r.setQdjb(gtxx.getQdjb());
            r.setQdsj(gtxx.getQdsj());
            r.setXjpdjg(gtxx.getXjpdjg());
            r.setJcqk(gtxx.getJcqk());
            list2.add(r);
        }
        list.addAll(list1);
        list.addAll(list2);
        return getDataTable(list);
    }
}
