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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;

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.itdmGongdanDetail.entity.ItdmGongdanDetail;
import org.jeecg.modules.itdmGongdanDetail.mapper.ItdmGongdanDetailMapper;
import org.jeecg.modules.itdmGongdanMaster.entity.ItdmGongdanMaster;
import org.jeecg.modules.itdmGongdanMaster.mapper.ItdmGongdanMasterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
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
    private ItdmGongdanMasterMapper masterMapper;

    @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));


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


    }


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