/*
 * 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 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.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.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 = "";
        Double mzl = 0.7; //折算比例，默认0.7
        if (StringUtils.isNotBlank(crkKcswParam.getLsxz())){ //表示需要进行过滤
            xz = crkKcswParam.getLsxz();
            isFilter = true;
        }

        List<CrkKcswXzDataVO> dfcbList = kcswDefaultMapper.getXzList(xz,orgId);
        for(CrkKcswXzDataVO xzDataVO:dfcbList){
            String orgId_ = xzDataVO.getOrgId();
            CrkKcswDataVO crkKcswDataVO = new CrkKcswDataVO();
            //地方储备数据
            crkKcswDataVO.setOrgName(xzDataVO.getOrgName());
            //小麦
            crkKcswDataVO.setDfcbXmhj(xzDataVO.getXm());
            crkKcswDataVO.setDfcbXmMzl(mzl);
            crkKcswDataVO.setDfcbXmToMf(xzDataVO.getXm()*mzl);
            //青稞
            crkKcswDataVO.setDfcbQkhj(xzDataVO.getQk());
            //稻谷
            crkKcswDataVO.setDfcbDghj(xzDataVO.getDg());
            crkKcswDataVO.setDfcbDgMzl(mzl);
            crkKcswDataVO.setDfcbDgToDm(xzDataVO.getDg()*mzl);
            //合计
            //原粮合计=小表+面折粮+青稞+稻谷+米折粮
            Double dfcbYyhj = crkKcswDataVO.getDfcbXmhj()+crkKcswDataVO.getDfcbXmToMf()+crkKcswDataVO.getDfcbQkhj()+crkKcswDataVO.getDfcbDghj()+crkKcswDataVO.getDfcbDgToDm();
            crkKcswDataVO.setDfcbYyhj(dfcbYyhj);
            crkKcswDataVO.setDfcbLyhj(dfcbYyhj); //粮油合计

            if (isFilter && StringUtils.equals(sj,crkKcswParam.getLsxz())) {
                //省级储备
                List<CrkKcswXzDataVO> sjcbList = kcswDefaultMapper.getXzList(sj, orgId_);
                if (sjcbList.size() > 0) {
                    CrkKcswXzDataVO sjcb = sjcbList.get(0);
                    //小麦
                    crkKcswDataVO.setSjcbXmhj(sjcb.getXm());
                    crkKcswDataVO.setSjcbXmMzl(mzl);
                    crkKcswDataVO.setSjcbXmToMf(sjcb.getXm() * mzl);
                    //青稞
                    crkKcswDataVO.setSjcbQkhj(sjcb.getQk());
                    //稻谷
                    crkKcswDataVO.setSjcbDghj(sjcb.getDg());
                    crkKcswDataVO.setSjcbDgMzl(mzl);
                    crkKcswDataVO.setSjcbDgToDm(sjcb.getDg() * mzl);
                    //合计
                    //原粮合计=小表+面折粮+青稞+稻谷+米折粮
                    Double sjcbYyhj = crkKcswDataVO.getSjcbXmhj() + crkKcswDataVO.getSjcbXmToMf() + crkKcswDataVO.getSjcbQkhj() + crkKcswDataVO.getSjcbDghj() + crkKcswDataVO.getSjcbDgToDm();
                    crkKcswDataVO.setSjcbYyhj(sjcbYyhj);
                    crkKcswDataVO.setSjcbLyhj(sjcbYyhj); //粮油合计
                }
            }
            //市级储备
            if (isFilter && StringUtils.equals(cj,crkKcswParam.getLsxz())) {
                List<CrkKcswXzDataVO> cjcbList = kcswDefaultMapper.getXzList(cj, orgId_);
                if (cjcbList.size() > 0) {
                    CrkKcswXzDataVO cjcb = cjcbList.get(0);
                    //小麦
                    crkKcswDataVO.setCjcbXmhj(cjcb.getXm());
                    crkKcswDataVO.setCjcbXmMzl(mzl);
                    crkKcswDataVO.setCjcbXmToMf(cjcb.getXm() * mzl);
                    //青稞
                    crkKcswDataVO.setCjcbQkhj(cjcb.getQk());
                    //稻谷
                    crkKcswDataVO.setCjcbDghj(cjcb.getDg());
                    crkKcswDataVO.setCjcbDgMzl(mzl);
                    crkKcswDataVO.setCjcbDgToDm(cjcb.getDg() * mzl);
                    //合计
                    //原粮合计=小表+面折粮+青稞+稻谷+米折粮
                    Double cjcbYyhj = crkKcswDataVO.getCjcbXmhj() + crkKcswDataVO.getCjcbXmToMf() + crkKcswDataVO.getCjcbQkhj() + crkKcswDataVO.getCjcbDghj() + crkKcswDataVO.getCjcbDgToDm();
                    crkKcswDataVO.setCjcbYyhj(cjcbYyhj);
                    crkKcswDataVO.setCjcbLyhj(cjcbYyhj); //粮油合计
                }
            }
            //县级储备
            if (isFilter && StringUtils.equals(xj,crkKcswParam.getLsxz())) {
                List<CrkKcswXzDataVO> xjcbList = kcswDefaultMapper.getXzList(xj, orgId_);
                if (xjcbList.size() > 0) {
                    CrkKcswXzDataVO xjcb = xjcbList.get(0);
                    //小麦
                    crkKcswDataVO.setXjcbXmhj(xjcb.getXm());
                    crkKcswDataVO.setXjcbXmMzl(mzl);
                    crkKcswDataVO.setXjcbXmToMf(xjcb.getXm() * mzl);
                    //青稞
                    crkKcswDataVO.setXjcbQkhj(xjcb.getQk());
                    //稻谷
                    crkKcswDataVO.setXjcbDghj(xjcb.getDg());
                    crkKcswDataVO.setXjcbDgMzl(mzl);
                    crkKcswDataVO.setXjcbDgToDm(xjcb.getDg() * mzl);
                    //合计
                    //原粮合计=小表+面折粮+青稞+稻谷+米折粮
                    Double xjcbYyhj = crkKcswDataVO.getXjcbXmhj() + crkKcswDataVO.getXjcbXmToMf() + crkKcswDataVO.getXjcbQkhj() + crkKcswDataVO.getXjcbDghj() + crkKcswDataVO.getXjcbDgToDm();
                    crkKcswDataVO.setXjcbYyhj(xjcbYyhj);
                    crkKcswDataVO.setXjcbLyhj(xjcbYyhj); //粮油合计
                }
            }
            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;
    }
}
