/*
 * 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.statisticalAnalysis.modular.kcsw.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.unis.rotationPlan.info.modular.rpBiweeklyReport.mapper.RpBiweeklyReportMapper;
import com.unis.rotationPlan.info.modular.rpBiweeklyReport.param.RpBiweeklyReportPageParam;
import com.unis.rotationPlan.info.modular.rpBiweeklyReport.service.impl.RpBiweeklyReportServiceImpl;
import com.unis.statisticalAnalysis.modular.kcsw.mapper.KcswMapper;
import com.unis.statisticalAnalysis.modular.kcsw.param.KcswPageParam;
import com.unis.statisticalAnalysis.modular.kcsw.service.KcswService;
import org.springframework.stereotype.Service;

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


/**
 * 维保管理
 *
 * @author shine
 * @date  2024/05/30 17:34
 **/

@Service
public class KcswServiceImpl<KcswServiceMapper> implements KcswService {

    @Resource
    private KcswMapper kcswMapper;

    @Resource
    private RpBiweeklyReportServiceImpl rpBiweeklyReportServiceImpl;

    @Override
    public IPage<List<Map<String, Object>>> kcswpage(KcswPageParam kcswPageParam) {
        return kcswMapper.kcswpage(new Page<>(kcswPageParam.getCurrent(), kcswPageParam.getSize()), kcswPageParam);
    }

    @Override
    public Map<String, Object> lhjhScale(Integer orgId) {
        RpBiweeklyReportPageParam pageParam = new RpBiweeklyReportPageParam();
        pageParam.setCurrent(1);
        pageParam.setSize(10);
        //当前年
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        pageParam.setRotationYear(year+"");
        if(null != orgId){
            pageParam.setOrgId(orgId);
        }
        IPage<Map<String, Object>> pageList = rpBiweeklyReportServiceImpl.findPlanPage(pageParam);
        Map<String,Object> map = new HashMap<String,Object>();
        if(pageList.getRecords().size()>0){
            //出 入 计划的数量
            BigDecimal outPlanQuantityBig = new BigDecimal( pageList.getRecords().get(0).get("outPlanQuantity").toString());
            BigDecimal inPlanQuantityBig = new BigDecimal( pageList.getRecords().get(0).get("inPlanQuantity").toString());

            //实际完成的数量
            BigDecimal ckslSumBig = new BigDecimal( pageList.getRecords().get(0).get("ckslSum").toString());
            BigDecimal rkslSumBig = new BigDecimal( pageList.getRecords().get(0).get("rkslSum").toString());


            /*BigDecimal percentage = (ckslSumBig.add(rkslSumBig)).divide(outPlanQuantityBig.add(inPlanQuantityBig),2, BigDecimal.ROUND_HALF_UP)
                                 .multiply(BigDecimal.valueOf(100));

            BigDecimal wwc = new BigDecimal(100).subtract(percentage);
            map.put("ywc",percentage+"%");
            map.put("wwc",wwc+"%");*/

            //制定计划数量
            BigDecimal jhsl = outPlanQuantityBig.add(inPlanQuantityBig);
            //完成数量
            BigDecimal sjywc = ckslSumBig.add(rkslSumBig);
            //未完成数量
            BigDecimal wwc = jhsl.subtract(sjywc);
            map.put("已完成数量",sjywc);
            map.put("未完成数量",wwc);
        }
        return map;
    }

    @Override
    public List<Map<String,Object>> qualityStatistics(Integer orgId) {
        return kcswMapper.qualityStatistics(orgId);
    }

    @Override
    public List<Map<String, Object>> grainPropertyStatistics(Integer orgId) {
        return kcswMapper.grainPropertyStatistics(orgId);
    }

    @Override
    public List<Map<String, Object>> numberOfWarning(Integer orgId) {
        return kcswMapper.yjcs(orgId);
    }

    @Override
    public List<Map<String, Object>> quantity() {
        return kcswMapper.quantity();
    }

    @Override
    public List<Map<String, Object>> inventoryRatio() {
        return kcswMapper.inventoryRatio();
    }

    @Override
    public List<Map<String, Object>> grainStorageStatistics() {
        List<Map<String, Object>> list = kcswMapper.grainStorageStatistics();
        list.get(0).put("zzl",0);
        for(int i=0 ;i< list.size()-1;i++){
            BigDecimal percentage = new BigDecimal(list.get(i+1).get("sl").toString()).subtract(new BigDecimal(list.get(i).get("sl").toString()))
                    .divide(new BigDecimal(list.get(i+1).get("sl").toString()),2, BigDecimal.ROUND_HALF_UP)
                    .multiply(BigDecimal.valueOf(100));
            list.get(i+1).put("zzl",percentage+"%");
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> inventoryInOutStatistics() {
        List<Map<String, Object>> rkList = kcswMapper.getRkslByMonth();
        List<Map<String, Object>> ckList = kcswMapper.getCkslByMonth();
        List<Map<String, Object>> kcList = kcswMapper.getKcslByMonth();
        List<Map<String, Object>> syjyslList = kcswMapper.getSyjyslByMonth();

        for(Map<String, Object> rkmap:rkList){
            for(Map<String, Object> ckmap:ckList){
                if(rkmap.get("yf").equals(ckmap.get("yf"))){
                    rkmap.put("cksl",ckmap.get("cksl"));
                }else{
                    rkmap.put("cksl",ckmap.get("cksl"));
                    rkmap.put("yf",ckmap.get("yf"));
                }
            }
        }

        for(Map<String, Object> rkmap:rkList){
            for(Map<String, Object> kcmap:kcList){
                if(rkmap.get("yf").equals(kcmap.get("yf"))){
                    rkmap.put("kcsl",kcmap.get("kcsl"));
                }else{
                    rkmap.put("kcsl",kcmap.get("kcsl"));
                    rkmap.put("yf",kcmap.get("yf"));
                }
            }
        }

        for(Map<String, Object> rkmap:rkList){
            for(Map<String, Object> syjyslmap:syjyslList){
                 BigDecimal a = new BigDecimal(syjyslmap.get("yf").toString()).add(new BigDecimal(1)) ;
                 BigDecimal b = new BigDecimal(rkmap.get("yf").toString()) ;
                 if(a.toString().equals(b.toString())){
                     rkmap.put("syjykc",syjyslmap.get("syjysl"));
                 }
            }
        }

        return rkList;
    }

    /*@Override
    public IPage<List<Map<String,Object>>> findPage(TjfxPageParam tjfxPageParam) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("orgName",tjfxPageParam.getOrgName());
        map.put("city",tjfxPageParam.getCity());
        map.put("designCapacity",tjfxPageParam.getDesignCapacity());

        return kczlMapper.findPage(new Page<>(tjfxPageParam.getCurrent(), tjfxPageParam.getSize()), map);
    }

   *//* public IPage<Map<String, Object>> findPage(OrgInfoPageParam orgInfoPageParam) {

        QueryWrapper<OrgInfo> queryWrapper = new QueryWrapper<>();
        //分页相关
        if (orgInfoPageParam.getOrgName() != null) {
            queryWrapper.like("a.org_name", orgInfoPageParam.getOrgName());
        }

        Page<OrgInfo> p = new Page<>(orgInfoPageParam.getCurrent(), orgInfoPageParam.getSize());

        IPage<Map<String, Object>> page = orgInfoMapper.findPage(p, queryWrapper);
        return page;

    }*//*


    @Override
    public Page<Map<String,Object>> detailsPage(TjfxPageParam tjfxPageParam) {
        QueryWrapper<BusinessQcQualityInspection> queryWrapper = new QueryWrapper<>();
        //分页相关
        if (tjfxPageParam.getOrgId() != null) {
            queryWrapper.eq("qqi.org_id", tjfxPageParam.getOrgId());
        }
        *//*if (tjfxPageParam.getLsdj() != null) {
            queryWrapper.eq("qqi.grain_grade_id", tjfxPageParam.getLsdj());
        }
        if (tjfxPageParam.getLsxz() != null) {
            queryWrapper.eq("qqi.grain_nature_id", tjfxPageParam.getLsxz());
        }
        if (tjfxPageParam.getLspz() != null) {
            queryWrapper.eq("qqi.grain_variety_id", tjfxPageParam.getLspz());
        }
        if (tjfxPageParam.getCfmc() != null) {
            queryWrapper.like("bs.storehouse_name", tjfxPageParam.getCfmc());
        }*//*
        if (tjfxPageParam.getOrgName() != null) {
            queryWrapper.like("oi.org_name", tjfxPageParam.getOrgName());
        }
        if (tjfxPageParam.getLsdjName() != null) {
            queryWrapper.like("be2.enumname", tjfxPageParam.getLsdjName());
        }
        if (tjfxPageParam.getLsxzName() != null) {
            queryWrapper.like("be1.enumname", tjfxPageParam.getLsxzName());
        }
        if (tjfxPageParam.getLspzName() != null) {
            queryWrapper.like("be.enumname", tjfxPageParam.getLspzName());
        }
        if (tjfxPageParam.getCfmc() != null) {
            queryWrapper.like("bs.storehouse_name", tjfxPageParam.getCfmc());
        }

        Page<BusinessQcQualityInspection> p = new Page<>(tjfxPageParam.getCurrent(), tjfxPageParam.getSize());

        Page<Map<String,Object>> page = kczlMapper.detailsPage(p, queryWrapper);
        return page;
    }

    @Override
    public BusinessQcQualityInspection getByMasterId(String id) {
        // id查询主表数据
        BusinessQcQualityInspection masterParam = kczlMapper.selectZjByPrimaryKey(id);
        if(ObjectUtil.isNotNull(masterParam)){
            masterParam.setItem(kczlMapper.selectZjMXByMainKey(id));
        }
        return masterParam;
    }*/



   /* @Override
    public IPage<Map<String, Object>> findPage(DevicePageParam devicePageParam) {
        QueryWrapper<Devicerepair> queryWrapper = new QueryWrapper<>();
        //分页相关
        if (devicePageParam.getOrgName() != null) {
            queryWrapper.like("b.org_name", devicePageParam.getOrgName());
        }
        if (devicePageParam.getDeviceName() != null) {
            queryWrapper.like("a.device_name", devicePageParam.getDeviceName());
        }

        Page<Devicerepair> p = new Page<>(devicePageParam.getCurrent(), devicePageParam.getSize());

        IPage<Map<String, Object>> page = deviceMapper.findPage(p, queryWrapper);

        return page;
    }*/

}

