package com.sunxung.factoring.web.weekWorkPlan;

import com.sunxung.factoring.component.annotation.OperationLog;
import com.sunxung.factoring.component.enums.OperationTypeEnum;
import com.sunxung.factoring.component.util.ConstantConversionUtil;
import com.sunxung.factoring.component.util.DateUtil;
import com.sunxung.factoring.dict.impl.WorkStateDict;
import com.sunxung.factoring.entity.weekWorkPlan.WeekWorkPlan;
import com.sunxung.factoring.entity.weekWorkPlan.vo.SearchBusinessProcessingVo;
import com.sunxung.factoring.entity.weekWorkPlan.vo.SearchWeekWorkPlan;
import com.sunxung.factoring.service.sys.DictionaryService;
import com.sunxung.factoring.service.weekWorkPlan.WeekWorkPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @Description:周工作计划Controller
 * @author:wuduanli
 * @e-mail:1912942707@qq.com
 * @date:2018年1月2日 下午4:08:24
 */
@Controller
public class WeekWorkPlanController {

    private static String PREFIX = "weekWorkPlan/";
    @Autowired
    private WeekWorkPlanService weekWorkPlanService;
    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 进入周工作计划列表数据展示页面. <br/>
     * 
     * @return
     */
    @RequestMapping(value = "/weekWorkPlan/list")
    public String list(Long userId, String workTime, Model model) {
        model.addAttribute("workStateList", dictionaryService.findByParentCode(WorkStateDict.CODE));
        model.addAttribute("overdue", WorkStateDict.ChildEnum.OVERDUE.getCode());
        model.addAttribute("userId", userId);
        model.addAttribute("workTime", workTime);
        return PREFIX + "weekWorkPlanList";
    }

    @RequestMapping("/weekWorkPlan/do_list")
    @ResponseBody
    public Object do_list(SearchWeekWorkPlan search) {
        return weekWorkPlanService.findWeekWorkPlan(search);
    }

    @RequestMapping(value = "/weekWorkPlan/add")
    public String add(Model model) {
        model.addAttribute("workStateList", dictionaryService.findByParentCode(WorkStateDict.CODE));
        model.addAttribute("overdue", WorkStateDict.ChildEnum.OVERDUE.getCode());
        return PREFIX + "weekWorkPlanAdd";
    }

    /**
     * 验证同一天内是否存在标题相同的工作计划. <br/>
     * 
     * @param plan
     * @return
     */
    @RequestMapping("/weekWorkPlan/do_validatorPlan")
    @ResponseBody
    public Object do_validatorPlan(WeekWorkPlan plan) {
        return weekWorkPlanService.validatorPlan(plan);
    }

    @RequestMapping("/weekWorkPlan/do_add")
    @ResponseBody
    @OperationLog(operationModule = "周工作计划",operationType = OperationTypeEnum.INSERT,description = "添加周工作计划")
    public Object do_add(WeekWorkPlan plan) {
        return weekWorkPlanService.add(plan);
    }

    /**
     * 获取周工作计划的所有状态. <br/>
     * 
     * @return
     */
    @RequestMapping(value = "/weekWorkPlan/do_getWorkState")
    @ResponseBody
    public Object do_getWorkState() {
        return dictionaryService.findByParentCode(WorkStateDict.CODE);
    }

    @RequestMapping(value = "/weekWorkPlan/update")
    public String update(Long id, Model model) {
        model.addAttribute("workStateList", dictionaryService.findByParentCode(WorkStateDict.CODE));
        model.addAttribute("overdue", WorkStateDict.ChildEnum.OVERDUE.getCode());
        model.addAttribute("weekWorkPlan", weekWorkPlanService.getDetail(id));
        return PREFIX + "weekWorkPlanUpdate";
    }

    @RequestMapping("/weekWorkPlan/do_update")
    @ResponseBody
    @OperationLog(operationModule = "周工作计划",operationType = OperationTypeEnum.UPDATE,description = "修改周工作计划")
    public Object do_update(WeekWorkPlan plan) {
        return weekWorkPlanService.update(plan);
    }

    @RequestMapping(value = "/weekWorkPlan/do_view")
    @ResponseBody
    public Object do_view(Long id, Model model) {
        WeekWorkPlan plan = weekWorkPlanService.getDetail(id);
        plan.setLevel(ConstantConversionUtil.levels.get(plan.getLevel()));
        return plan;
    }

    @RequestMapping("/weekWorkPlan/do_delete")
    @ResponseBody
    @OperationLog(operationModule = "周工作计划",operationType = OperationTypeEnum.DELETE,description = "删除周工作计划")
    public Object do_delete(Long id) {
        return weekWorkPlanService.delete(id);
    }

    /**
     * 进入月历展示页面. <br/>
     * 
     * @return
     */
    @RequestMapping(value = "/weekWorkPlan/calendarPage")
    public String calendarPage(Model model) {
        // 计算当前日期所在周一对应的年份（存在跨年的情况，主要根据周一的年份判断本周所属的年份）
        // 每周一的5点之前都查询的是上周计划作为本周计划，故当前时间减去5小时
        Date thisWeekStart = DateUtil.getFirstDayOfWeekClear(DateUtil.addHour(new Date(), -5));
        Integer weekYear = Integer.parseInt(DateUtil.dateToString(thisWeekStart, "yyyy").toString());
        model.addAttribute("weekYear", weekYear);
        // 封装当前系统时间所在对应周数
        Integer weekthNum = DateUtil.getWeekByDay(DateUtil.addHour(new Date(), -5));
        model.addAttribute("weekthNum", weekthNum);
        return PREFIX + "weekWorkPlanCalendarPage";
    }

    /**
     * 获取月历工作计划数据. <br/>
     * 
     * @param search
     * @return
     */
    @RequestMapping("/weekWorkPlan/do_findCalendarList")
    @ResponseBody
    public Object do_findCalendarList(SearchWeekWorkPlan search) {
        return weekWorkPlanService.findCalendarList(search);
    }

    /**
     * 获取每年对应周数及周对应的日期. <br/>
     * 
     * @param year
     * @return
     */
    @RequestMapping("/weekWorkPlan/do_getWeeks")
    @ResponseBody
    public Object do_getWeeks(Integer year) {
        return weekWorkPlanService.getWeeks(year);
    }

    /**
     * 获取周历工作计划数据. <br/>
     * 
     * @param search
     * @return
     */
    @RequestMapping("/weekWorkPlan/do_findWeekList")
    @ResponseBody
    public Object do_findWeekList(SearchWeekWorkPlan search) {
        return weekWorkPlanService.findWeekList(search);
    }

    /**
     * 进入周计划查看页面. <br/>
     * 
     * @return
     */
    @RequestMapping("/weekWorkPlan/weekPlanViewList")
    public String weekPlanList(Model model) {
        return PREFIX + "weekPlanViewList";
    }

    /**
     * 获取周计划查看页面列表数据. <br/>
     * 
     * @param search
     * @return
     */
    @RequestMapping("/weekWorkPlan/do_weekPlanList")
    @ResponseBody
    public Object do_weekPlanList(SearchWeekWorkPlan search) {
        return weekWorkPlanService.findWeekPlanList(search);
    }

    /**
     * 进入周例会页面. <br/>
     * 
     * @return
     */
    @RequestMapping("/weekWorkPlan/weeklyMeetingList")
    public String weeklyMeetingList(Model model) {
        model.addAttribute("businessModuleMap", weekWorkPlanService.getBusinessModule());
        return PREFIX + "weeklyMeeting";
    }

    /**
     * 获取周例会中上周工作计划统计数据. <br/>
     * 
     * @param search
     * @return
     */
    @RequestMapping("/weekWorkPlan/do_getLastWeekWorkPlanStatistics")
    @ResponseBody
    public Object do_getLastWeekWorkPlanStatistics(SearchWeekWorkPlan search) {
        return weekWorkPlanService.getLastWeekWorkPlanStatistics(search);
    }

    /**
     * 获取周例会中上周工作计划数据. <br/>
     * 
     * @param search
     * @return
     */
    @RequestMapping("/weekWorkPlan/do_findLastWeekWorkPlanList")
    @ResponseBody
    public Object do_findLastWeekWorkPlanList(SearchWeekWorkPlan search) {
        return weekWorkPlanService.findLastWeekWorkPlanList(search);
    }

    /**
     * 进入周例会中上周工作计划统计点击进入详情列表页面. <br/>
     * 
     * @param workStateId 状态ID
     * @return
     */
    @RequestMapping("/weekWorkPlan/weeklyMeetingPlanDetailList")
    public String weeklyMeetingPlanDetailList(Long workStateId, Model model) {
        model.addAttribute("workStateId", workStateId);
        return PREFIX + "weeklyMeetingPlanDetailList";
    }

    /**
     * 获取周例会中上周工作计划统计点击进入详情列表数据. <br/
     * 
     * @param search
     * @return
     */
    @RequestMapping("/weekWorkPlan/do_findLastWeekPlanDetailList")
    @ResponseBody
    public Object do_findLastWeekPlanDetailList(SearchWeekWorkPlan search) {
        return weekWorkPlanService.findLastWeekPlanDetailList(search);
    }

    /**
     * 获取当前系统所在时间. <br/>
     * 
     * @return
     */
    @RequestMapping("/weekWorkPlan/do_getNowDate")
    @ResponseBody
    public Date getNowDate() {
        return weekWorkPlanService.getNowDate();
    }

    /**
     * 周列会；上周业务进度统计
     */
    @GetMapping("/weekWorkPlan/doWeeklyMeetingLastBusinessProcessingStatistics")
    @ResponseBody
    public Object doWeeklyMeetingLastBusinessProcessingStatistics(SearchBusinessProcessingVo search) {
        return weekWorkPlanService.weeklyMeetingLastBusinessProcessingStatistics(search);
    }

    /**
     * 跳转到业务进度列表页面
     */
    @RequestMapping("/weekWorkPlan/weeklyMeetingBusinessProcessingList")
    public String businessStatisticslList() {
        return PREFIX + "weeklyMeetingBusinessProcessingList";
    }

    /**
     * 查询列业务进度统计数据的列表数据
     */
    @GetMapping("/weekWorkPlan/doWeeklyMeetingBusinessProcessingList")
    @ResponseBody
    public Object doWeeklyMeetingBusinessProcessingList(SearchBusinessProcessingVo search) {
        return weekWorkPlanService.weeklyMeetingBusinessProcessingList(search);
    }
}
