package com.securitycontrol.background.service.impl;

import cn.afterturn.easypoi.cache.manager.IFileLoader;
import com.securitycontrol.background.mapper.DutyPlanMapper;
import com.securitycontrol.background.service.DutyPlanService;
import com.securitycontrol.common.core.utils.aes.ListHelper;
import com.securitycontrol.common.core.utils.aes.StringHelper;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.entity.background.vo.DutyPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黑子
 */
@Service(value = "DutyPlanService")
@Slf4j
public class DutyPlanServiceImpl implements DutyPlanService {



    @Resource(name = "DutyPlanMapper")
    private DutyPlanMapper mapper;

    @Override
    public Map<String, Object> getTimeLists() {
        Map<String, Object> map=new HashMap<>();
        List<String> dates=new ArrayList<>();
        try{
            for(int i=-4;i<=4;i++){
                String monday= getWeekMon(new Date((System.currentTimeMillis())+(i*7*60*60*1000*24L)));
                String sunday= getWeekSun(new Date((System.currentTimeMillis())+(i*7*60*60*1000*24L)));
                dates.add(monday+"~"+sunday);
            }
            map.put("code",200);
            map.put("data",dates);
        }catch (Exception e){
            map.put("code",400);
        }
        return map;
    }

    @Override
    public AjaxResult addOrUpdateDutyPlan(DutyPlanVo vo) {
        try{
            if(StringHelper.isEmpty(vo.getCreateDay())){
                    return AjaxResult.error("日期不能未空");
            }
            vo.setType("1");
            //对上午时间进行判断
            int num= mapper.replaceDutyPlan(vo);
            //存储修改下午数据
            DutyPlanVo vo1=new DutyPlanVo();
            vo1.setType("2");
            vo1.setCreateDay(vo.getCreateDay());
            vo1.setMon(vo.getMon1());
            vo1.setTue(vo.getTue1());
            vo1.setWed(vo.getWed1());
            vo1.setThu(vo.getThu1());
            vo1.setFri(vo.getFri1());
            vo1.setSat(vo.getSat1());
            vo1.setSun(vo.getSun1());
           int num2= mapper.replaceDutyPlan(vo1);
            if(num>0 ||num2>0){
                return AjaxResult.success();
            }

        }catch (Exception e){
            AjaxResult.error("修改失败");
        }
        return AjaxResult.success();
    }

    @Override
    public List<DutyPlanVo> getDutyPlan(DutyPlanVo vo) {
        return mapper.getDutyPlan(vo);
    }


    /**
     * 获取本周周日日期
     * @return
     */
    public static String getWeekSun(Date date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 如果是周日直接返回
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            return sdf.format(date);
        }
        //System.out.println(c.get(Calendar.DAY_OF_WEEK));
        c.add(Calendar.DATE, 7 - c.get(Calendar.DAY_OF_WEEK) + 1);
        return sdf.format(c.getTime());
    }
    /**
     * 获取本周周一日期
     * @return
     */
    public static String getWeekMon(Date date) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            c.add(Calendar.DAY_OF_MONTH, -1);
        }
        c.add(Calendar.DATE, c.getFirstDayOfWeek() - c.get(Calendar.DAY_OF_WEEK) + 1);
        return sdf.format(c.getTime());
    }
}
