/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.unis.reserveLayout.modular.dataKcglKcswDefault.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.druid.sql.visitor.functions.Char;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.unis.reserveLayout.modular.dataKcglKcswDefault.entity.DataKcglKcswDefault;
import com.unis.reserveLayout.modular.dataKcglKcswDefault.entity.VO.CrkChartDataVO;
import com.unis.reserveLayout.modular.dataKcglKcswDefault.entity.VO.CrkJyxxDataVO;
import com.unis.reserveLayout.modular.dataKcglKcswDefault.entity.VO.CrkJyxxMxDataVO;
import com.unis.reserveLayout.modular.dataKcglKcswDefault.entity.VO.CrkKcswDataVO;
import com.unis.reserveLayout.modular.dataKcglKcswDefault.entity.VO.CrkKcswXzDataVO;
import com.unis.reserveLayout.modular.dataKcglKcswDefault.mapper.KcswDefaultMapper;
import com.unis.reserveLayout.modular.dataKcglKcswDefault.param.CrkCharParam;
import com.unis.reserveLayout.modular.dataKcglKcswDefault.param.CrkKcswParam;
import com.unis.reserveLayout.modular.dataKcglKcswDefault.param.KcswDefaultPageParam;
import com.unis.reserveLayout.modular.dataKcglKcswDefault.service.KcswDefaultService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * 储备布局-出入库信息Service接口实现类
 *
 * @author TS
 * @date  2024/06/12 14:12
 **/
@Service
public class KcswDefaultServiceImpl extends ServiceImpl<KcswDefaultMapper, DataKcglKcswDefault> implements KcswDefaultService {

    @Resource
    private KcswDefaultMapper kcswDefaultMapper;

    /**
     * 获取原粮出入库检验信息分页
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<CrkJyxxDataVO> findJyxxPage(KcswDefaultPageParam pageParam) {
            //条形查询器
            QueryWrapper<CrkJyxxDataVO> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(pageParam.getOrgId())){
            queryWrapper.eq("DATA_CRKYW_RMDJ_DEFAULT.unitId", pageParam.getOrgId());
        }
        if (StringUtils.isNotBlank(pageParam.getBizNo())){
            queryWrapper.like("DATA_CRKYW_RMDJ_DEFAULT.bizNo", pageParam.getBizNo());
        }
        if (StringUtils.isNotBlank(pageParam.getLspzName())){
            queryWrapper.like("b_2.enumName",pageParam.getLspzName());
        }
        if (StringUtils.isNotBlank(pageParam.getJyjg())){
            queryWrapper.like("DATA_CRKYW_JYXX_DEFAULT.jyjg",pageParam.getJyjg());
        }
        if (StringUtils.isNotBlank(pageParam.getCph())){
            queryWrapper.like("DATA_CRKYW_RMDJ_DEFAULT.cph",pageParam.getCph());
        }
        if (StringUtils.isNotBlank(pageParam.getJysjStart()) && StringUtils.isNotBlank(pageParam.getJysjEnd())){
            queryWrapper.apply(" DATE_FORMAT(ifnull(DATA_CRKYW_JYXX_DEFAULT.jysj,NOW()),'%Y-%m-%d') >=  '"+pageParam.getJysjStart()+"' AND DATE_FORMAT(ifnull(DATA_CRKYW_JYXX_DEFAULT.jysj,NOW()),'%Y-%m-%d') <= '"+pageParam.getJysjEnd()+" 23:59:59'");
        }
            Page<CrkJyxxDataVO> p =new Page<>(pageParam.getCurrent(),pageParam.getSize());
            IPage<CrkJyxxDataVO> page = kcswDefaultMapper.findJyxxPage(p,queryWrapper);
            return page;
    }

    /**
     * 获取出入库分析图表数据
     *
     * @param crkCharParam
     * @return
     */
    @Override
    public List<CrkChartDataVO> getCrkChartDatas(CrkCharParam crkCharParam) {
        if (crkCharParam.getType() == 1){ //品种
            return kcswDefaultMapper.getCrkChartDataByPz(crkCharParam);
        }
        //等级
        return kcswDefaultMapper.getCrkChartDataByDj(crkCharParam);
    }

    /**
     * 获取出入库库存相关数据
     *
     * @param crkKcswParam
     * @return
     */
    @Override
    public List<CrkKcswDataVO> getCrkKcswDatas(CrkKcswParam crkKcswParam) {
        List<CrkKcswDataVO> allList= new ArrayList<>();
        //得到地方储备数据
        String xz = "6877, 6878, 6879"; //默认认查询所有
        String sj = "6877"; //省级储备
        String cj = "6878"; //市级储备
        String xj = "6879"; //县级储备
        Boolean isFilter = false; //是否性质条件过滤
        String orgId = "";
        String cityId = crkKcswParam.getCityId();
        Double mzl = 0.7; //折算比例，默认0.7
        if (StringUtils.isNotBlank(crkKcswParam.getLsxz())){ //表示需要进行过滤
            xz = crkKcswParam.getLsxz();
            isFilter = true;
        }

        List<CrkKcswXzDataVO> dfcbList = kcswDefaultMapper.getXzList(xz,orgId,cityId);
        for(CrkKcswXzDataVO xzDataVO:dfcbList){
            String orgId_ = xzDataVO.getOrgId();
            CrkKcswDataVO crkKcswDataVO = new CrkKcswDataVO();
            //地方储备数据
            crkKcswDataVO.setOrgName(xzDataVO.getOrgName());
            //得到地方储备小麦，大米
            CrkKcswXzDataVO dfcbMzlData = kcswDefaultMapper.getMzlDataByxz(xz, orgId_);
            //小麦
            crkKcswDataVO.setDfcbXmhj(xzDataVO.getXm());
            if (dfcbMzlData != null){ //小麦粉
                crkKcswDataVO.setDfcbXmToMf(dfcbMzlData.getXmf());
                Double  dfcbXmfMzl = NumberUtil.round((dfcbMzlData.getXmf()/mzl),2).doubleValue();
                crkKcswDataVO.setDfcbXmMzl(dfcbXmfMzl);
            }
            //青稞
            crkKcswDataVO.setDfcbQkhj(xzDataVO.getQk());
            //稻谷
            crkKcswDataVO.setDfcbDghj(xzDataVO.getDg());
            if (dfcbMzlData != null){ //大米
                crkKcswDataVO.setDfcbDgToDm(dfcbMzlData.getDm());
                Double  dfcDmMzl = NumberUtil.round((dfcbMzlData.getDm()/mzl),2).doubleValue();
                crkKcswDataVO.setDfcbDgMzl(dfcDmMzl);
            }
            //合计
            //原粮合计=小表+面折粮+青稞+稻谷+米折粮
            Double dfcbYyhj = crkKcswDataVO.getDfcbXmhj()+crkKcswDataVO.getDfcbXmMzl()+crkKcswDataVO.getDfcbQkhj()+crkKcswDataVO.getDfcbDghj()+crkKcswDataVO.getDfcbDgMzl();
            crkKcswDataVO.setDfcbYyhj(dfcbYyhj);
            //菜籽油
            CrkKcswXzDataVO dfcbCzyData = kcswDefaultMapper.getCzyDataByxz(xz, orgId_);
            if (dfcbCzyData != null) {
                crkKcswDataVO.setDfcbCzyhj(dfcbCzyData.getCzy());
            }
            crkKcswDataVO.setDfcbLyhj(dfcbYyhj+crkKcswDataVO.getDfcbCzyhj()); //粮油合计

            if (!isFilter ||(isFilter  && StringUtils.equals(sj,crkKcswParam.getLsxz()))) {
                //省级储备
                //原粮：小麦，青稞，稻谷
                List<CrkKcswXzDataVO> sjcbList = kcswDefaultMapper.getXzList(sj, orgId_,cityId);
                if (sjcbList.size() > 0) {
                    CrkKcswXzDataVO sjcb = sjcbList.get(0);
                    //得到省级小麦，大米
                    CrkKcswXzDataVO sjcbMzlData = kcswDefaultMapper.getMzlDataByxz(sj, orgId_);
                    //小麦
                    crkKcswDataVO.setSjcbXmhj(sjcb.getXm());
                    if (sjcbMzlData != null){ //小麦粉
                        crkKcswDataVO.setSjcbXmToMf(sjcb.getXmf());
                        Double  sjcbXmMzl = NumberUtil.round((sjcbMzlData.getXmf()/mzl),2).doubleValue();
                        crkKcswDataVO.setSjcbXmMzl(sjcbXmMzl);
                    }
                    //青稞
                    crkKcswDataVO.setSjcbQkhj(sjcb.getQk());
                    //稻谷
                    crkKcswDataVO.setSjcbDghj(sjcb.getDg());
                    if (sjcbMzlData != null){ //稻谷
                        crkKcswDataVO.setSjcbDgToDm(sjcb.getDm());
                        Double  sjcbDgMzl = NumberUtil.round((sjcbMzlData.getDm()/mzl),2).doubleValue();
                        crkKcswDataVO.setSjcbDgMzl(sjcbDgMzl);
                    }
                    crkKcswDataVO.setSjcbDgToDm(sjcb.getDg() * mzl);
                    //合计
                    //原粮合计=小表+面折粮+青稞+稻谷+米折粮
                    Double sjcbYyhj = crkKcswDataVO.getSjcbXmhj() + crkKcswDataVO.getSjcbXmMzl() + crkKcswDataVO.getSjcbQkhj() + crkKcswDataVO.getSjcbDghj() + crkKcswDataVO.getSjcbDgMzl();
                    crkKcswDataVO.setSjcbYyhj(sjcbYyhj);
                }
                //菜籽油
                CrkKcswXzDataVO sjcbCzyData = kcswDefaultMapper.getCzyDataByxz(sj, orgId_);
                if (sjcbCzyData != null) {
                    crkKcswDataVO.setSjcbCzyhj(sjcbCzyData.getCzy());
                }
                crkKcswDataVO.setSjcbLyhj(crkKcswDataVO.getSjcbYyhj()+crkKcswDataVO.getSjcbCzyhj()); //粮油合计
            }
            //市级储备
            if (!isFilter ||(isFilter  && StringUtils.equals(cj,crkKcswParam.getLsxz()))) {
                //原粮：小麦，青稞，稻谷
                List<CrkKcswXzDataVO> cjcbList = kcswDefaultMapper.getXzList(cj, orgId_,cityId);
                if (cjcbList.size() > 0) {
                    CrkKcswXzDataVO cjcb = cjcbList.get(0);
                    //得到市级小麦，大米
                    CrkKcswXzDataVO cjcbMzlData = kcswDefaultMapper.getMzlDataByxz(cj, orgId_);
                    //小麦
                    crkKcswDataVO.setCjcbXmhj(cjcb.getXm());
                    if (cjcbMzlData != null){ //小麦粉
                        crkKcswDataVO.setCjcbXmToMf(cjcb.getXmf());
                        Double  cjcbXmMzl = NumberUtil.round((cjcbMzlData.getXmf()/mzl),2).doubleValue();
                        crkKcswDataVO.setCjcbXmMzl(cjcbXmMzl);
                    }
                    //青稞
                    crkKcswDataVO.setCjcbQkhj(cjcb.getQk());
                    //稻谷
                    crkKcswDataVO.setCjcbDghj(cjcb.getDg());
                    if (cjcbMzlData != null){ //稻谷
                        crkKcswDataVO.setCjcbDgToDm(cjcb.getDm());
                        Double  cjcbDgMzl = NumberUtil.round((cjcbMzlData.getDm()/mzl),2).doubleValue();
                        crkKcswDataVO.setCjcbDgMzl(cjcbDgMzl);
                    }
                    //合计
                    //原粮合计=小表+面折粮+青稞+稻谷+米折粮
                    Double cjcbYyhj = crkKcswDataVO.getCjcbXmhj() + crkKcswDataVO.getCjcbXmMzl() + crkKcswDataVO.getCjcbQkhj() + crkKcswDataVO.getCjcbDghj() + crkKcswDataVO.getCjcbDgMzl();
                    crkKcswDataVO.setCjcbYyhj(cjcbYyhj);
                }
                //菜籽油
                CrkKcswXzDataVO cjcbCzyData = kcswDefaultMapper.getCzyDataByxz(cj, orgId_);
                if (cjcbCzyData != null) {
                    crkKcswDataVO.setCjcbCzyhj(cjcbCzyData.getCzy());
                }
                crkKcswDataVO.setCjcbLyhj(crkKcswDataVO.getCjcbYyhj()+crkKcswDataVO.getCjcbCzyhj()); //粮油合计
            }
            //县级储备
            if (!isFilter ||(isFilter  && StringUtils.equals(xj,crkKcswParam.getLsxz()))) {
                //原粮：小麦，青稞，稻谷
                List<CrkKcswXzDataVO> xjcbList = kcswDefaultMapper.getXzList(xj, orgId_,cityId);
                if (xjcbList.size() > 0) {
                    CrkKcswXzDataVO xjcb = xjcbList.get(0);
                    //得到县级小麦，大米
                    CrkKcswXzDataVO xjcbMzlData = kcswDefaultMapper.getMzlDataByxz(xj, orgId_);
                    //小麦
                    crkKcswDataVO.setXjcbXmhj(xjcb.getXm());

                    if (xjcbMzlData != null){ //小麦粉
                        crkKcswDataVO.setXjcbXmToMf(xjcb.getXmf());
                        Double  xjcbXmMzl = NumberUtil.round((xjcbMzlData.getXmf()/mzl),2).doubleValue();
                        crkKcswDataVO.setXjcbXmMzl(xjcbXmMzl);
                    }
                    //青稞
                    crkKcswDataVO.setXjcbQkhj(xjcb.getQk());
                    //稻谷
                    crkKcswDataVO.setXjcbDghj(xjcb.getDg());
                    crkKcswDataVO.setXjcbDgMzl(mzl);
                    if (xjcbMzlData != null){ //稻谷
                        crkKcswDataVO.setXjcbDgToDm(xjcb.getDm());
                        Double  xjcbDgMzl = NumberUtil.round((xjcbMzlData.getDm()/mzl),2).doubleValue();
                        crkKcswDataVO.setXjcbDgMzl(xjcbDgMzl);
                    }
                    //合计
                    //原粮合计=小表+面折粮+青稞+稻谷+米折粮
                    Double xjcbYyhj = crkKcswDataVO.getXjcbXmhj() + crkKcswDataVO.getXjcbXmMzl() + crkKcswDataVO.getXjcbQkhj() + crkKcswDataVO.getXjcbDghj() + crkKcswDataVO.getXjcbDgMzl();
                    crkKcswDataVO.setXjcbYyhj(xjcbYyhj);
                }
                //菜籽油
                CrkKcswXzDataVO xjcbCzyData = kcswDefaultMapper.getCzyDataByxz(xj, orgId_);
                if (xjcbCzyData != null) {
                    crkKcswDataVO.setXjcbCzyhj(xjcbCzyData.getCzy());
                }
                crkKcswDataVO.setXjcbLyhj( crkKcswDataVO.getXjcbYyhj()+crkKcswDataVO.getXjcbCzyhj()); //粮油合计
            }
            allList.add(crkKcswDataVO);

        }
        return allList;
    }

    /**
     * 获取原粮出入库检验信息详情
     *
     * @param dataId
     * @return
     */
    @Override
    public CrkJyxxDataVO getJyxxDetail(String dataId) {
        CrkJyxxDataVO data = kcswDefaultMapper.getJyxxDetail(dataId);
        if (data != null){
            //得到检验明细数据
            List<CrkJyxxMxDataVO> jyxxMxList =kcswDefaultMapper.getJyxxMxDetail(dataId);
            data.setJyxxMxList(jyxxMxList);
        }
        return data;
    }
}
