package com.repair.controller;


import com.repair.api.RepairShiftSettingsControllerAPI;
import com.repair.common.utils.TimeExchange;
import com.repair.common.utils.paramUtils;
import com.repair.model.pojo.RepairShiftSettings;
import com.repair.model.request.insertRepairShiftSettingRequest;
import com.repair.model.request.timeVo;
import com.repair.model.request.updateRepairShiftRequest;
import com.repair.model.result.CommonResult;
import com.repair.model.result.PageUtils;
import com.repair.services.RepairShiftSettingsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-07-13
 */
@RestController
public class RepairShiftSettingsController implements RepairShiftSettingsControllerAPI {

    @Autowired
    private RepairShiftSettingsService repairShiftSettingsService;

    /**
     * 新增班次
     * @param irssr 班次数据
     * @param bindingResult 是否为空判断
     * @return
     * @throws ParseException
     */
    @Override
    public CommonResult InsertRepairShiftSettings(insertRepairShiftSettingRequest irssr, BindingResult bindingResult) throws ParseException {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        if (!TimeExchange.CompareDate(irssr.getStartTime(), irssr.getEndTime(), "HH:mm")) {
            return CommonResult.fail("开始时间不能大于结束时间");
        }

        List<timeVo> tvs = new ArrayList<>();
        //非工作时间必须都在结束时间之内 防止非工作时间重叠
        for (String i : irssr.getNotWorkTime()) {
            String[] time = i.split("-");
            if(TimeExchange.CompareDate(time[0],time[1],"HH:mm")){
                timeVo tv = new timeVo();
                tv.setStart(time[0]);
                tv.setEnd(time[1]);
                tvs.add(tv);
            }
        }

        for (int i = 0;i<tvs.size();i++){
            for (int j = i+1;j<tvs.size();j++){
                boolean isOver = TimeExchange.overlapping(tvs.get(i).getStart(),tvs.get(i).getEnd(),tvs.get(j).getStart(),tvs.get(j).getEnd());
                if(isOver){
                    return CommonResult.fail(tvs.get(i).getStart()+"-"+tvs.get(i).getEnd()+"时间段和"+tvs.get(j).getStart()+"-"+tvs.get(j).getEnd()+"时间段存在重叠");
                }
            }
        }

        //非工作时间的最小时间不能小于起止时间的开始时间
        //非工作时间的最大时间不能大于起止时间的结束时间
        tvs.sort(Comparator.comparing(timeVo::getStart));
        if(tvs.size() > 0){
            if(TimeExchange.CompareDate(tvs.get(0).getStart(),irssr.getStartTime(),"HH:mm")){
                return CommonResult.fail(tvs.get(0).getStart()+"非工作时间段不能小于开始工作时间"+irssr.getStartTime());
            }
            if(TimeExchange.CompareDate(irssr.getEndTime(),tvs.get(tvs.size() -1).getEnd(),"HH:mm")){
                return CommonResult.fail(tvs.get(tvs.size() -1).getEnd()+"非工作时间段不能大于工作结束时间"+irssr.getEndTime());
            }
        }

        RepairShiftSettings rss = new RepairShiftSettings();
        rss.setName(irssr.getName());
        rss.setStartTime(irssr.getStartTime());
        rss.setEndTime(irssr.getEndTime());
        rss.setNotWorkTime(StringUtils.join(irssr.getNotWorkTime(), ','));
        rss.setIsDuty(irssr.getIsDuty());
        rss.setColor(irssr.getColor());

        int result = repairShiftSettingsService.insertRepairShiftSettings(rss);

        return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
    }

    public static void main(String[] args) throws ParseException {
        List<timeVo> tvs = new ArrayList<>();
        timeVo tv = new timeVo();
        tv.setStart("5:00");
        tv.setEnd("6:00");
        tvs.add(tv);

        timeVo tv2 = new timeVo();
        tv2.setStart("11:00");
        tv2.setEnd("12:00");
        tvs.add(tv2);

        timeVo tv1 = new timeVo();
        tv1.setStart("7:00");
        tv1.setEnd("11:30");
        tvs.add(tv1);


        for (int i = 0;i<tvs.size();i++){
            for (int j = i+1;j<tvs.size();j++){
                boolean isOver = TimeExchange.overlapping(tvs.get(i).getStart(),tvs.get(i).getEnd(),tvs.get(j).getStart(),tvs.get(j).getEnd());
                if(isOver){
                    String sss = tvs.get(i).getStart()+"-"+tvs.get(i).getEnd()+"时间段和"+tvs.get(j).getStart()+"-"+tvs.get(j).getEnd()+"时间段存在重叠";
                    String ssds = "";
                }
            }
        }
    }

    /**
     * 更新班次数据
     * @param ursr 班次数据
     * @param bindingResult 是否为空判断
     * @return
     * @throws ParseException
     */
    @Override
    public CommonResult updateRepairShiftSettingsById(updateRepairShiftRequest ursr, BindingResult bindingResult) throws ParseException {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        if (!TimeExchange.CompareDate(ursr.getStartTime(), ursr.getEndTime(), "HH:mm")) {
            return CommonResult.fail("开始时间不能大于结束时间");
        }

        List<timeVo> tvs = new ArrayList<>();
        //非工作时间必须都在结束时间之内 防止非工作时间重叠
        for (String i : ursr.getNotWorkTime()) {
            String[] time = i.split("-");
            if(TimeExchange.CompareDate(time[0],time[1],"HH:mm")){
                timeVo tv = new timeVo();
                tv.setStart(time[0]);
                tv.setEnd(time[1]);
                tvs.add(tv);
            }
        }

        for (int i = 0;i<tvs.size();i++){
            for (int j = i+1;j<tvs.size();j++){
                boolean isOver = TimeExchange.overlapping(tvs.get(i).getStart(),tvs.get(i).getEnd(),tvs.get(j).getStart(),tvs.get(j).getEnd());
                if(isOver){
                    return CommonResult.fail(tvs.get(i).getStart()+"-"+tvs.get(i).getEnd()+"时间段和"+tvs.get(j).getStart()+"-"+tvs.get(j).getEnd()+"时间段存在重叠");
                }
            }
        }

        //非工作时间的最小时间不能小于起止时间的开始时间
        //非工作时间的最大时间不能大于起止时间的结束时间
        tvs.sort(Comparator.comparing(timeVo::getStart));
        if(tvs.size() > 0){
            if(TimeExchange.CompareDate(tvs.get(0).getStart(),ursr.getStartTime(),"HH:mm")){
                return CommonResult.fail(tvs.get(0).getStart()+"非工作时间段不能小于开始工作时间"+ursr.getStartTime());
            }
            if(TimeExchange.CompareDate(ursr.getEndTime(),tvs.get(tvs.size() -1).getEnd(),"HH:mm")){
                return CommonResult.fail(tvs.get(tvs.size() -1).getEnd()+"非工作时间段不能大于工作结束时间"+ursr.getEndTime());
            }
        }

        RepairShiftSettings ra = new RepairShiftSettings();
        ra.setId(ursr.getId());
        ra.setName(ursr.getName());
        ra.setStartTime(ursr.getStartTime());
        ra.setEndTime(ursr.getEndTime());
        ra.setNotWorkTime(StringUtils.join(ursr.getNotWorkTime(), ','));
        ra.setColor(ursr.getColor());
        ra.setIsDuty(ursr.getIsDuty());

        int result = repairShiftSettingsService.updateRepairShiftSettings(ra);

        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    /**
     * 班次分页数据查询
     * @param currentPage 当前页数
     * @param pageCount 一页数据条数
     * @param name 查询名称
     * @return
     */
    @Override
    public CommonResult queryPageRepairShiftSettingss(int currentPage, int pageCount, String name) {
        PageUtils<RepairShiftSettings> result = repairShiftSettingsService.queryPageRepairShiftSettingss(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }

    /**
     * 根据数据ID删除班次数据
     * @param id 数据ID
     * @return
     */
    @Override
    public CommonResult deleteRepairShiftSettingsById(int id) {
        RepairShiftSettings data = repairShiftSettingsService.getRepairById(id);
        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = repairShiftSettingsService.deleteRepairShiftSettingsById(id);

        return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
    }
}

