package com.unis.module.coding.service.kshzs;

import com.unis.framework.common.pojo.PageResult;
import com.unis.framework.common.util.object.BeanUtils;
import com.unis.module.coding.controller.admin.crkxx.vo.CrkxxPageReqVO;
import com.unis.module.coding.controller.admin.cwxx.vo.CwxxRespVO;
import com.unis.module.coding.controller.admin.cwxxyear.vo.CwxxYearSaveReqVO;
import com.unis.module.coding.controller.admin.kshzs.vo.*;
import com.unis.module.coding.controller.admin.syxx.vo.SyxxPageReqVO;
import com.unis.module.coding.controller.admin.zjxx.vo.ZjxxPageReqVO;
import com.unis.module.coding.dal.dataobject.crkxx.CrkxxDO;
import com.unis.module.coding.dal.dataobject.syxx.SyxxDO;
import com.unis.module.coding.dal.dataobject.zjxx.ZjxxDO;
import com.unis.module.coding.dal.mysql.crkxx.CrkxxMapper;
import com.unis.module.coding.dal.mysql.kshzs.KshzsMapper;
import com.unis.module.coding.dal.mysql.syxx.SyxxMapper;
import com.unis.module.coding.dal.mysql.zjxx.ZjxxMapper;
import com.unis.module.system.dal.dataobject.dict.DictDataDO;
import com.unis.module.system.dal.mysql.dict.DictDataMapper;
import org.ehcache.shadow.org.terracotta.offheapstore.storage.listener.ListenableStorageEngine;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Insight
 * @Description: TODO
 * @Date: 2025/2/11 10:20
 * @Version: 1.0
 */
@Service
@Validated
public class KshzsServiceImpl implements KshzsService{

    @Resource
    private KshzsMapper kshzsMapper;

    @Resource
    private CrkxxMapper crkxxMapper;

    @Resource
    private ZjxxMapper zjxxMapper;

    @Resource
    private SyxxMapper syxxMapper;

    @Resource
    private DictDataMapper dictDataMapper;

    @Override
    public Map<String, Object> selectMapList(paramVo paramVo) {
        Map<String, Object> mobjMap = new HashMap<>();
        List<Map<String, Object>> objData = new ArrayList<>();
        Map<String, Object> objMap = new HashMap<>();
        if(paramVo.getXzqhdm() != null && !"".equals(paramVo.getXzqhdm())) {
            // 省的库点数量
            objData = kshzsMapper.queryList(paramVo);
            // 卡片数据
            objMap = kshzsMapper.selectCardList(paramVo);
        }else{
            if(paramVo.getLspzmc() != null && !"".equals(paramVo.getLspzmc())){
                List<String> lspzmcLst = new ArrayList<>();
                String[] split = paramVo.getLspzmc().split(",");
                for (int i = 0; i < split.length; i++) {
                    lspzmcLst.add((split[i]));
                    paramVo.setLspzmcLst(lspzmcLst);
                }
            }
            if(paramVo.getLsdjdm() != null && !"".equals(paramVo.getLsdjdm())){
                List<String> lsdjdmLst = new ArrayList<>();
                String[] split = paramVo.getLsdjdm().split(",");
                for (int i = 0; i < split.length; i++) {
                    lsdjdmLst.add((split[i]));
                    paramVo.setLsdjdmLst(lsdjdmLst);
                }
            }
            // 各省的库存数量
            objData = kshzsMapper.selectList(paramVo);
            objMap = kshzsMapper.selectCardList(paramVo);
        }
        // 各省的库存数量
        mobjMap.put("kdsl", objData);
        // 卡片数据
        mobjMap.put("cckd", objMap.get("kdsl").toString());
        mobjMap.put("cfsl", objMap.get("cfsl").toString());
        mobjMap.put("crzl", objMap.get("crzl").toString());
        mobjMap.put("ckl", objMap.get("cksl").toString());
        mobjMap.put("rkl", objMap.get("rksl").toString());
        mobjMap.put("kcsl", objMap.get("kcsl").toString());
        return mobjMap;
    }

    @Override
    public totalVo selectFrameList(paramVo paramVo) {
        totalVo objVo = new totalVo();
        Map<String, Object> objMap = new HashMap<>();
        if(paramVo.getKqid()!= null &&!"".equals(paramVo.getKqid())){
            objMap = kshzsMapper.selectCardMapList(paramVo);
            if(objMap != null) {
                objVo.setCfsl(Integer.parseInt(objMap.get("cfsl").toString()));
                objVo.setCrzl(BigDecimal.valueOf(Double.parseDouble(objMap.get("crzl").toString())));
            }
        }else{
            objMap = kshzsMapper.selectCardList(paramVo);
            if(objMap != null){
                objVo.setKdsl(Integer.parseInt(objMap.get("kdsl").toString()));
                objVo.setCfsl(Integer.parseInt(objMap.get("cfsl").toString()));
                objVo.setCrzl(BigDecimal.valueOf(Double.parseDouble(objMap.get("crzl").toString())));
                objVo.setCkl(BigDecimal.valueOf(Double.parseDouble(objMap.get("cksl").toString())));
                objVo.setRkl(BigDecimal.valueOf(Double.parseDouble(objMap.get("rksl").toString())));
                objVo.setZkc(BigDecimal.valueOf(Double.parseDouble(objMap.get("kcsl").toString())));
            }
        }
        List<Map<String, Object>> objListMap = kshzsMapper.queryLspzList(paramVo);
        if(objListMap!= null && objListMap.size()>0){
            for (int i = 0; i < objListMap.size(); i++) {
                if("小麦".equals(objListMap.get(i).get("name").toString())){
                    objVo.setXmkc(BigDecimal.valueOf(Double.parseDouble(objListMap.get(i).get("value").toString())));
                }else if("玉米".equals(objListMap.get(i).get("name").toString())){
                    objVo.setYmkc(BigDecimal.valueOf(Double.parseDouble(objListMap.get(i).get("value").toString())));
                }else if("大豆".equals(objListMap.get(i).get("name").toString())){
                    objVo.setDdkc(BigDecimal.valueOf(Double.parseDouble(objListMap.get(i).get("value").toString())));
                }else if("稻谷".equals(objListMap.get(i).get("name").toString())){
                    objVo.setDgkc(BigDecimal.valueOf(Double.parseDouble(objListMap.get(i).get("value").toString())));
                }
            }
        }
        return objVo;
    }

    @Override
    public List<Map<String, Object>> selectPieChart(paramVo paramVo) {
        float valSum = 0;
        List<Map<String, Object>> mapList = kshzsMapper.selectPieChart(paramVo);
        for (Map<String, Object> map : mapList) {
            float val = Float.valueOf(map.get("value").toString());
            valSum += val;
        }
        for (Map<String, Object> map : mapList) {
            map.put("valSum", valSum);
        }
        return mapList;
    }

    @Override
    public List<cfxxVo> selectCxxList(paramVo paramVo) {
        return kshzsMapper.selectCxxList(paramVo);
    }

    @Override
    public kqxxVo queryKqInfo(paramVo paramVo) {
        kqxxVo vo = kshzsMapper.queryKqInfo(paramVo);
        Map<String,Object> objMap = kshzsMapper.selectCardMapList(paramVo);
        if(objMap != null) {
            vo.setCfsl(Integer.parseInt(objMap.get("cfsl").toString()));
            vo.setCrzl(BigDecimal.valueOf(Double.parseDouble(objMap.get("crzl").toString())));
        }
        return vo;
    }

    @Override
    public List<cwxxVo> selectCwxxList(paramVo paramVo) {
        List<cwxxVo> list = new ArrayList<>();
        cwxxVo vo = kshzsMapper.selectCwxxInfo(paramVo);
        List<CwxxYearSaveReqVO> yearList = kshzsMapper.selectCwxxYearList(vo.getCwxxid());
        if(yearList != null && yearList.size()>0){
            vo.setCwxxYearList(yearList);
        }
        list.add(vo);
        return list;
    }

    @Override
    public cfxxVo selectCfInfo(paramVo paramVo) {
        return kshzsMapper.selectCfInfo(paramVo);
    }

    @Override
    public hwkVo selectHwkInfo(paramVo paramVo) {
        return kshzsMapper.selectHwkInfo(paramVo);
    }

    @Override
    public PageResult<crkVo> selectCrkList(pageParamVo paramVo) {
        // 获取数据字典
        List<DictDataDO> list = dictDataMapper.selectList(DictDataDO::getDictType, "system_lsdj");
        CrkxxPageReqVO reqVO = new CrkxxPageReqVO();
        reqVO.setPageNo(paramVo.getPageNo());
        reqVO.setPageSize(paramVo.getPageSize());
        reqVO.setCfId(Long.valueOf(paramVo.getCfid()));
        PageResult<CrkxxDO> crkxxDOPageResult = crkxxMapper.selectPage(reqVO);
        for (CrkxxDO crkxxDO : crkxxDOPageResult.getList()) {
            if (crkxxDO.getLsdjbm() != null){
                for (DictDataDO dictDataDO : list) {
                    if (crkxxDO.getLsdjbm().equals(dictDataDO.getValue())){
                        crkxxDO.setLsdjbm(dictDataDO.getLabel());
                    }
                }
            }
        }
        return BeanUtils.toBean(crkxxDOPageResult, crkVo.class);
    }

    @Override
    public PageResult<zjxxVo> selectZjList(pageParamVo paramVo) {
        // 获取数据字典
        List<DictDataDO> list = dictDataMapper.selectList(DictDataDO::getDictType, "system_lsdj");
        ZjxxPageReqVO reqVO = new ZjxxPageReqVO();
        reqVO.setPageNo(paramVo.getPageNo());
        reqVO.setPageSize(paramVo.getPageSize());
        reqVO.setCfId(Long.valueOf(paramVo.getCfid()));
        PageResult<ZjxxDO> zjxxDOPageResult = zjxxMapper.selectPage(reqVO);
        for (ZjxxDO zjxxDO : zjxxDOPageResult.getList()) {
            if (zjxxDO.getLsdjbm() != null){
                for (DictDataDO dictDataDO : list) {
                    if (zjxxDO.getLsdjbm().equals(dictDataDO.getValue())){
                        zjxxDO.setLsdjbm(dictDataDO.getLabel());
                    }
                }
            }
        }
        return BeanUtils.toBean(zjxxDOPageResult, zjxxVo.class);
    }

    @Override
    public List<lqjcVo> selectLqjcList(paramVo paramVo) {
        return kshzsMapper.selectLqjcList(paramVo);
    }

    @Override
    public PageResult<sydVo> selectSydList(pageParamVo paramVo) {
        SyxxPageReqVO reqVO = new SyxxPageReqVO();
        reqVO.setPageNo(paramVo.getPageNo());
        reqVO.setPageSize(paramVo.getPageSize());
        reqVO.setCfId(Long.valueOf(paramVo.getCfid()));
        PageResult<SyxxDO> syxxDOPageResult = syxxMapper.selectPage(reqVO);
        return BeanUtils.toBean(syxxDOPageResult, sydVo.class);
    }

    @Override
    public List<kqxxVo> selectKqmcList(paramVo paramVo) {
        return kshzsMapper.selectKqmcList(paramVo);
    }

    @Override
    public List<Map<String, Object>> selectSpList(paramVo paramVo) {
        return kshzsMapper.selectSpList(paramVo);
    }
}
