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.app.WorkUser;
import cn.wisenergy.model.dto.*;
import cn.wisenergy.service.*;
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.CollectionUtils;
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;

    @Autowired
    private WorkProjectService workProjectService;

    @Autowired
    private WorkUserProjectService workUserProjectService;


    @Autowired
    private WorkUserDeptService workUserDeptService;

    @Autowired
    private WorkUserService workUserService;


    @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<WorkTimeOrderDto> dtos = workTimeOrderService.getByDayToDto(userId, date);
        return getResult(dtos);
    }

    @ApiOperation(value = "工时填报", notes = "工时填报", httpMethod = "POST")
    @ApiImplicitParam(name = "dto", value = "创建工单dto", dataType = "CreateWorkOrderDto")
    @PostMapping(value = "/add")
    public Result<List<WorkTimeOrderDto>> add(@RequestBody CreateWorkOrderDto dto) {
        log.info("WorkTimeOrderController[]add[]input.param" + dto);
        if (null == dto || CollectionUtils.isEmpty(dto.getWorkTimeOrders())) {
            throw new CustomException(RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<WorkTimeOrderDto> workOrderList = workTimeOrderService.add(dto);
        return getResult(workOrderList);
    }

    @ApiOperation(value = "工时驳回重新填报", notes = "工时驳回重新填报", httpMethod = "PUT")
    @ApiImplicitParam(name = "dto", value = "创建工单dto", dataType = "CreateWorkOrderDto")
    @PutMapping(value = "/update")
    public Result<List<WorkTimeOrderDto>> update(@RequestBody CreateWorkOrderDto dto) {
        log.info("WorkTimeOrderController[]update[]input.param" + dto);
        if (null == dto || CollectionUtils.isEmpty(dto.getWorkTimeOrders())) {
            throw new CustomException(RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<WorkTimeOrderDto> updateData = workTimeOrderService.update(dto);
        return getResult(updateData);
    }

    @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 = "size", value = "PC端一页多少条记录", dataType = "int"),
            @ApiImplicitParam(name = "status", value = "PC端审批状态", dataType = "int"),
    })
    @GetMapping(value = "/getExamine")
    public Result<PageInfo<WorkTimeOrder>> getExamine(Integer userId, Integer projectId, Integer type, Integer page, Integer size, Integer status) {
        log.info("WorkTimeOrderController[]getExamine[]input.param.userId:{},projectId:{},type:{},page:{},size:{},status:{}" + userId, projectId, type, page, size, status);
        if (null == userId) {
            throw new CustomException(RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        PageInfo<WorkTimeOrder> examine = workTimeOrderService.getExamine(userId, projectId, type, page, size, status);
        return getResult(examine);
    }


    @ApiOperation(value = "查询用户的所有项目和部门经理", notes = "查询用户的所有项目和部门经理", httpMethod = "GET")
    @ApiImplicitParam(name = "userId", value = "用户id", dataType = "int", required = true)
    @GetMapping(value = "/getProject")
    public Result<List<ProjectDto>> getProjectByUserId(Integer userId) {
        log.info("WorkTimeOrderController[]getProjectByUserId[]input.param.userId:{}" + userId);
        if (null == userId) {
            throw new CustomException(RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<Integer> ids = workUserProjectService.getProjectIdByUserId(userId);
        List<ProjectDto> list = workProjectService.getNameById(ids);
        Integer deptId = workUserDeptService.getDeptIdByUserId(userId);
        List<Integer> idsByDeptId = workUserDeptService.getUserIdsByDeptId(deptId);
        WorkUser workUser = workUserService.getDeptManagerByIds(idsByDeptId);
        list.forEach(index -> index.setDeptManagerName(workUser.getName()));
        return getResult(list);
    }

    @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("\\,");
        System.out.println("我是传进来的数据：" + ids);
        Integer[] newIds = (Integer[]) ConvertUtils.convert(split, Integer.class);
        List<Integer> lists = new ArrayList<>(Arrays.asList(newIds));
        System.out.println(lists);
        Boolean aBoolean = workTimeOrderService.examineByIds(reviewerId, lists);
        return getResult(aBoolean);
        // return null;
    }

    @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);
    }

}
