package org.jeecg.modules.fbsWorkshopDispatchList.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
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.mapper.FbsWorkshopDispatchListMapper;
import org.jeecg.modules.fbsWorkshopDispatchList.service.IFbsWorkshopDispatchListService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.jeecg.common.util.oConvertUtils.isEmpty;

/**
 * @Description: 车间派工单
 * @author： jeecg-boot
 * @date：   2020-10-13
 * @version： V1.0
 */
@Service
@Transactional
public class FbsWorkshopDispatchListServiceImpl extends ServiceImpl<FbsWorkshopDispatchListMapper, FbsWorkshopDispatchList> implements IFbsWorkshopDispatchListService {

    @Resource
    private IFbsWorkshopDispatchListService iFbsWorkshopDispatchListService;

    @Resource
    private FbsWorkshopDispatchListMapper fbsWorkshopDispatchListMapper;

    @Resource
    private IFbsMomOrderdetailService iFbsMomOrderdetailService;

    /**
     * 更新状态
     * @param state 状态（0初始，1接单,2暂停，3完成）
     * @param id
     * @return
     */
    public int updateState(String state,String id){
        return fbsWorkshopDispatchListMapper.updateState(state,id);
    }
    @Override
    public List<FbsWorkshopDispatchList> selNowDispatchList() {
        return fbsWorkshopDispatchListMapper.selNowDispatchList();
    }
    @Override
    public List<FbsWorkshopDispatchList> getSumDispatch(List<FbsMomOrderdetail> fbsMomOrderdetailList) {

        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        //求和的派工单集合
//        List<FbsWorkshopDispatchList> fbsWorkshopDispatchListListSum=fbsWorkshopDispatchListMapper.getSumDispatch();
        List<FbsWorkshopDispatchList> fbsWorkshopDispatchListListSum=new ArrayList<>();
        getWorkshopDispatchListListSum(fbsWorkshopDispatchListListSum,fbsMomOrderdetailList);
        //全部派工单为求和
//        LambdaQueryWrapper<FbsWorkshopDispatchList> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(FbsWorkshopDispatchList::getDelFlag,"0");
//        List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=iFbsWorkshopDispatchListService.list(lambdaQueryWrapper);
        // 获取当月第一天和最后一天
        Calendar cale = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String firstday, lastday;
        // 获取前月的第一天
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        firstday = format.format(cale.getTime());
        // 获取前月的最后一天
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        lastday = format.format(cale.getTime());
        //获取开始时间结束时间 当前日期前15天时间
//        String startTime = fmt.format(new Date().getTime()-14*24*60*60*1000);
//        String endTime = fmt.format(new Date());
        String startTime = firstday;
        String endTime = lastday;
        List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=fbsWorkshopDispatchListMapper.getDispatchClose(startTime,endTime);
        //两个时间之间的所有日期
        List<Date> dateList=startDateAndEndDate(startTime,endTime);

        //组装派工单报表数据
        if(fbsWorkshopDispatchListListSum!=null&&fbsWorkshopDispatchListList!=null&&dateList!=null){
            for(FbsWorkshopDispatchList dispatchListSum:fbsWorkshopDispatchListListSum){
                Integer quantityCompletedTotal=0;
                BigDecimal manHoursCompletedTotal=new BigDecimal(0.00);//累计总工时
                List<FbsWorkshopDispatchList> dispatchListAdd=new ArrayList<>();//需要保存子集集合的派工单（日期分组派工单）
                for(Date date:dateList){
                    String dateString=fmt.format(date);//时间
                    boolean isorder=false;
                    FbsWorkshopDispatchList fbsWorkshopDispatchList=new FbsWorkshopDispatchList();
                    BigDecimal number_of_tasks=new BigDecimal(0.00);//派工数量
                    BigDecimal standard_working_hours=new BigDecimal(0.00);//标准工时
                    BigDecimal qualified_number=new BigDecimal(0.00);//完成数量
                    BigDecimal man_hours_completed=new BigDecimal(0.00);//完成工时
                    BigDecimal standard_working_hours_z=new BigDecimal(0.00);//单个标准工时
                    int i=1;
                    for(FbsWorkshopDispatchList dispatchList:fbsWorkshopDispatchListList){
                        String orderIdSum=dispatchListSum.getProductionOrderNumberName();//求和的派工生产单号
                        String orderId=dispatchList.getProductionOrderNumberName();//全部派工生产单号
                        String dispatchDate=fmt.format(dispatchList.getFinalTime());//完工时间
                        String proSum=dispatchListSum.getProductId();//求和中的产品id
                        String pro=dispatchList.getProductId();//全部中的产品id
                        //找到对应当天的派工单则添加

                        if(orderIdSum.equals(orderId)&&dateString.equals(dispatchDate)&&proSum.equals(pro)){
                            if(dispatchList.getQualifiedNumber()!=null){
                                quantityCompletedTotal+=dispatchList.getQualifiedNumber();//统计累计完成数量
                            }
                            manHoursCompletedTotal=manHoursCompletedTotal.add(getDecimal(Double.valueOf(dispatchList.getManHoursCompleted())));//累计完成工时
                            fbsWorkshopDispatchList=dispatchList;
                            if(i<=1){
                                number_of_tasks=dispatchList.getNumberOfTasks();
                                standard_working_hours=getDecimal(Double.valueOf(dispatchList.getStandardWorkingHours()));
                                qualified_number=getDecimal(Double.valueOf(dispatchList.getQualifiedNumber()));
                                man_hours_completed=getDecimal(Double.valueOf(dispatchList.getManHoursCompleted()));
                            }else{
                                number_of_tasks=number_of_tasks.add(dispatchList.getNumberOfTasks());
                                standard_working_hours=standard_working_hours.add(getDecimal(Double.valueOf(dispatchList.getStandardWorkingHours())));
                                qualified_number=qualified_number.add(getDecimal(Double.valueOf(dispatchList.getQualifiedNumber())));
                                man_hours_completed=man_hours_completed.add(getDecimal(Double.valueOf(dispatchList.getManHoursCompleted())));
                                fbsWorkshopDispatchList.setNumberOfTasks(getDecimal(Double.valueOf(formatDouble5(number_of_tasks.doubleValue()))));
                                fbsWorkshopDispatchList.setStandardWorkingHours(formatDouble5(standard_working_hours.doubleValue()));
                                fbsWorkshopDispatchList.setQualifiedNumber(new Double(qualified_number.doubleValue()).intValue());
                            }


                            isorder=true;
                            i++;
                        }
                    }
                    //没有则添加空的派工单
                    if(isorder==false){
                        FbsWorkshopDispatchList fbsWorkshopDispatchList2=new FbsWorkshopDispatchList();
                        fbsWorkshopDispatchList2.setFinalTime(date);
                        dispatchListAdd.add(fbsWorkshopDispatchList2);
                    }else{
                        if(fbsWorkshopDispatchList.getQualifiedNumber()==null||fbsWorkshopDispatchList.getQualifiedNumber()==0){
                            fbsWorkshopDispatchList.setProductionEfficiency("0.0");
                        }
                        if(i>1){
                            //一天存同个订单分多个派工单的情况 qualified_number.doubleValue()>0.0&&fbsWorkshopDispatchList.getStandardWorkingHoursZ()!=null&&Double.valueOf(fbsWorkshopDispatchList.getStandardWorkingHoursZ())>0.0&&
                            if(man_hours_completed.doubleValue()>0.0){

                                fbsWorkshopDispatchList.setProductionEfficiency(getDecimal(Double.valueOf(fbsWorkshopDispatchList.getStandardWorkingHoursZ())).multiply(qualified_number).divide(man_hours_completed,1,BigDecimal.ROUND_HALF_UP).multiply(getDecimal(100.0)).doubleValue()+"");
                                fbsWorkshopDispatchList.setAchievementRate(qualified_number.divide(number_of_tasks,1,BigDecimal.ROUND_HALF_UP).multiply(getDecimal(100.0)).doubleValue()+"");
                            }

                        }

                        dispatchListAdd.add(fbsWorkshopDispatchList);

                    }
                }
                if(quantityCompletedTotal!=0){
                    dispatchListSum.setQuantityCompletedTotal(quantityCompletedTotal);
                }
                if(manHoursCompletedTotal.doubleValue()!=0){
                    dispatchListSum.setManHoursCompletedTotal(formatDouble5(manHoursCompletedTotal.doubleValue()));
                }
                //计算标准工时
                BigDecimal workingHoursTotal=new BigDecimal("0.0");
                if(StringUtils.isNotBlank(dispatchListSum.getStandardWorkingHoursZ())){
                    workingHoursTotal=getDecimal(Double.valueOf(dispatchListSum.getStandardWorkingHoursZ()));
                }
                if(dispatchListSum.getQuantityCompletedTotal()!=null){
                    String working=formatDouble5((workingHoursTotal.multiply(getDecimal(dispatchListSum.getQuantityCompletedTotal().doubleValue()))).doubleValue());
                    dispatchListSum.setStandardWorkingHoursActualTotal(working);
                }
                dispatchListSum.setFbsWorkshopDispatchListList(dispatchListAdd);
            }
        }



        //获取生产名称合并行
        mergeLines(fbsWorkshopDispatchListListSum);
        //获取统计信息
        getStatisticsTotal(fbsWorkshopDispatchListListSum,dateList);
        return fbsWorkshopDispatchListListSum;
    }


    public List<FbsWorkshopDispatchList> export(List<FbsMomOrderdetail> fbsMomOrderdetailList,String month){
        //erp数据
        List<FbsWorkshopDispatchList> fbsWorkshopDispatchListListErp=new ArrayList<>();
        getWorkshopDispatchListListSum(fbsWorkshopDispatchListListErp,fbsMomOrderdetailList);
        //派工数据
        List<FbsWorkshopDispatchList> fbsWorkshopDispatchListListSum=fbsWorkshopDispatchListMapper.getSumDispatchNumber(month);
        if(fbsWorkshopDispatchListListErp!=null&&fbsWorkshopDispatchListListSum!=null){
            for(FbsWorkshopDispatchList dispatchListErp:fbsWorkshopDispatchListListErp){
                for(FbsWorkshopDispatchList dispatchListSum:fbsWorkshopDispatchListListSum){
                    String orderIdSum=dispatchListSum.getProductionOrderNumberName();//求和的派工生产单号
                    String orderId=dispatchListErp.getProductionOrderNumberName();//erp派工生产单号
                    String proSum=dispatchListSum.getProductId();//求和中的产品id
                    String pro=dispatchListErp.getProductId();//erp中的产品id
                    //找到对应当天的派工单则添加
                    if(orderIdSum.equals(orderId)&&proSum.equals(pro)){
                        dispatchListErp.setPlannedQuantity(dispatchListSum.getPlannedQuantity());
                        dispatchListErp.setStandardWorkingHoursZ(dispatchListSum.getStandardWorkingHoursZ());
                        dispatchListErp.setLatestCompletedQuantity(dispatchListSum.getLatestCompletedQuantity());
                        dispatchListErp.setManHoursCompletedTotal(dispatchListSum.getManHoursCompletedTotal());
                    }
                }
            }
        }
        return fbsWorkshopDispatchListListErp;
    }

    //获取erp中的订单数据
    public void getWorkshopDispatchListListSum(List<FbsWorkshopDispatchList> fbsWorkshopDispatchListListSum,List<FbsMomOrderdetail> fbsMomOrderdetailList){
        for(FbsMomOrderdetail fbsMomOrderdetail:fbsMomOrderdetailList){
            FbsWorkshopDispatchList fbsWorkshopDispatchList=new FbsWorkshopDispatchList();
            fbsWorkshopDispatchList.setProjectName(fbsMomOrderdetail.getDefine12());
            fbsWorkshopDispatchList.setProductionOrderNumberName(fbsMomOrderdetail.getMocode());
            fbsWorkshopDispatchList.setProductId(fbsMomOrderdetail.getCinvcode());
            fbsWorkshopDispatchList.setProduct(fbsMomOrderdetail.getCinvname());
            fbsWorkshopDispatchList.setNumberOfTasksTotal(Math.rint(Double.parseDouble(fbsMomOrderdetail.getQty()+""))+"");
            BigDecimal workingHoursTotal=new BigDecimal("0.0");
            if(StringUtils.isNotBlank(fbsMomOrderdetail.getCinvDefine13())){
                workingHoursTotal=getDecimal(Double.valueOf(fbsMomOrderdetail.getCinvDefine13()));
            }
            String working=formatDouble5((workingHoursTotal.multiply(getDecimal(Double.valueOf(fbsMomOrderdetail.getQty()+"")))).doubleValue());
            fbsWorkshopDispatchList.setStandardWorkingHoursTotal(working);
            fbsWorkshopDispatchList.setStandardWorkingHoursZ(fbsMomOrderdetail.getCinvDefine13());
            fbsWorkshopDispatchList.setStatus(fbsMomOrderdetail.getStatus());
            fbsWorkshopDispatchListListSum.add(fbsWorkshopDispatchList);
        }
    }



    public List<Date> startDateAndEndDate(String startDate, String endDate) {
        List<Date> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = startDate; //开始时间
        String date2 = endDate; //结束时间
        Long startTIme = null;
        try {
            startTIme = sdf.parse(date1).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Long endTime = null;
        try {
            endTime = sdf.parse(date2).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Long oneDay = 1000 * 60 * 60 * 24l;
        Long time = startTIme;
        while (time <= endTime) {
            Date d = new Date(time);
            // DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            // String date=df.format(d);
            list.add(d);
            time += oneDay;
        }
        return list;
    }

    /**
    * @Author chenchuang
    * @Description //TODO 获取生产名称合并行
    * @Date 2020/10/17 17:23
    * @Param [fbsWorkshopDispatchListList]
    * @return void
    */
    public void mergeLines(List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList){
        if(fbsWorkshopDispatchListList!=null){
            String productionLineId="";
            Integer sort=1;
            for(FbsWorkshopDispatchList fbsWorkshopDispatchList:fbsWorkshopDispatchListList){
//                Integer mergeLines=0;
//                if(productionLineId.equals("")||!productionLineId.equals(fbsWorkshopDispatchList.getProductionLineId())){
//                    productionLineId=fbsWorkshopDispatchList.getProductionLineId();
//                    for(FbsWorkshopDispatchList workshopDispatchList:fbsWorkshopDispatchListList){
//                        if(productionLineId.equals(workshopDispatchList.getProductionLineId())){
//                            mergeLines++;
//                        }
//                    }
//                    fbsWorkshopDispatchList.setSort(sort);
//                    sort++;
//                }
//                fbsWorkshopDispatchList.setMergeLines(mergeLines);
                fbsWorkshopDispatchList.setSort(sort);
                sort++;
            }
        }
    }

/**
* @Author chenchuang
* @Description //TODO 获取统计行的数据
* @Date 2020/10/20 17:30
* @Param [fbsWorkshopDispatchListList, dateList]
* @return void
*/
    public void getStatisticsTotal(List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList,List<Date> dateList){
        Integer numberOfTasksTotal=0;//月度计划
        BigDecimal standardWorkingHoursTotal=new BigDecimal(0.00);//标准工时
        BigDecimal standardWorkingHoursActualTotal=new BigDecimal(0.00);//实际标准工时
        Integer quantityCompletedTotal=0;//累计完成数量
        BigDecimal manHoursCompletedTotal=new BigDecimal(0.00);//累计总工时
        //循环累加计算
        for(FbsWorkshopDispatchList workshopDispatchList:fbsWorkshopDispatchListList){
            if(isNumber(workshopDispatchList.getNumberOfTasksTotal())){
                numberOfTasksTotal+=(int)Math.rint(Double.valueOf(workshopDispatchList.getNumberOfTasksTotal()));
            }
            if(isNumber(workshopDispatchList.getStandardWorkingHoursTotal())){
                standardWorkingHoursTotal=standardWorkingHoursTotal.add(getDecimal(Double.valueOf(workshopDispatchList.getStandardWorkingHoursTotal())));
            }
            if(isNumber(workshopDispatchList.getStandardWorkingHoursActualTotal())){
                standardWorkingHoursActualTotal=standardWorkingHoursActualTotal.add(getDecimal(Double.valueOf(workshopDispatchList.getStandardWorkingHoursActualTotal())));
            }
            if(workshopDispatchList.getQuantityCompletedTotal()!=null){
                quantityCompletedTotal+=workshopDispatchList.getQuantityCompletedTotal();
            }
            if(isNumber(workshopDispatchList.getManHoursCompletedTotal())){
                manHoursCompletedTotal=manHoursCompletedTotal.add(getDecimal(Double.valueOf(workshopDispatchList.getManHoursCompletedTotal())));
            }

        }
        //获取按日期排列的统计数据
        List<FbsWorkshopDispatchList> fbsWorkshopDispatchListListTotal=new ArrayList<>();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        //循环时间段
        for(Date date:dateList){
            String dateN=fmt.format(date);
            BigDecimal numberOfTasks=new BigDecimal("0.0");//当日计算数量
            Integer qualifiedNumber=0;//当日完成数量;
            BigDecimal standardWorkingHours=new BigDecimal(0.00);//当日标准工时;
            BigDecimal manHoursCompleted=new BigDecimal(0.00);//当日标准工时;
            //循环报表数据获取对应的时间数据
            for(FbsWorkshopDispatchList workshopDispatchList:fbsWorkshopDispatchListList){
                if(workshopDispatchList.getFbsWorkshopDispatchListList()!=null){
                    for(FbsWorkshopDispatchList workshopDispatchListC:workshopDispatchList.getFbsWorkshopDispatchListList()){
                        String endDate=fmt.format(workshopDispatchListC.getFinalTime());
                        //找到对应时间的数据
                        if(dateN.equals(endDate)){//累加
                            if(workshopDispatchListC.getNumberOfTasks()!=null){
                                numberOfTasks=numberOfTasks.add(workshopDispatchListC.getNumberOfTasks());
                            }
                            if(workshopDispatchListC.getQualifiedNumber()!=null){
                                qualifiedNumber+=workshopDispatchListC.getQualifiedNumber();
                            }
                            if(isNumber(workshopDispatchListC.getStandardWorkingHours())){
                                standardWorkingHours=standardWorkingHours.add(getDecimal(Double.valueOf(workshopDispatchListC.getStandardWorkingHours())));
                            }
                            if(isNumber(workshopDispatchListC.getManHoursCompleted())){
                                manHoursCompleted=manHoursCompleted.add(getDecimal(Double.valueOf(workshopDispatchListC.getManHoursCompleted())));
                            }
                        }
                    }
                }
            }
            //存储
            FbsWorkshopDispatchList fbsWorkshopDispatchList=new FbsWorkshopDispatchList();
            fbsWorkshopDispatchList.setNumberOfTasks(numberOfTasks);
            fbsWorkshopDispatchList.setQualifiedNumber(qualifiedNumber);
            fbsWorkshopDispatchList.setStandardWorkingHours(formatDouble5(standardWorkingHours.doubleValue())+"");
            fbsWorkshopDispatchList.setManHoursCompleted(formatDouble5(manHoursCompleted.doubleValue())+"");
            Double achievement=0.0;
            if(qualifiedNumber!=0||numberOfTasks.doubleValue()!=0){
                achievement=qualifiedNumber.doubleValue()/numberOfTasks.doubleValue();
            }
            fbsWorkshopDispatchList.setAchievementRate(formatDouble5((achievement*100.0)));
            Double production=0.0;
            if(standardWorkingHours.doubleValue()>0){
                production=standardWorkingHours.divide(manHoursCompleted,2, RoundingMode.HALF_UP).multiply(getDecimal(100.0)).doubleValue();
            }
            fbsWorkshopDispatchList.setProductionEfficiency(production+"");
            fbsWorkshopDispatchListListTotal.add(fbsWorkshopDispatchList);
        }


        //放到最后一行
        FbsWorkshopDispatchList fbsWorkshopDispatchList=new FbsWorkshopDispatchList();
        fbsWorkshopDispatchList.setProjectName("统计");
        fbsWorkshopDispatchList.setNumberOfTasksTotal(numberOfTasksTotal+"");
        fbsWorkshopDispatchList.setStandardWorkingHoursTotal(formatDouble5(standardWorkingHoursTotal.doubleValue())+"");
        fbsWorkshopDispatchList.setStandardWorkingHoursActualTotal(formatDouble5(standardWorkingHoursActualTotal.doubleValue())+"");
        fbsWorkshopDispatchList.setQuantityCompletedTotal(quantityCompletedTotal);
        fbsWorkshopDispatchList.setManHoursCompletedTotal(formatDouble5(manHoursCompletedTotal.doubleValue())+"");
        fbsWorkshopDispatchList.setFbsWorkshopDispatchListList(fbsWorkshopDispatchListListTotal);
        fbsWorkshopDispatchListList.add(fbsWorkshopDispatchList);
    }



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

    private boolean isNumber(String str){
        if(isEmpty(str)){
            return false;
        }
        String reg = "\\d+(\\.\\d+)?";
        return str.matches(reg);

    }

    public static String formatDouble5(double d) {
        return String.format("%.1f", d);
    }

    @Override
    public Map<String,Object> getProductHoursMonthReport(String dateString) {
        Map<String,Object> map=new HashMap<>();
        List<FbsWorkshopDispatchList> fbsWorkshopDispatchListList=fbsWorkshopDispatchListMapper.getProductHoursMonthReport(dateString);
        if(fbsWorkshopDispatchListList!=null){
            for(FbsWorkshopDispatchList fbsWorkshopDispatchList:fbsWorkshopDispatchListList){
                map.put(fbsWorkshopDispatchList.getProduct(),fbsWorkshopDispatchList.getProductTotal());
            }
        }

        return map;
    }

    @Override
    public Map<String, Object> getAbnormalHoursMonthReport(String dateString) {
        return fbsWorkshopDispatchListMapper.getAbnormalHoursMonthReport(dateString);
    }

    @Override
    public List<Map<String, Object>> getAttendance() {
        List<Map<String, Object>> mapList=new ArrayList<>();
        //查询数据存在的月份出勤
        List<Map<String, Object>> mapRelut=fbsWorkshopDispatchListMapper.getAttendance();
        for(int i=1;i<=12;i++){
            String month="";
            //获取月份字符
            if(i<10){
               month="0"+i;
            }else{
                month=i+"";
            }
            boolean ismonth=false;//是否该月的数据
            for(Map<String, Object> map:mapRelut){
                //查询是否有
                if(month.equals(map.get("label"))){
                    ismonth=true;
                    mapList.add(map);
                }
            }
            //如果没有则
            if(ismonth==false){
                Map<String, Object> map=new HashMap<>();
                map.put("label",month);
                map.put("sum1",0);
                map.put("sum2",0);
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public boolean writeBackManHoursCompleted(String manHoursCompleted,String latestCompletedQuantity,String fbsWorkshopDispatchListId) {
        Boolean isReults=false;
        //根据id查询派工单
        FbsWorkshopDispatchList fbsWorkshopDispatchListGetById=fbsWorkshopDispatchListMapper.selectById(fbsWorkshopDispatchListId);
        fbsWorkshopDispatchListGetById.setManHoursCompleted(manHoursCompleted);//回写的完成工时
        fbsWorkshopDispatchListGetById.setLatestCompletedQuantity(latestCompletedQuantity);//回写最新完成数量
        //回写
        int reultRow=fbsWorkshopDispatchListMapper.updateById(fbsWorkshopDispatchListGetById);
        if(reultRow>0){
            isReults=true;
        }
        return isReults;
    }

    @Override
    public List<FbsWorkshopDispatchList> getWorkingHoursContrast() {
        return fbsWorkshopDispatchListMapper.getWorkingHoursContrast();
    }

    @Override
    public String getProductionEfficiency(String dateString) {
        return fbsWorkshopDispatchListMapper.getProductionEfficiency(dateString);
    }

    @Override
    public Map<String, Object> getAchievementRate() {
        return fbsWorkshopDispatchListMapper.getAchievementRate();
    }

    @Override
    public Map<String, Object> getAttendanceOfTheDay() {
        Map<String, Object> map=fbsWorkshopDispatchListMapper.getAttendanceOfTheDay();
        return fbsWorkshopDispatchListMapper.getAttendanceOfTheDay();
    }

    @Override
    public List<FbsWorkshopDispatchList> getProductList() {
        return fbsWorkshopDispatchListMapper.getProductList();
    }

    @Override
    public Map<String, Object> getAbnormalHoursMonthReportSameDay() {
        return fbsWorkshopDispatchListMapper.getAbnormalHoursMonthReportSameDay();
    }

    @Override
    public List<FbsWorkshopDispatchList> getProjectScheduleCompletion() {
        return fbsWorkshopDispatchListMapper.getProjectScheduleCompletion();
    }

    @Override
    public void workSaveBatch(FbsWorkshopDispatchList fbsWorkshopDispatchList) {
        if(fbsWorkshopDispatchList.getFbsWorkshopDispatchListList()!=null){
            Integer sort=1;
            for(FbsWorkshopDispatchList dispatchList:fbsWorkshopDispatchList.getFbsWorkshopDispatchListList()){
                BigDecimal remain=dispatchList.getRemainingQuantity().subtract(dispatchList.getNumberOfTasks());
                if(remain.doubleValue()<0.0){
                    dispatchList.setRemainingQuantity(new BigDecimal("0.0"));
                }else{
                    dispatchList.setRemainingQuantity(remain);
                }
                dispatchList.setSort(sort);
                sort++;
            }
            iFbsWorkshopDispatchListService.saveBatch(fbsWorkshopDispatchList.getFbsWorkshopDispatchListList());
        }
    }

    @Override
    public List<FbsWorkshopDispatchList> getListWhereStartEnd() {
        return fbsWorkshopDispatchListMapper.getListWhereStartEnd();
    }


}
