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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.gongdandetail.convert.GongdanDetailConvert;
import org.jeecg.modules.gongdandetail.dto.ItdmGongdanDetailEditDTO;
import org.jeecg.modules.gongdandetail.service.IItdmGongdanQuerenService;
import org.jeecg.modules.itdmDevice.entity.ItdmDevice;
import org.jeecg.modules.itdmDevice.mapper.ItdmDeviceMapper;
import org.jeecg.modules.itdmGongDan.entity.ItdmGongdanDetail;
import org.jeecg.modules.itdmGongDan.entity.ItdmGongdanMaster;
import org.jeecg.modules.itdmGongDan.mapper.ItdmGongdanDetailMapper;
import org.jeecg.modules.itdmGongDan.mapper.ItdmGongdanMasterMapper;
import org.jeecg.modules.itdmGongDan.mapper.ItdmTestPlanMapper;
import org.jeecg.modules.system.mapper.SysDictItemMapper;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @Description: 工单detail
 * @Author: jeecg-boot
 * @Date: 2023-05-21
 * @Version: V1.0
 */
@Service
public class ItdmGongdanQuerenServiceImpl extends ServiceImpl<ItdmGongdanDetailMapper, ItdmGongdanDetail> implements IItdmGongdanQuerenService {


    @Autowired
    @SuppressWarnings("all")
    private ItdmGongdanMasterMapper masterMapper;

    @Autowired
    @SuppressWarnings("all")
    private ItdmTestPlanMapper itdmTestPlanMapper;

    @Autowired
    @SuppressWarnings("all")
    private ItdmGongdanDetailMapper itdmGongdanDetailMapper;

    @Autowired
    @SuppressWarnings("all")

    private SysDictMapper sysDictItemMapper;

    @Autowired
    private ItdmDeviceMapper itdmDeviceMapper;

    @Override
    public void queren(ItdmGongdanDetailEditDTO itdmGongdanDetail) throws ParseException {

        ItdmGongdanDetail data = this.baseMapper.selectById(itdmGongdanDetail.getId());


        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDate1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        this.baseMapper.updateById(GongdanDetailConvert.INSTANCE.convert(itdmGongdanDetail));

        List<ItdmGongdanDetail> list = baseMapper.
                selectList(Wrappers.lambdaQuery(ItdmGongdanDetail.class).eq(ItdmGongdanDetail::getWoId, data.getWoId()));


        List<ItdmGongdanDetail> list1 = baseMapper.
                selectList(Wrappers.lambdaQuery(ItdmGongdanDetail.class).eq(ItdmGongdanDetail::getWoId, data.getWoId()).isNull(ItdmGongdanDetail::getShijiEndTime)
                        .isNull(ItdmGongdanDetail::getShijiStartTime).isNull(ItdmGongdanDetail::getShijiWorkDate));


        if (list1.size() > 0) {
            ItdmGongdanMaster master = new ItdmGongdanMaster();
            master.setId(data.getWoId());
            master.setShijiStartDate(getMin(list, simpleDate, simpleDate1));
            masterMapper.updateById(master);
        }


        if (list.size() > 0 && list1.size() < 1) {
            // todo
            ItdmGongdanMaster master = new ItdmGongdanMaster();
            master.setId(data.getWoId());
            master.setShijiEndDate(getMax(list, simpleDate, simpleDate1));
            master.setShijiStartDate(getMin(list, simpleDate, simpleDate1));
            masterMapper.updateById(master);
        }


    }


    @Override
    public List<String> getByweituoId(String weituoId) {
        return itdmTestPlanMapper.getByweituoNO(weituoId);
    }

    @Override
    public List<Map<String, String>> getDetailListByWoId(String woId, String riqi) {

        QueryWrapper<ItdmGongdanDetail> queryWrapper = new QueryWrapper<ItdmGongdanDetail>();
        queryWrapper.eq("wo_id", woId);
        queryWrapper.eq("shiji_work_date", riqi);
        List<ItdmGongdanDetail> list = itdmGongdanDetailMapper.selectList(queryWrapper);
        Set<String> stringSet = new HashSet<>();
        List<Map<String, String>> mapList = new ArrayList<>();
        for (ItdmGongdanDetail item : list) {
            stringSet.addAll(Arrays.asList(item.getShijiShiiyanType().split(",")));
        }


        for (String string : stringSet) {
            Map<String, String> map = new HashMap<>();
            map.put("type", string);
            map.put("name", sysDictItemMapper.queryDictTextByKey("shiiyan_type", string));


            mapList.add(map);
        }
        return mapList;
    }


    @Override
    public Map<String, String> getDetailListByWoId1(String woId, String riqi, String shiyanType, String shebeiId) {

        QueryWrapper<ItdmGongdanDetail> queryWrapper = new QueryWrapper<ItdmGongdanDetail>();
        queryWrapper.eq("wo_id", woId);
        queryWrapper.eq("shiji_work_date", riqi);
        queryWrapper.eq("shiji_shebei_id", shebeiId);
        queryWrapper.apply("FIND_IN_SET('" + shiyanType + "',shiji_shiiyan_type)");
        List<ItdmGongdanDetail> list = itdmGongdanDetailMapper.selectList(queryWrapper);
        Map<String, String> map = new HashMap<>();
        map.put("shiyanKaishitime", "00:00");
        map.put("shiyanJieshutime", "23:59");
        if (list.size() == 1) {

            map.put("shiyanKaishitime", list.get(0).getShijiStartTime());
            map.put("shiyanJieshutime", list.get(0).getShijiEndTime());
        }
        return map;
    }

    @Override
    public List<String> getWorkDateByWeituoNO(String weituoNo) {
        return itdmTestPlanMapper.getWorkDateByWeituoNO(weituoNo);
    }

    @Override
    public List<String> getWoIdByWeituoIdAndWordDate(String weituoNo, String work_date) {
        return itdmTestPlanMapper.getWoIdByWeituoIdAndWordDate(weituoNo, work_date);
    }


    @Override
    public List<Map<String, String>> gongdanShebeiList(String woId, String riqi, String shiyanType) {
        QueryWrapper<ItdmGongdanDetail> queryWrapper = new QueryWrapper<ItdmGongdanDetail>();
        queryWrapper.eq("wo_id", woId);
        queryWrapper.eq("shiji_work_date", riqi);
        queryWrapper.apply("FIND_IN_SET('" + shiyanType + "',shiji_shiiyan_type)");
        List<ItdmGongdanDetail> list = itdmGongdanDetailMapper.selectList(queryWrapper);

        List<Map<String, String>> mapList = new ArrayList<>();

        for (String i : list.stream().map(ItdmGongdanDetail::getShijiShebeiId).distinct().collect(Collectors.toList())) {

            ItdmDevice itdmDevice = itdmDeviceMapper.selectById(i);

            if (itdmDevice != null) {
                Map<String, String> map = new HashMap<>();

                map.put("type", i);
                map.put("name", itdmDevice.getDeviceName());
                mapList.add(map);
            }

        }

        return mapList;
    }

    private Date getMax(List<ItdmGongdanDetail> list, SimpleDateFormat simpleDate, SimpleDateFormat simpleDate1) {


        return list.stream()
                .filter(i -> i.getShijiWorkDate() != null && i.getShijiEndTime() != null)

                .map(i -> getDateFormat(i, i::getShijiEndTime, simpleDate, simpleDate1)).max(Date::compareTo).orElse(null);
    }


    private Date getMin(List<ItdmGongdanDetail> list, SimpleDateFormat simpleDate, SimpleDateFormat simpleDate1) {


        return list.stream().filter(i -> i.getShijiWorkDate() != null && i.getShijiStartTime() != null)

                .map(i -> getDateFormat(i, i::getShijiStartTime, simpleDate, simpleDate1)).min(Date::compareTo).orElse(null);


    }


    private Date getDateFormat(ItdmGongdanDetail getDateFormat, Supplier<String> supplier, SimpleDateFormat simpleDate, SimpleDateFormat simpleDate1) {

        try {
            return simpleDate1.parse(simpleDate.format(getDateFormat.getShijiWorkDate()) + " " + supplier.get());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
