package cn.gyee.tamplate.controller;


import cn.gyee.tamplate.model.auto.Benchmarkingbetween;
import cn.gyee.tamplate.model.auto.Dutyschedule;
import cn.gyee.tamplate.model.vo.DutyVo;
import cn.gyee.tamplate.service.IDutyscheduleService;
import cn.gyee.tamplate.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 谢生杰
 * @since 2021-01-18
 */
@RestController
@RequestMapping("//dutyschedule")
public class DutyscheduleController {
    @Resource
    private IDutyscheduleService dutyscheduleService;


    @PostMapping("/saveplus")
    @ResponseBody
    @CrossOrigin(origins = "*", maxAge = 3600)
    public void saveplus(@RequestBody List<DutyVo> dutys){
        String begin = null;
        String end = null;
        for (DutyVo duty : dutys) {

            if (duty.getBc().equals("上午")){
                begin = "8:00";
                end = "13:59";
            }else if (duty.getBc().equals("下午")){
                begin = "14:00";
                end = "19:59";
            }else{
                begin = "0:00,20:00";
                end = "7:59,23:59";
            }
            Date theday = DateUtils.parseDate(duty.getRq());
            Dutyschedule dutyschedule = new Dutyschedule();
            dutyschedule.setBegin(begin);
            dutyschedule.setEnd(end);
            dutyschedule.setCreatedate(theday);
            dutyschedule.setName(duty.getZj());
            dutyscheduleService.save(dutyschedule);
        }

    }


    @GetMapping("/save")
    @ResponseBody
    @CrossOrigin(origins = "*", maxAge = 3600)
    public void save(@RequestParam(value = "dutyname",required = true)String dutyname,
                                          @RequestParam(value = "frequency",required = true)String frequency,
                                          @RequestParam(value = "date",required = true)String date){
        String begin = null;
        String end = null;
        if (frequency.equals("上午")){
            begin = "8:00";
            end = "13:59";
        }else if (frequency.equals("下午")){
            begin = "14:00";
            end = "19:59";
        }else{
            begin = "0:00,20:00";
            end = "7:59,23:59";
        }
        Date theday = DateUtils.parseDate(date);
        Dutyschedule dutyschedule = new Dutyschedule();
        dutyschedule.setBegin(begin);
        dutyschedule.setEnd(end);
        dutyschedule.setCreatedate(theday);
        dutyschedule.setName(dutyname);
        dutyscheduleService.save(dutyschedule);

    }

    @GetMapping("/duty")
    @ResponseBody
    @CrossOrigin(origins = "*", maxAge = 3600)
    public List<DutyVo> list(@RequestParam(value = "beginDate",required = true)String beginDate,
                             @RequestParam(value = "endDate",required = true)String endDate){
        List<DutyVo> resultList = new ArrayList<>();
        Date begin=DateUtils.parseDate(beginDate);
        Date end = DateUtils.parseDate(endDate);
        QueryWrapper<Dutyschedule> wrapper = new QueryWrapper();
        //wrapper.select("max(id) id,name,max(begin) begin,max(end) end,max(createdate) createdate");
        wrapper.le("createdate",end).ge("createdate",begin);
        List<Dutyschedule> dutyschedulesList=dutyscheduleService.list(wrapper);
        dutyschedulesList.stream().forEach(i -> {
            DutyVo vo = new DutyVo();
            vo.setZj(i.getName());
            vo.setRq(DateUtils.toDate(i.getCreatedate()));
            if (i.getBegin().equals("8:00")){
                vo.setBc("上午");
            }else if(i.getBegin().equals("14:00")){
                vo.setBc("下午");
            }else{
                vo.setBc("晚上");
            }
            resultList.add(vo);
        });
        return resultList;
    }

    @GetMapping("/add")
    @CrossOrigin(origins = "*", maxAge = 3600)
    public void add(@RequestParam(value = "name",required = true)String name,
                    @RequestParam(value = "bc",required = true)String bc,
                    @RequestParam(value = "date",required = true)String date){


        Dutyschedule duty = new Dutyschedule();
        duty.setName(name);
        duty.setCreatedate(DateUtils.parseDate(date));
        String begin = null;
        String end = null;
        if (bc.equals("上午")){
            begin = "8:00";
            end = "13:59";
        }else if (bc.equals("下午")){
            begin = "14:00";
            end = "19:59";
        }else{
            begin = "0:00,20:00";
            end = "7:59,23:59";
        }
        duty.setBegin(begin);
        duty.setEnd(end);
        //dutyscheduleService.saveOrUpdate()
        dutyscheduleService.save(duty);
    }

}
