package cn.wisenergy.web.admin.controller.app;

import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.common.utils.exception.CustomException;
import cn.wisenergy.common.utils.exception.RESP_CODE_ENUM;
import cn.wisenergy.common.utils.exception.Result;
import cn.wisenergy.model.app.WorkTimeOrder;
import cn.wisenergy.model.dto.AppletsPendApprovalDto;
import cn.wisenergy.model.dto.CreateWorkOrderDto;
import cn.wisenergy.model.dto.ProjectsDto;
import cn.wisenergy.model.dto.WorkTimeOrderDto;
import cn.wisenergy.service.WorkTimeOrderService;
import cn.wisenergy.web.admin.controller.common.BaseController;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping(value = "/work/order")
@Api(tags = "工时模块接口")
public class WorkTimeOrderController extends BaseController {
    @Autowired
    private WorkTimeOrderService workTimeOrderService;


    @ApiOperation(value = "获取某日工单详情", notes = "获取某日工单详情", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", required = true),
            @ApiImplicitParam(name = "workDay", value = "工单日期(yyyy-MM-dd)", dataType = "String", required = true)
    })
    @GetMapping(value = "/query")
    public Result<List<WorkTimeOrderDto>> query(Integer userId, String workDay) {
        log.info("WorkOrderController[]query[]input.param.id" + userId, workDay);
        if (null == userId || StringUtils.isEmpty(workDay)) {
            throw new CustomException(RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        Date date = DateUtil.convertStrToDate(workDay, "yyyy-MM-dd");
        List<WorkTimeOrder> dtos = workTimeOrderService.getByDayToDto(userId, date);
        return getResult(workTimeOrderService.wtoToWtod(dtos));
    }


    @ApiOperation(value = "查询待审批(小程序)", notes = "查询待审批(小程序)", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "int", required = true),
            @ApiImplicitParam(name = "projectId", value = "项目id,不给就默认项目", dataType = "int"), @ApiImplicitParam(name = "type", value = "给部门经理：工单类型type(5,6,7)", dataType = "int")
    })
    @GetMapping(value = "/getExamineApplets")
    public Result<List<AppletsPendApprovalDto>> getExamineApplets(Integer userId, Integer projectId, Integer type) {
        log.info("WorkTimeOrderController[]getExamineApplets[]input.param.userId:{},projectId:{},type:{}}" + userId, projectId, type);
        if (null == userId) {
            throw new CustomException(RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<AppletsPendApprovalDto> examineApplets = workTimeOrderService.getExamineApplets(userId, projectId, type);
        return getResult(examineApplets);

    }

    @ApiOperation(value = "查询待审批(PC端)", notes = "查询待审批(PC端)", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "int", required = true),
            @ApiImplicitParam(name = "projectId", value = "项目id", dataType = "int"),
            @ApiImplicitParam(name = "type", value = "部门经理：工单类型type(5,6,7)", dataType = "int"),
            @ApiImplicitParam(name = "page", value = "PC端当前页", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "PC端一页多少条记录", dataType = "int"),
            @ApiImplicitParam(name = "status", value = "PC端审批状态", dataType = "string")
    })
    @GetMapping(value = "/getExamine")
    public Result<PageInfo<WorkTimeOrderDto>> getExamine(Integer userId, Integer projectId, Integer type, Integer page, Integer pageSize, String status) {
        log.info("WorkTimeOrderController[]getExamine[]input.param.userId:{},projectId:{},type:{},page:{},pageSize:{},status:{}" + userId, projectId, type, page, pageSize, status);
        if (null == userId) {
            throw new CustomException(RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<Integer> lists = null;
        if (!StringUtils.isEmpty(status)) {
            String[] split = status.split(",");
            Integer[] newIds = (Integer[]) ConvertUtils.convert(split, Integer.class);
            lists = new ArrayList<>(Arrays.asList(newIds));
        }
        PageInfo<WorkTimeOrderDto> examine = workTimeOrderService.getExamine(userId, projectId, type, page, pageSize, lists);
        return getResult(examine);
    }


    @ApiOperation(value = "查询当前用户可填报和可审批的项目", notes = "查询当前用户可填报和可审批的项目", httpMethod = "GET")
    @ApiImplicitParam(name = "userId", value = "用户id", dataType = "int", required = true)
    @GetMapping(value = "/getProject")
    public Result<ProjectsDto> getProjectByUserId(Integer userId) {
        log.info("WorkTimeOrderController[]getProjectByUserId[]input.param.userId:{}" + userId);
        if (null == userId) {
            throw new CustomException(RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        ProjectsDto projectDto = workTimeOrderService.getProjectDto(userId);
        return getResult(projectDto);
    }

    @ApiOperation(value = "单个或批量审批", notes = "单个或批量审批", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "工单id", dataType = "String", required = true),
            @ApiImplicitParam(name = "reviewerId", value = "审核人id", dataType = "int", required = true)})
    @GetMapping(value = "/examine")
    public Result<Boolean> examineByIds(Integer reviewerId, String ids) {
        log.info("WorkTimeOrderController[]examineByIds[]input.param.reviewerId:{},ids:{}" + reviewerId, ids);
        if (null == reviewerId || StringUtils.isEmpty(ids)) {
            throw new CustomException(RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        String[] split = ids.split(",");
        Integer[] newIds = (Integer[]) ConvertUtils.convert(split, Integer.class);
        List<Integer> lists = new ArrayList<>(Arrays.asList(newIds));
        Boolean aBoolean = workTimeOrderService.examineByIds(reviewerId, lists);
        return getResult(aBoolean);

    }

    @ApiOperation(value = "工单驳回", notes = "工单驳回", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewerId", value = "审核人id", dataType = "int", required = true),
            @ApiImplicitParam(name = "id", value = "工单id", dataType = "int", required = true),
            @ApiImplicitParam(name = "reason", value = "驳回理由", dataType = "String", required = true)
    })
    @PutMapping(value = "/reject")
    public Result<Boolean> rejectById(Integer reviewerId, Integer id, String reason) {
        log.info("WorkTimeOrderController[]rejectById[]input.param.reviewerId:{},id:{},reason:{}" + reviewerId, id, reason);
        if (null == reviewerId || null == id || StringUtils.isEmpty(reason)) {
            throw new CustomException(RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        Boolean aBoolean = workTimeOrderService.rejectById(reviewerId, id, reason);
        return getResult(aBoolean);
    }

    @ApiOperation(value = "查询驳回未提交的", notes = "查询驳回未提交的", httpMethod = "GET")
    @ApiImplicitParam(name = "userId", value = "用户id", dataType = "int", required = true)
    @GetMapping(value = "/noSubmit")
    public Result<List<WorkTimeOrderDto>> rejectNotSubmit(Integer userId) {
        log.info("WorkTimeOrderController[]rejectNotSubmit[]input.param.userId:{}" + userId);
        if (null == userId) {
            throw new CustomException(RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<WorkTimeOrderDto> workTimeOrderDtos = workTimeOrderService.rejectNotSubmit(userId);
        return getResult(workTimeOrderDtos);
    }

    @ApiOperation(value = "工时填报", notes = "工时填报", httpMethod = "POST")
    @ApiImplicitParam(name = "dto", value = "创建工单dto", dataType = "CreateWorkOrderDto")
    @PostMapping(value = "/addOrder")
    public Result<List<WorkTimeOrderDto>> addOrder(@RequestBody CreateWorkOrderDto dto) {
        log.info("WorkTimeOrderController[]addTest[]input.param" + dto);
        List<WorkTimeOrderDto> workTimeOrderDtos = workTimeOrderService.addOrder(dto);
        return getResult(workTimeOrderDtos);
    }

}
