package com.jeeplus.modules.projectplanweekly.service;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.MenuStatusEnum;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.projectplanweekly.dao.ProjectPlanWeeklyDao;
import com.jeeplus.modules.projectplanweekly.entity.ProjectPlanWeekly;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(readOnly = true)
public class ProjectPlanWeeklyService extends CrudService<ProjectPlanWeeklyDao, ProjectPlanWeekly> {
    @Autowired
    private ProjectPlanWeeklyDao projectPlanWeeklyDao;

    //获取单条记录
    public ProjectPlanWeekly get(String id){
       return super.get(id);
    }

    @Transactional(readOnly = false)
    public void save(ProjectPlanWeekly projectPlanWeekly){
        super.save(projectPlanWeekly);
    }

    @Transactional(readOnly = false)
    public void delete(ProjectPlanWeekly projectPlanWeekly){
        super.delete(projectPlanWeekly);
    }

    @Transactional(readOnly = false)
    public void updateWeekly(ProjectPlanWeekly projectPlanWeekly){
        projectPlanWeekly.preUpdate();
        projectPlanWeeklyDao.updateWeekly(projectPlanWeekly);
    }

    public List<ProjectPlanWeekly> findWeekly(ProjectPlanWeekly projectPlanWeekly){
       return projectPlanWeeklyDao.findWeekly(projectPlanWeekly);
    }

    public ProjectPlanWeekly getPlanWeekly(String id){
        return  projectPlanWeeklyDao.getPlanWeekly(id);
    }
    /**
     * 查询分页数据
     * @param page 分页对象
     * @param entity
     * @return
     */
    public Page<ProjectPlanWeekly> findPageWeekly(Page<ProjectPlanWeekly> page, ProjectPlanWeekly entity) {
        //设置数据权限
        if(!UserUtils.getUser().isAdmin()) {
            String dataScopeSql = dataScopeFilterOR(entity.getCurrentUser(), "o", "u", "s", MenuStatusEnum.WORK_REGULARAPPLY.getValue());
            entity.getSqlMap().put("dsf", dataScopeSql);
        }
        if (StringUtils.isBlank(entity.getWeeklyStatus())){
            entity.setWeeklyStatus("0");
        }
        entity.setPage(page);
        List<ProjectPlanWeekly> weekly = new ArrayList<>();
        if (0 != entity.getSqlMap().size()){
            String dataSql = entity.getSqlMap().get("dsf");
            boolean boolResult = dataSql.contains("create_by");
            if (boolResult){
                weekly = projectPlanWeeklyDao.findWeeklyByStaff(entity);
            }else{
                weekly = projectPlanWeeklyDao.findWeekly(entity);
            }
        }else{
            weekly = projectPlanWeeklyDao.findWeekly(entity);
        }
        page.setList(weekly);
        return page;
    }


    //计算当前日期所在的星期一或者星期日
    public Map<String,Date> getMondayAndSunday(Date nowDate){

        //获取当前日期
        Date time = new Date(nowDate.getYear(),nowDate.getMonth(),nowDate.getDate());
        //设置时间格式

        //获取日历
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        //判断是否为周日
        int dayWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (1==dayWeek){
            calendar.add(Calendar.DAY_OF_MONTH,-1);
        }
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        //获取当前日期市一个星期的第几天
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        //给当前日期减去星期几与一个星期第一天的差值
        calendar.add(Calendar.DATE,calendar.getFirstDayOfWeek()-day);
        calendar.set(Calendar.HOUR,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        //所在本周星期一的日期
        Date mondayDate = calendar.getTime();

        calendar.add(Calendar.DATE, 6);
        calendar.set(Calendar.HOUR, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        //所在本周星期日的日期
        Date sundayDate = calendar.getTime();

        //存入map集合中
        Map<String,Date> map = new HashMap<String, Date>();
        map.put("mondayDate",mondayDate);
        map.put("sundayDate",sundayDate);
        return map;
    }

    //添加未完成原因
    @Transactional(readOnly = false)
    public void saveRemarksReason(ProjectPlanWeekly projectPlanWeekly){
        projectPlanWeekly.preUpdate();
        projectPlanWeeklyDao.saveRemarksReason(projectPlanWeekly);
    }

    @Transactional(readOnly = false)
    public void saveRemarksReasonInfo(ProjectPlanWeekly projectPlanWeekly){
        projectPlanWeekly.preUpdate();
        projectPlanWeeklyDao.saveRemarksReasonInfo(projectPlanWeekly);
    }

    //修改周报表状态
    @Transactional(readOnly = false)
    public void sureWeekly(ProjectPlanWeekly projectPlanWeekly){
        projectPlanWeekly.preUpdate();
        projectPlanWeeklyDao.sureWeekly(projectPlanWeekly);
    }

    //修改周报表状态
    @Transactional(readOnly = false)
    public void sureWeeklyInfo(ProjectPlanWeekly projectPlanWeekly){
        projectPlanWeekly.preUpdate();
        projectPlanWeeklyDao.sureWeeklyInfo(projectPlanWeekly);
    }
    //删除周报
    @Transactional(readOnly = false)
    public void deleteWeekly(ProjectPlanWeekly projectPlanWeekly){
        projectPlanWeekly.preUpdate();
        projectPlanWeeklyDao.deleteWeekly(projectPlanWeekly);
    }
    @Transactional(readOnly = false)
    public void deleteWeeklyInfo(ProjectPlanWeekly projectPlanWeekly){
        projectPlanWeekly.preUpdate();
        projectPlanWeeklyDao.deleteWeeklyInfo(projectPlanWeekly);
    }

}
