package com.unis.crk.common.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.unis.crk.common.entity.Bizflowtrail;
import com.unis.crk.common.mapper.BillnonewgjjMapper;
import com.unis.crk.common.mapper.BizflowtrailMapper;
import com.unis.crk.common.service.IBizflowtrailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.crk.utils.SqpMapUtil;
import lombok.experimental.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Dictionary;
import java.util.Map;

/**
 * <p>
 * 数据流程节点轨迹记录表 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2021-09-28
 */
@Service
public class BizflowtrailServiceImpl extends ServiceImpl<BizflowtrailMapper, Bizflowtrail> implements IBizflowtrailService {

    @Resource
    private BizflowtrailMapper bizflowtrailMapper;
    @Resource
    private BillnonewgjjMapper billnonewgjjMapper;

    /**
     * 查询转圈粮
     *
     * @param bizNo     单据号
     * @param direction 方向：1;入库，2;出库
     * @return
     */
    @Override
    public JSONObject circleBusinessInfo(String bizNo, int direction) {
        try {

            //根据方向查询单位，品种，车牌号及业务日期
            Map<String,Object> map = bizflowtrailMapper.getCrkPzInfo(bizNo,direction);
            if (map != null){
                String unitId = map.getOrDefault("unitId","").toString(); //单位id
                String lspz = map.getOrDefault("lspz","").toString(); //品种id
                String lspzName = map.getOrDefault("lspzName","").toString(); //品种id
                String orgName = map.getOrDefault("orgName","").toString(); // 单位名称
                String ywrq = map.getOrDefault("ywrq","").toString(); // 业务日期
                String slr = map.getOrDefault("slr","").toString(); // 客户名称
                String cph = map.getOrDefault("cph","").toString(); // 车牌号
            //得到最近三天内出入库情况
                int DayCount=3;//3天内
                String dateFormat = "yyyy-MM-dd";
                Date ywrq_ = DateUtil.parse(ywrq,dateFormat);
                String beginDay=DateUtil.offsetDay(ywrq_,DayCount*-1).toString(dateFormat); //开始日期,前三天
                String endDay=ywrq; //结束日期
                Map<String,Object> map1 =bizflowtrailMapper.getCircleBusinessInfo(unitId,lspz,beginDay,endDay,cph,direction);
                if (map1 != null){ //表示有转圈粮的可能性
                    String ywrq_1 = map1.getOrDefault("ywrq","").toString(); // 业务日期
                    String cfmc = map1.getOrDefault("cfmc","").toString(); // 仓库名称
                    String sl = map1.getOrDefault("sl","").toString(); // 数量
                    String lsdjName = map1.getOrDefault("lsdjName","").toString(); // 等级名称
                    //写一条预警信息到库级的转圈粮预警信息表中
                    String detail="注意：车牌号【"+cph+"】车辆，"+ywrq_1+"日存在“"+orgName+"”"+
                            cfmc+((direction==1)?"出":"入")+"库记录，粮食品种："+lspzName+"、粮食数量："+sl+"kg、粮食等级："+lsdjName+"。"+
                            "系统自动判断疑为转圈粮！";
                    String addSql = String.format(SqpMapUtil.getSql("INSERT_STORAGEPREVIOLATION"), slr,orgName,detail,unitId);
                    billnonewgjjMapper.executeUpdate(addSql);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
