package org.jeecg.modules.fbsWorkshopDispatchList.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.appInterface.entity.FbsWorkingStatistics;
import org.jeecg.modules.appInterface.service.IFbsWorkingStatisticsService;
import org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail;
import org.jeecg.modules.fbsMomOrder.service.IFbsMomOrderdetailService;
import org.jeecg.modules.fbsWorkshopDispatchList.entity.FbsWorkshopDispatchList;
import org.jeecg.modules.fbsWorkshopDispatchList.service.IFbsWorkshopDispatchListService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能描述：
 *
 * @Author: chenchuang
 * @Date: 2021/1/7 10:36
 */
@Transactional
public class FbsLatestCompletedQuantityUpdateJob implements Job {
    @Autowired
    IFbsWorkshopDispatchListService iFbsWorkshopDispatchListService;

    @Autowired
    IFbsMomOrderdetailService iFbsMomOrderdetailService;
    @Autowired
    IFbsWorkingStatisticsService iFbsWorkingStatisticsService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("9999999999999999999999999999");
        show();

    }

    public void show(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        DecimalFormat dff = new DecimalFormat("#.0");
        //最近90天的派工数据
//        QueryWrapper<FbsWorkshopDispatchList> queryWrapper=new QueryWrapper<>();
//        queryWrapper.lambda().eq(FbsWorkshopDispatchList::getDelFlag,'0');
//        queryWrapper.apply("date  <= DATE_SUB( CURDATE( ), INTERVAL 3 DAY ) ");
        List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=iFbsWorkshopDispatchListService.getLatelyDayWorkShopDispatch();
        //最近90天rep中做的生产订单
        List<FbsMomOrderdetail> fbsMomOrderdetailList=iFbsMomOrderdetailService.getLatelyDayMomOrderList();
        //需要更新完成数量的派工
        List<FbsWorkshopDispatchList> fbsWorkshopDispatchListListToUpdate=new ArrayList<>();
        //派工订单
        for(FbsWorkshopDispatchList fbsWorkshopDispatchList:fbsWorkshopDispatchListList){

            String code=fbsWorkshopDispatchList.getProductionOrderNumberName();//派工中的订单号
            String codeN=fbsWorkshopDispatchList.getProductId();
            System.out.println(code);
            //循环rep订单
            for(FbsMomOrderdetail fbsMomOrderdetail:fbsMomOrderdetailList){
                String modeCode=fbsMomOrderdetail.getMocode();//rep中的订单号
                String invcode=fbsMomOrderdetail.getInvcode();
                //派工数量
                BigDecimal numberOfTasks=new BigDecimal("0.0");
                if(fbsWorkshopDispatchList.getNumberOfTasks()!=null){
                    numberOfTasks=fbsWorkshopDispatchList.getNumberOfTasks();
                }
                //完工数量
                BigDecimal latestCompletedQuantity=new BigDecimal("0.0");
                if(StringUtils.isNotBlank(fbsWorkshopDispatchList.getLatestCompletedQuantity())){
                    latestCompletedQuantity=getDecimal(fbsWorkshopDispatchList.getLatestCompletedQuantity());
                }
                //入库数量
                BigDecimal qualifiedinqty=new BigDecimal("0.0");
                if(StringUtils.isNotBlank(String.valueOf(fbsMomOrderdetail.getQualifiedinqty()))){
                    qualifiedinqty=getDecimal(String.valueOf(fbsMomOrderdetail.getQualifiedinqty()));
                }

                //找到对应需要更新的订单
                if(code.equals(modeCode)&&codeN.equals(invcode)&&latestCompletedQuantity.doubleValue()!=qualifiedinqty.doubleValue()){
                    Date date = new Date();//获取当前的日期
                    String str = df.format(date);//获取String类型的时间
                    //获取本次更新信息
                    String memo="订单号："+code+",更新前的完成数量："+fbsWorkshopDispatchList.getLatestCompletedQuantity()+",更新后的完成数量："+fbsMomOrderdetail.getQualifiedinqty();

                    memo+=",跟新前的状态："+fbsWorkshopDispatchList.getState();
                    fbsWorkshopDispatchList.setLatestCompletedQuantity(qualifiedinqty.doubleValue()+"");
                    if(numberOfTasks.doubleValue()==qualifiedinqty.doubleValue()){//如果完工了更新状态完工
                        fbsWorkshopDispatchList.setState("3");
                    }
                    memo+=",更新后的状态："+fbsWorkshopDispatchList.getState();
                    //更新所花时间
                    memo+=",更新前的完成时间："+fbsWorkshopDispatchList.getManHoursCompleted();
                    BigDecimal manHoursCompleted=new BigDecimal("0.0");
                    if(StringUtils.isNotBlank(fbsWorkshopDispatchList.getManHoursCompleted())){
                        manHoursCompleted=getDecimal(fbsWorkshopDispatchList.getManHoursCompleted());
                    }
                    BigDecimal differNumber=new BigDecimal("0.0");//获取更新相差数量
                    BigDecimal differHour=new BigDecimal("0.0");//相差工时
                    ///////////////////////////////////////////////////////////////erp数量比派工多
                    if(qualifiedinqty.doubleValue()>latestCompletedQuantity.doubleValue()){
                        differNumber=qualifiedinqty.subtract(latestCompletedQuantity);//erp-派工
                        if(StringUtils.isNotBlank(fbsWorkshopDispatchList.getStandardWorkingHoursZ())){
                            //算出相差的时间
                            differHour=differNumber.multiply(getDecimal(fbsWorkshopDispatchList.getStandardWorkingHoursZ()));
                            //累计在完成工时上
                            System.out.println("1"+dff.format((manHoursCompleted.add(differHour)).doubleValue()));
                            fbsWorkshopDispatchList.setManHoursCompleted(dff.format((manHoursCompleted.add(differHour)).doubleValue()));
                        }
//                        //新增扫码记录
//                        FbsWorkingStatistics fbsWorkingStatistics=new FbsWorkingStatistics();
//                        fbsWorkingStatistics.setPkWorkshopDispatchList(fbsWorkshopDispatchList.getId());
//                        fbsWorkingStatistics.setPersonnelCode("更新");
//                        fbsWorkingStatistics.setPersonnelName("更新");
//                        fbsWorkingStatistics.setMaterielCode(fbsWorkshopDispatchList.getProductId());
//                        fbsWorkingStatistics.setPersonnelName(fbsWorkshopDispatchList.getProduct());
//                        fbsWorkingStatistics.setMaterielCode(fbsWorkshopDispatchList.getProductId());
//                        fbsWorkingStatistics.setMaterielName(fbsWorkshopDispatchList.getProduct());
//                        fbsWorkingStatistics.setStartDate(fbsWorkshopDispatchList.getPlannedStartTime());
//                        fbsWorkingStatistics.setEndDate(fbsWorkshopDispatchList.getPlannedEndTime());
//                        fbsWorkingStatistics.setFirstTime(new Date());
//                        fbsWorkingStatistics.setFinalTime(new Date());
//                        fbsWorkingStatistics.setFinalInfo("实时报工");
//                        fbsWorkingStatistics.setFinalState("3");
//                        fbsWorkingStatistics.setWorkingHours(differHour);
//                        fbsWorkingStatistics.setDelFlag("0");
//                        fbsWorkingStatistics.setOrderNumber(fbsWorkshopDispatchList.getProductionOrderNumberName());
//                        fbsWorkingStatistics.setQualifiedNumber(differNumber.doubleValue()+"");
//                        fbsWorkingStatistics.setState("1");
//                        iFbsWorkingStatisticsService.save(fbsWorkingStatistics);
                    }
                    ///////////////////////////////////////////////////////////////////派工数量比erp
                    if(qualifiedinqty.doubleValue()<latestCompletedQuantity.doubleValue()){
                        differNumber=latestCompletedQuantity.subtract(qualifiedinqty);//派工-erp
                        if(StringUtils.isNotBlank(fbsWorkshopDispatchList.getStandardWorkingHoursZ())){
                            //算出相差的时间
                            differHour=differNumber.multiply(getDecimal(fbsWorkshopDispatchList.getStandardWorkingHoursZ()));
                            //在完成工时上减掉
                            System.out.println("2"+dff.format((manHoursCompleted.subtract(differHour)).doubleValue()));
                            fbsWorkshopDispatchList.setManHoursCompleted( dff.format((manHoursCompleted.subtract(differHour)).doubleValue()));
                        }
//                        LambdaQueryWrapper<FbsWorkingStatistics> fbsWorkingStatisticsLambdaQueryWrapper=new LambdaQueryWrapper<>();
//                        fbsWorkingStatisticsLambdaQueryWrapper.eq(FbsWorkingStatistics::getDelFlag,"0");
//                        fbsWorkingStatisticsLambdaQueryWrapper.eq(FbsWorkingStatistics::getPkWorkshopDispatchList,fbsWorkshopDispatchList.getId());
//                        fbsWorkingStatisticsLambdaQueryWrapper.orderByAsc(FbsWorkingStatistics::getFinalTime);
//                        List<FbsWorkingStatistics> fbsWorkingStatisticsList=iFbsWorkingStatisticsService.list(fbsWorkingStatisticsLambdaQueryWrapper);
//                        if(fbsWorkingStatisticsList!=null){
//                            BigDecimal differHourS=new BigDecimal("0.0");//相差时间
//                            differHourS=differHourS.add(differHour);
//                            BigDecimal differNumberS=new BigDecimal("0.0");//相差数量
//                            differNumberS=differNumberS.add(differNumber);
//                            for(FbsWorkingStatistics fbsWorkingStatistics:fbsWorkingStatisticsList){
//                                String statisticMemo="更新前：时间："+fbsWorkingStatistics.getWorkingHours()+"-数量："+fbsWorkingStatistics.getQualifiedNumber();
//                                //创建扫码修改对象
//                                FbsWorkingStatistics fbsWorkingStatisticsUpdate=new FbsWorkingStatistics();
//                                fbsWorkingStatisticsUpdate.setId(fbsWorkingStatistics.getId());
//                                //扫码的工时
//                                BigDecimal workingHours=new BigDecimal("0.0");
//                                if(fbsWorkingStatistics.getWorkingHours()!=null){
//                                    workingHours=fbsWorkingStatistics.getWorkingHours();
//                                }
//                                //扫码的数量
//                                BigDecimal qualifiedNumber=new BigDecimal("0.0");
//                                if(StringUtils.isNotBlank(fbsWorkingStatistics.getQualifiedNumber())){
//                                    qualifiedNumber=getDecimal(fbsWorkingStatistics.getQualifiedNumber());
//                                }
//                                //如果还有相差时间没减掉则继续扫码记录减工时
//                                if(differHourS.doubleValue()>0.0){
//                                    //如果相差时间比当前扫码记录时间还大 则直接扫码记录工时变0
//                                    if(differHourS.doubleValue()>workingHours.doubleValue()){
//                                        fbsWorkingStatisticsUpdate.setWorkingHours(new BigDecimal("0.0"));
//                                        differHourS.subtract(workingHours);//相差时间减去扫码记录时间
//                                    }else {//相差时间比扫码时间小 则扫码时间去除相差时间 时间更新完毕
//                                        fbsWorkingStatisticsUpdate.setWorkingHours(workingHours.subtract(differHourS));
//                                        differHourS=new BigDecimal("0.0");//相差时间清零
//                                    }
//                                }
//                                //如果还有相差数量没减掉则继续扫码记录减工时
//                                if(differNumberS.doubleValue()>0.0){
//                                    //如果相差数量比当前扫码记录数量还大 则直接扫码记录数量变0
//                                    if(differNumberS.doubleValue()>qualifiedNumber.doubleValue()){
//                                        fbsWorkingStatisticsUpdate.setQualifiedNumber("0");
//                                        differNumberS.subtract(qualifiedNumber);//相差时间减去扫码记录时间
//                                    }else {//相差数量比扫码数量小 则扫码数量去除相差数量 数量更新完毕
//                                        fbsWorkingStatisticsUpdate.setQualifiedNumber((qualifiedNumber.subtract(differNumberS).doubleValue())+"");
//                                        differNumberS=new BigDecimal("0.0");//相差数量清零
//                                    }
//                                }
//                                statisticMemo+="；更新后：时间："+fbsWorkingStatisticsUpdate.getWorkingHours()+"-数量："+fbsWorkingStatisticsUpdate.getQualifiedNumber()+"时间"+str;
//                                fbsWorkingStatisticsUpdate.setRefuseInfo(statisticMemo);
//                                iFbsWorkingStatisticsService.updateById(fbsWorkingStatisticsUpdate);
//                            }
//                        }

                    }
                    memo+=",更新后的完成时间："+fbsWorkshopDispatchList.getManHoursCompleted()+",更新时间："+str;
                    //更新备注
                    String lamemo=fbsWorkshopDispatchList.getLatestCompletedQuantityU8Memo();
                    if(lamemo==null){
                        lamemo="";
                    }
                    fbsWorkshopDispatchList.setLatestCompletedQuantityU8Memo(lamemo+";"+memo);
                    fbsWorkshopDispatchListListToUpdate.add(fbsWorkshopDispatchList);
                }
            }
        }
        //更新派工的入库数量
        if(fbsWorkshopDispatchListListToUpdate.size()>0){
            iFbsWorkshopDispatchListService.updateBatchWorkShopDispatch(fbsWorkshopDispatchListListToUpdate);
        }

    }




    public BigDecimal getDecimal(String d){
        BigDecimal decimal= new BigDecimal(d);
        return decimal;
    }
}
