package com.ssj.service.conch.curator.service.impl.v1;

import com.ssj.bean.curator.TbVacationLimit;
import com.ssj.dao.conch.curator.dao.IVacationLimitDao;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.conch.curator.service.v1.IVacationLimitService;
import com.ssj.service.conch.parents.v1.dto.VacationDto;
import com.ssj.service.conch.parents.v1.request.VacationSaveReq;
import com.ssj.service.weixin.sales.service.LibVipServiceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class VacationLimitServiceImpl extends BaseServiceImpl<TbVacationLimit, String> implements IVacationLimitService {

    @Autowired
    private IVacationLimitDao dao;

    @Autowired
    private LibVipServiceService vipServiceService;

    @Override
    public PagingAndSortingRepository<TbVacationLimit, String> getDao() {
        return dao;
    }

    @Override
    public List<TbVacationLimit> findValidVocationLimit(Date beginTime, Date endTime, String libId) {
        return dao.findValidVocationLimit(beginTime, endTime, libId);
    }

    @Override
    public ResponseEntity vacationList(String libId) {
        Map<String,Object> data = new HashMap<>();
        String year = DateHelper.format(new Date(),"yyyy");
        List<TbVacationLimit> list = dao.findLibVacationData(libId,year);
        if(list.isEmpty()){
            //初始化数据
            list = new ArrayList<>();
            for(int i=1;i<5;i++){
                TbVacationLimit vacation = new TbVacationLimit();
                vacation.setLibId(libId);
                vacation.setType(i);
                vacation.setLimitYear(year);
                vacation.setLimitMsg(i==1?"开始时间不能超过1月2月":i==2?"日期不超过7天,开始时间不能超过4月、5月":i==3?"开始时间不能超过7月、8月":"日期不超过7天，开始时间不能超过9月、10月");
                vacation.setSetState(1);
                vacation = this.save(vacation);
                list.add(vacation);
            }
        }
        List<VacationDto> dtoList = new ArrayList<>();
        list.forEach(t->{
            VacationDto dto = new VacationDto();
            BeanUtils.copyProperties(t,dto);
            Date now = new Date();
            if(t.getStartDate()==null){
                dto.setIsEffect(2);
                dto.setStartDate("");
                dto.setEndDate("");
            }else{
                if(DateHelper.compareTwoDate(t.getStartDate(),now) || (DateHelper.compareTwoDate(t.getEndDate(),now) && DateHelper.compareTwoDate(now,t.getEndDate()) )){
                    dto.setIsEffect(1);
                }else {
                    dto.setIsEffect(2);
                }
                dto.setStartDate(DateHelper.format(t.getStartDate(),"yyyy-MM-dd"));
                dto.setEndDate(DateHelper.format(t.getEndDate(),"yyyy-MM-dd"));
            }
            dtoList.add(dto);
        });


        data.put("list",dtoList);
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.success(data,"获取数据成功");
        return responseEntity;
    }

    @Override
    public ResponseEntity vacationSetSave(VacationSaveReq req,String userId) {
        ResponseEntity responseEntity = new ResponseEntity();
        String year = DateHelper.format(new Date(),"yyyy");
        try{
            TbVacationLimit vacation = this.getById(req.getId());
            //原有的假日设置
            TbVacationLimit oldVacation = new TbVacationLimit();
            BeanUtils.copyProperties(vacation, oldVacation);
            if(req.getSetState()==2) {   //开启
                //校验数据有效性
                String startDate = req.getStartDate();
                String endDate = req.getEndDate();
                if(startDate.compareTo(DateHelper.format(new Date(),"yyyy-MM-dd"))<0){

                    responseEntity.failure("000","假期开始时间不能早于今天");
                    return  responseEntity;
                }
                if(vacation.getType()==1){ //寒假开始时间不能超过1月2月
                    String limitStartDate = year+"-01"+"-01";
                    String limitEndDate = year+"-03"+"-01";
                    if(startDate.compareTo(limitStartDate)<0 || startDate.compareTo(limitEndDate)>0 || endDate.compareTo(limitStartDate)<0 || endDate.compareTo(limitEndDate)>0){
                        responseEntity.failure("000","寒假时间不能超过1月2月");
                        return  responseEntity;
                    }
                }else if(vacation.getType()==2){ //五一 日期不超过7天,开始时间不能超过4月、5月
                    String limitStartDate = year+"-04"+"-01";
                    String limitEndDate = year+"-06"+"-01";
                    if(startDate.compareTo(limitStartDate)<0 || startDate.compareTo(limitEndDate)>0 || endDate.compareTo(limitStartDate)<0 || endDate.compareTo(limitEndDate)>0){
                        responseEntity.failure("000","五一时间不能超过4月、5月");
                        return  responseEntity;
                    }
                    if(DateHelper.daysBetween(DateHelper.parseDate(endDate,"yyyy-MM-dd"),DateHelper.parseDate(startDate,"yyyy-MM-dd"))>7){
                        responseEntity.failure("000","五一假期不能超过7天");
                        return  responseEntity;
                    }
                }else if(vacation.getType()==3){
                    String limitStartDate = year+"-07"+"-01";
                    String limitEndDate = year+"-09"+"-01";
                    if(startDate.compareTo(limitStartDate)<0 || startDate.compareTo(limitEndDate)>0 || endDate.compareTo(limitStartDate)<0 || endDate.compareTo(limitEndDate)>0){
                        responseEntity.failure("000","暑假时间不能超过7月8月");
                        return  responseEntity;
                    }
                }else if(vacation.getType()==4){
                    String limitStartDate = year+"-09"+"-01";
                    String limitEndDate = year+"-11"+"-01";
                    if(startDate.compareTo(limitStartDate)<0 || startDate.compareTo(limitEndDate)>0 || endDate.compareTo(limitStartDate)<0 || endDate.compareTo(limitEndDate)>0){
                        responseEntity.failure("000","国庆时间不能超过9月10月");
                        return  responseEntity;
                    }
                    if(DateHelper.daysBetween(DateHelper.parseDate(endDate,"yyyy-MM-dd"),DateHelper.parseDate(startDate,"yyyy-MM-dd"))>7){
                        responseEntity.failure("000","国庆假期不能超过7天");
                        return  responseEntity;
                    }
                }else{
                    responseEntity.failure("000","超出系统定义！");
                    return  responseEntity;
                }
            }

            if(vacation.getStartDate()!=null){
                if(DateHelper.compareTwoDate(vacation.getStartDate(),new Date())){
                    responseEntity.failure("000","设置已生效，不能修改！");
                    return  responseEntity;
                }
            }

            //处理保存数据
            //if(req.getSetState()==2){  //开启
                vacation.setStartDate(DateHelper.parseDate(req.getStartDate(),"yyyy-MM-dd"));
                vacation.setEndDate(DateHelper.parseDate(req.getEndDate(),"yyyy-MM-dd"));
            //}
            vacation.setUpdateTime(new Date());
            vacation.setUserId(userId);
            vacation.setSetState(req.getSetState());
            vacation = this.save(vacation);

            //处理有效服务数据延长或缩短
            vipServiceService.setLibService(vacation, oldVacation);

            responseEntity.success("保存数据成功");
        }catch (Exception e){
            e.printStackTrace();
            responseEntity.failure("500","保存处理数据出现异常！");
        }
        return responseEntity;
    }
}
