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

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.fbsAbnormalWorkingHours.entity.FbsAbnormalWorkingHours;
import org.jeecg.modules.fbsAbnormalWorkingHours.entity.FbsAbnormalWorkingHoursDescribe;
import org.jeecg.modules.fbsAbnormalWorkingHours.entity.FbsAbnormalWorkingHoursType;
import org.jeecg.modules.fbsAbnormalWorkingHours.mapper.FbsAbnormalWorkingHoursMapper;
import org.jeecg.modules.fbsAbnormalWorkingHours.service.IFbsAbnormalWorkingHoursDescribeService;
import org.jeecg.modules.fbsAbnormalWorkingHours.service.IFbsAbnormalWorkingHoursService;
import org.jeecg.modules.fbsAbnormalWorkingHours.service.IFbsAbnormalWorkingHoursTypeService;
import org.jeecg.modules.fbsPerson.entity.FbsPerson;
import org.jeecg.modules.fbsPerson.mapper.FbsPersonMapper;
import org.jeecg.modules.fbsPerson.service.IFbsPersonService;
import org.jeecg.modules.fbsWorkshopDispatchList.entity.FbsWorkshopDispatchList;
import org.jeecg.modules.fbsWorkshopDispatchList.mapper.FbsWorkshopDispatchListMapper;
import org.springframework.beans.factory.annotation.Autowired;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 生产异常信息统计单
 * @author： jeecg-boot
 * @date：   2020-10-13
 * @version： V1.0
 */
@Service
@Transactional
public class FbsAbnormalWorkingHoursServiceImpl extends ServiceImpl<FbsAbnormalWorkingHoursMapper, FbsAbnormalWorkingHours> implements IFbsAbnormalWorkingHoursService {

    @Resource
    private FbsAbnormalWorkingHoursMapper fbsAbnormalWorkingHoursMapper;

    @Resource
    private IFbsAbnormalWorkingHoursDescribeService iFbsAbnormalWorkingHoursDescribeService;

    @Resource
    private IFbsAbnormalWorkingHoursTypeService iFbsAbnormalWorkingHoursTypeService;
    @Resource
    private IFbsAbnormalWorkingHoursService iFbsAbnormalWorkingHoursService;

    @Resource
    private FbsWorkshopDispatchListMapper fbsWorkshopDispatchListMapper;

    @Override
    public boolean addAndChild(FbsAbnormalWorkingHours fbsAbnormalWorkingHours) {
        boolean resulet=true;
        //新增主表
        fbsAbnormalWorkingHoursMapper.insert(fbsAbnormalWorkingHours);

        //新增子表
        addChild(fbsAbnormalWorkingHours);

        return resulet;
    }

    /**
    * @Author chenchuang
    * @Description //TODO 新增子表
    * @Date 2020/10/14 15:21
    * @Param [fbsAbnormalWorkingHours]
    * @return void
    */
    public void addChild(FbsAbnormalWorkingHours fbsAbnormalWorkingHours){
        //新增异常工时描述
        if(fbsAbnormalWorkingHours.getFbsAbnormalWorkingHoursDescribeList()!=null){
            Integer sort=1;
            for(FbsAbnormalWorkingHoursDescribe fbsAbnormalWorkingHoursDescribe:fbsAbnormalWorkingHours.getFbsAbnormalWorkingHoursDescribeList()){
                fbsAbnormalWorkingHoursDescribe.setId(null);
                fbsAbnormalWorkingHoursDescribe.setAbnormalWorkingHoursId(fbsAbnormalWorkingHours.getId());
                fbsAbnormalWorkingHoursDescribe.setSort(sort);
                sort++;
            }
            //批量保存
            iFbsAbnormalWorkingHoursDescribeService.saveBatch(fbsAbnormalWorkingHours.getFbsAbnormalWorkingHoursDescribeList());
        }

        //新增异常类别
        if(fbsAbnormalWorkingHours.getFbsAbnormalWorkingHoursTypeList()!=null){
            Integer sort=1;
            for(FbsAbnormalWorkingHoursType fbsAbnormalWorkingHoursType:fbsAbnormalWorkingHours.getFbsAbnormalWorkingHoursTypeList()){
                fbsAbnormalWorkingHoursType.setId(null);
                fbsAbnormalWorkingHoursType.setAbnormalWorkingHoursId(fbsAbnormalWorkingHours.getId());
                fbsAbnormalWorkingHoursType.setSort(sort);
                sort++;
            }
            //批量保存
            iFbsAbnormalWorkingHoursTypeService.saveBatch(fbsAbnormalWorkingHours.getFbsAbnormalWorkingHoursTypeList());
        }
    }

    @Override
    public boolean updateAndChild(FbsAbnormalWorkingHours fbsAbnormalWorkingHours) {
        boolean resulet=true;
        //新增主表
        fbsAbnormalWorkingHoursMapper.updateById(fbsAbnormalWorkingHours);
        //物理删除子表旧数据
        deleteChild(fbsAbnormalWorkingHours);
        //新增子表
        addChild(fbsAbnormalWorkingHours);


        return resulet;
    }



    @Autowired
    IFbsPersonService iFbsPersonService;


    /**
    * @Author chenchuang
    * @Description //TODO 物理删除子表数据
    * @Date 2020/10/14 15:33
    * @Param [fbsAbnormalWorkingHours]
    * @return void
    */
    public void deleteChild(FbsAbnormalWorkingHours fbsAbnormalWorkingHours){
        //物理删除异常工时描述
        LambdaQueryWrapper<FbsAbnormalWorkingHoursDescribe> describeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        describeLambdaQueryWrapper.eq(FbsAbnormalWorkingHoursDescribe::getAbnormalWorkingHoursId,fbsAbnormalWorkingHours.getId());
        describeLambdaQueryWrapper.eq(FbsAbnormalWorkingHoursDescribe::getDelFlag,"0");
        FbsAbnormalWorkingHoursDescribe fbsAbnormalWorkingHoursDescribe=new FbsAbnormalWorkingHoursDescribe();
        fbsAbnormalWorkingHoursDescribe.setDelFlag("1");
        iFbsAbnormalWorkingHoursDescribeService.update(fbsAbnormalWorkingHoursDescribe,describeLambdaQueryWrapper);
        //物理删除异常类别
        LambdaQueryWrapper<FbsAbnormalWorkingHoursType> typeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        typeLambdaQueryWrapper.eq(FbsAbnormalWorkingHoursType::getAbnormalWorkingHoursId,fbsAbnormalWorkingHours.getId());
        typeLambdaQueryWrapper.eq(FbsAbnormalWorkingHoursType::getDelFlag,"0");
        FbsAbnormalWorkingHoursType fbsAbnormalWorkingHoursType=new FbsAbnormalWorkingHoursType();
        fbsAbnormalWorkingHoursType.setDelFlag("1");
        iFbsAbnormalWorkingHoursTypeService.update(fbsAbnormalWorkingHoursType,typeLambdaQueryWrapper);
    }


    @Override
    public FbsAbnormalWorkingHours getAbnormalWorkingHoursReport() {
        FbsAbnormalWorkingHours fbsAbnormalWorkingHoursResult=new FbsAbnormalWorkingHours();
        //获取开始时间结束时间 当前日期前15天时间
        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());
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
//        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<Date> dateList=startDateAndEndDate(startTime,endTime);
        //查询全部异常工时
        QueryWrapper<FbsAbnormalWorkingHours> fbsAbnormalWorkingHoursLambdaQueryWrapper=new QueryWrapper<>();
        fbsAbnormalWorkingHoursLambdaQueryWrapper.lambda().eq(FbsAbnormalWorkingHours::getDelFlag,"0");
        SimpleDateFormat fmts = new SimpleDateFormat("yyyy-MM");
        String now=fmts.format(new Date());
        fbsAbnormalWorkingHoursLambdaQueryWrapper.eq("DATE_FORMAT(date,'%Y-%m')",now);
        List<FbsAbnormalWorkingHours> fbsAbnormalWorkingHoursList=iFbsAbnormalWorkingHoursService.list(fbsAbnormalWorkingHoursLambdaQueryWrapper);
        //获取其全部主表id
        List<String> hourIdList=new ArrayList<>();
        if(fbsAbnormalWorkingHoursList!=null){
            for(FbsAbnormalWorkingHours fbsAbnormalWorkingHours:fbsAbnormalWorkingHoursList){
                hourIdList.add(fbsAbnormalWorkingHours.getId());
            }
        }
        //根据主表查询异常描述子表
        LambdaQueryWrapper<FbsAbnormalWorkingHoursDescribe> fbsAbnormalWorkingHoursDescribeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        fbsAbnormalWorkingHoursDescribeLambdaQueryWrapper.eq(FbsAbnormalWorkingHoursDescribe::getDelFlag,"0");
        if(hourIdList.size()<=0){
//            hourIdList=null;
            hourIdList.add("0");
        }
        fbsAbnormalWorkingHoursDescribeLambdaQueryWrapper.in(FbsAbnormalWorkingHoursDescribe::getAbnormalWorkingHoursId,hourIdList);
        List<FbsAbnormalWorkingHoursDescribe> fbsAbnormalWorkingHoursDescribeList=iFbsAbnormalWorkingHoursDescribeService.list(fbsAbnormalWorkingHoursDescribeLambdaQueryWrapper);
        getDescribeList(fbsAbnormalWorkingHoursList,fbsAbnormalWorkingHoursDescribeList);

        List<FbsAbnormalWorkingHours> fbsAbnormalWorkingHoursListAdd=new ArrayList<>();
        if(dateList!=null){//是否为空
            for(Date date:dateList){
                String dateString=fmt.format(date);//时间
                boolean ish=false;//是否找到时间对应的异常工时单
                if(fbsAbnormalWorkingHoursList!=null){
                    for (FbsAbnormalWorkingHours fbsAbnormalWorkingHours:fbsAbnormalWorkingHoursList){
                        String  hdate=fmt.format(fbsAbnormalWorkingHours.getDate());
                        if(dateString.equals(hdate)){//找到时间对应的时间工时单
                            fbsAbnormalWorkingHoursListAdd.add(fbsAbnormalWorkingHours);
                            ish=true;
                        }
                    }
                }
                //如果没找到则添加空对象
                if(ish==false){
                    FbsAbnormalWorkingHours fbsAbnormalWorkingHours=new FbsAbnormalWorkingHours();
                    fbsAbnormalWorkingHours.setDate(date);
                    fbsAbnormalWorkingHoursListAdd.add(fbsAbnormalWorkingHours);
                }
            }
        }
        fbsAbnormalWorkingHoursResult.setFbsAbnormalWorkingHoursList(fbsAbnormalWorkingHoursListAdd);
        return fbsAbnormalWorkingHoursResult;
    }

    /**
    * @Author chenchuang
    * @Description //TODO 根据主表id获取子表描述数据
    * @Date 2020/10/17 18:14
    * @Param [fbsAbnormalWorkingHoursList, fbsAbnormalWorkingHoursDescribeList]
    * @return void
    */
    public void getDescribeList(List<FbsAbnormalWorkingHours> fbsAbnormalWorkingHoursList,List<FbsAbnormalWorkingHoursDescribe> fbsAbnormalWorkingHoursDescribeList){
        //判断是否为空
        if(fbsAbnormalWorkingHoursList!=null&&fbsAbnormalWorkingHoursDescribeList!=null){
            //循环主表
            for(FbsAbnormalWorkingHours fbsAbnormalWorkingHours:fbsAbnormalWorkingHoursList){
                List<FbsAbnormalWorkingHoursDescribe> fbsAbnormalWorkingHoursDescribeListAdd=new ArrayList<>();
                //循环子表
                for(FbsAbnormalWorkingHoursDescribe fbsAbnormalWorkingHoursDescribe:fbsAbnormalWorkingHoursDescribeList){
                    //根据主表id查询子表数据
                    if(fbsAbnormalWorkingHours.getId().equals(fbsAbnormalWorkingHoursDescribe.getAbnormalWorkingHoursId())){
                        fbsAbnormalWorkingHoursDescribeListAdd.add(fbsAbnormalWorkingHoursDescribe);
                    }
                }
                fbsAbnormalWorkingHours.setFbsAbnormalWorkingHoursDescribeList(fbsAbnormalWorkingHoursDescribeListAdd);
            }
        }

    }

    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;
    }


    @Override
    public List<FbsWorkshopDispatchList> getDispatchListByDate(String date) {
        return fbsWorkshopDispatchListMapper.getDispatchListByDate(date);
    }

    @Override
    public Map<String, Object> getQualityType() {
        Map<String, Object> map=new HashMap<>();
        //查询出按异常类别分类的质量异常工时
        List<FbsAbnormalWorkingHours> fbsAbnormalWorkingHoursList=fbsAbnormalWorkingHoursMapper.getQualityType();
        if(fbsAbnormalWorkingHoursList!=null){
            for(FbsAbnormalWorkingHours fbsAbnormalWorkingHours:fbsAbnormalWorkingHoursList){
                map.put(fbsAbnormalWorkingHours.getTypeName(),fbsAbnormalWorkingHours.getQuality());
            }
        }
        return map;
    }

    @Override
    public List<FbsAbnormalWorkingHours> getRejectsNumber(String dateString) {
        return fbsAbnormalWorkingHoursMapper.getRejectsNumber(dateString);
    }

    @Override
    public List<FbsAbnormalWorkingHours> getRejectsNumberSameDay() {
        return fbsAbnormalWorkingHoursMapper.getRejectsNumberSameDay();
    }

    @Override
    public List<Map<String, Object>> getMonthAttendance() {
        List<Map<String, Object>> mapList=new ArrayList<>();
        //查询月份分组的出勤数据
        List<FbsAbnormalWorkingHours> fbsAbnormalWorkingHoursList=fbsAbnormalWorkingHoursMapper.getMonthAttendance();
        for(int i=1;i<=12;i++){
            String month="";
            //获取月份字符
            if(i<10){
                month="0"+i;
            }else{
                month=i+"";
            }
            boolean ismonth=false;//是否该月的数据
            if(fbsAbnormalWorkingHoursList!=null){
                for(FbsAbnormalWorkingHours fbsAbnormalWorkingHours:fbsAbnormalWorkingHoursList){
                    //查询是否有
                    if(month.equals(fbsAbnormalWorkingHours.getMonths())){
                        ismonth=true;
                        Map<String, Object> map=new HashMap<>();
                        map.put("label",month);
                        map.put("sum1",fbsAbnormalWorkingHours.getActualNumberOfPeople());
                        map.put("sum2",fbsAbnormalWorkingHours.getNumberOfPeopleToArrive());
                        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;
    }

}
