package com.zcxk.task.controller;

import com.zcxk.client.UserCenterClient;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.LoginUser;
import com.zcxk.model.ResultStatus;
import com.zcxk.task.dto.PlanManageDto;
import com.zcxk.task.dto.TaskChangeTendDto;
import com.zcxk.task.dto.TaskConditionDto;
import com.zcxk.task.service.PlanManageService;
import com.zcxk.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/task/planStatistics")
@Api(tags = "任务统计")
public class PlanStatisticsController {

    @Autowired
    PlanManageService planManageService;
    @Autowired
    private UserCenterClient userCenterClient;

    /**
     * 查询任务完成情况
     * @return Response对象
     */
    @RequestMapping(value = "selectTaskFinishedByDate", method = RequestMethod.GET)
    @ApiOperation(value = "按时间查询任务完成情况")
    public AjaxMessage<List<PlanManageDto>> selectTaskFinished(
            @ApiParam(value = "开始时间", required = true) @RequestParam String startDate,
            @ApiParam(value = "结束时间", required = false) @RequestParam(required = false) String endDate,
            @ApiParam(value = "统计类型0按月、1按年、2自定义", required = true)@RequestParam Integer statsType) {
        PlanManageDto planManageDto=new PlanManageDto();
        planManageDto.setStatsType(statsType);

        LoginUser loginUser = UserUtil.getCurrentUser();
        planManageDto.setTenantId(loginUser.getTenantId());
        planManageDto.setProgramItems(loginUser.getProgramItemList());
        planManageDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        planManageDto.setPermissonType(loginUser.getPermissonType());
        planManageDto.setStartDate(startDate);
        planManageDto.setEndDate(endDate);
        List<PlanManageDto> result=planManageService.selectTaskFinishedByDate(planManageDto);
        if(result.size()>0) {
            Map<String,String> dictMap=userCenterClient.selectListByPCodes("SC_TASK_TYPE");
            for(PlanManageDto plan:result){
                if (plan.getTaskType() != null) {
                    plan.setTypeName(dictMap.get(plan.getTaskType().toString()));
                }
            }
        }
        return new AjaxMessage<>(ResultStatus.OK, result );
    }

    /**
     * 查询任务情况
     * @return Response对象
     */
    @RequestMapping(value = "selectTaskCondition", method = RequestMethod.GET)
    @ApiOperation(value = "查询任务情况")
    public AjaxMessage<TaskConditionDto> selectTaskCondition(
            @ApiParam(value = "开始时间", required = true) @RequestParam String startDate,
            @ApiParam(value = "结束时间", required = false) @RequestParam(required = false) String endDate,
            @ApiParam(value = "统计类型0按月、1按年、2自定义", required = true)@RequestParam Integer statsType) {
        PlanManageDto planManageDto=new PlanManageDto();
        planManageDto.setStatsType(statsType);
        planManageDto.setStartDate(startDate);
        planManageDto.setEndDate(endDate);

        LoginUser loginUser = UserUtil.getCurrentUser();
        planManageDto.setTenantId(loginUser.getTenantId());
        planManageDto.setProgramItems(loginUser.getProgramItemList());
        planManageDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        planManageDto.setPermissonType(loginUser.getPermissonType());

        TaskConditionDto result=planManageService.selectTaskCondition(planManageDto);
        return new AjaxMessage<>(ResultStatus.OK, result );
    }
    /**
     * 查询任务变化趋势和人均情况
     * * @return Response对象
     */
    @RequestMapping(value = "selectTaskChangeTrend", method = RequestMethod.GET)
    @ApiOperation(value = "查询任务变化趋势和人均情况")
    public AjaxMessage<List<TaskChangeTendDto>> selectTaskChangeTrend(
            @ApiParam(value = "开始时间", required = true) @RequestParam String startDate,
            @ApiParam(value = "结束时间", required = false) @RequestParam(required = false) String endDate,
            @ApiParam(value = "统计类型0按月、1按年、2自定义", required = true)@RequestParam Integer statsType) {
        PlanManageDto planManageDto=new PlanManageDto();
        planManageDto.setStatsType(statsType);
        planManageDto.setStartDate(startDate);
        planManageDto.setEndDate(endDate);

        LoginUser loginUser = UserUtil.getCurrentUser();
        planManageDto.setTenantId(loginUser.getTenantId());
        planManageDto.setProgramItems(loginUser.getProgramItemList());
        planManageDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        planManageDto.setPermissonType(loginUser.getPermissonType());
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Calendar c=Calendar.getInstance();
            String maintainerEndDate=null;
            Integer intervalCount=0;
            switch (statsType){
                case 0:
                    maintainerEndDate=startDate;
                    c.setTime(sdf.parse(startDate));
                    intervalCount= c.getActualMaximum(Calendar.DAY_OF_MONTH);
                    break;
                case 1:
                    maintainerEndDate=startDate+"-12";
                    intervalCount=12;
                    break;
                case 2:
                    maintainerEndDate=endDate;
                    c.setTime(sdf.parse(endDate));
                    intervalCount= Days.daysBetween(new DateTime(startDate), new DateTime(endDate)).getDays()+c.getActualMaximum(Calendar.DAY_OF_MONTH);
                    break;
                default:
            }
            planManageDto.setMaintainerCount(userCenterClient.findMaintainerCount(maintainerEndDate));
            planManageDto.setIntervalCount(intervalCount);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new AjaxMessage<>(ResultStatus.OK, planManageService.selectTaskChangeTrend(planManageDto));
    }

    /**
     * 查询任务处理时长
     * @return Response对象
     */
    @RequestMapping(value = "selectTaskDuration", method = RequestMethod.GET)
    @ApiOperation(value = "查询任务处理时长")
    public AjaxMessage<int[]> selectTaskDuration(
            @ApiParam(value = "开始时间", required = true) @RequestParam String startDate,
            @ApiParam(value = "结束时间", required = false) @RequestParam(required = false) String endDate,
            @ApiParam(value = "统计类型0按月、1按年、2自定义", required = true)@RequestParam Integer statsType) {
        PlanManageDto planManageDto=new PlanManageDto();
        planManageDto.setStatsType(statsType);
        planManageDto.setStartDate(startDate);
        planManageDto.setEndDate(endDate);

        LoginUser loginUser = UserUtil.getCurrentUser();
        planManageDto.setTenantId(loginUser.getTenantId());
        planManageDto.setProgramItems(loginUser.getProgramItemList());
        planManageDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        planManageDto.setPermissonType(loginUser.getPermissonType());

        int[] result=planManageService.selectTaskDuration(planManageDto);
        return new AjaxMessage<>(ResultStatus.OK, result );
    }
}