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.*;
import cn.wisenergy.model.vo.OrderChangeLogVo;
import cn.wisenergy.model.vo.OrderChangeManagerVo;
import cn.wisenergy.model.vo.OrderChangeVo;
import cn.wisenergy.service.WorkOrderChangeService;
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;

    @Autowired
    private WorkOrderChangeService workOrderChangeService;


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

    @ApiOperation(value = "查询工时缺失", notes = "查询工时缺失", httpMethod = "GET")
    @ApiImplicitParam(name = "userId", value = "管理员Id", dataType = "int",required = true)
    @GetMapping(value = "/getDeptNotOrderInfo")
    public Result getDeptNotOrderInfo(Integer userId) {
        log.info("WorkTimeOrderController[]getDeptNotOrderInfo[]input.param.userId:{}" + userId);
        NotWorkSubmitOrderDto deptNotOrderInfo = workTimeOrderService.getDeptNotOrderInfo(userId);
        return getResult(deptNotOrderInfo);

    }

    @ApiOperation(value = "工单变更管理分页",notes = "工单变更分页",httpMethod = "POST")
    @ApiImplicitParam(name = "vo", value = "工单变更分页条件Vo", dataType = "OrderChangeManagerVo")
    @PostMapping("/orderChangePage")
    public Result orderChangePage(@RequestBody OrderChangeManagerVo vo) {
        log.info("WorkTimeOrderController[]orderChangePage[]input.param.OrderChangeManagerVo:{}" + vo);
        PageInfo<OrderChangePageDto> orderChangePage = workTimeOrderService.getOrderChangePage(vo);
        return getResult(orderChangePage);
    }

    @ApiOperation(value = "新增变更工单", notes = "新增变更工单", httpMethod = "POST")
    @ApiImplicitParam(name = "vo", value = "新增变更工单vo",dataType = "OrderChangeVo")
    @PostMapping("/createOrderChange")
    public Result createOrderChange(@RequestBody OrderChangeVo vo) {
        log.info("WorkTimeOrderController[]createOrderChange[]input.param.OrderChangeVo:{}" + vo);
        Boolean orderChange = workTimeOrderService.createOrderChange(vo);
        return getResult(orderChange);
    }

    @ApiOperation(value = "修改变更工单",notes = "修改变更工单",httpMethod = "PUT")
    @ApiImplicitParam(name = "vo", value = "修改变更工单vo",dataType = "OrderChangeVo")
    @PutMapping("/updateOrderChange")
    public Result updateOrderChange(@RequestBody OrderChangeVo vo) {
        log.info("WorkTimeOrderController[]updateOrderChange[]input.param.OrderChangeVo:{}" + vo);
        Boolean aBoolean = workTimeOrderService.updateOrderChange(vo);
        return getResult(aBoolean);
    }

    @ApiOperation(value = "删除工单",notes = "删除工单",httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workId",value = "工单id",dataType = "int",required = true),
            @ApiImplicitParam(name = "operator",value = "操作者",dataType = "int",required = true),
            @ApiImplicitParam(name = "changeReason",value = "删除理由",dataType = "String",required = true)
    })
    @DeleteMapping("/deleteOrderChange")
    public Result deleteOrderChange(Integer workId,Integer operator,String changeReason) {
        log.info("WorkTimeOrderController[]deleteOrderChange[]input.param.workId:{},operator:{},changeReason:{}" + workId,operator,changeReason);
        Boolean aBoolean = workTimeOrderService.deleteOrderChange(workId, operator,changeReason);
        return getResult(aBoolean);
    }

    @ApiOperation(value = "工单变更记录分页",notes = "工单变更记录分页",httpMethod = "POST")
    @ApiImplicitParam(name = "vo", value = "工单变更记录分页条件vo",dataType = "OrderChangeLogVo")
    @PostMapping("/orderChangeLogPage")
    public Result orderChangeLogPage(@RequestBody OrderChangeLogVo vo) {
        log.info("WorkTimeOrderController[]OrderChangeLogPage[]input.param.OrderChangeLogVo:{}" + vo);
        PageInfo orderChangeLogPage = workOrderChangeService.getOrderChangeLogPage(vo);
        return getResult(orderChangeLogPage);
    }

    @ApiOperation(value = "小程序长假/长调休填报", notes = "小程序长假/长调休填报")
    @PostMapping("/takeALongLeave")
    public Result takeALongLeave(TakeALongLeaveParam takeALongLeaveParam) {
        log.info("WorkTimeOrderController[]takeALongLeave[]input.param" + takeALongLeaveParam);
        Boolean isSuccess = workTimeOrderService.takeALongLeave(takeALongLeaveParam);
        return getResult(isSuccess);
    }

    @ApiOperation(value = "获取用户长假/长调休信息", notes = "获取用户长假或长调休信息")
    @GetMapping("/getUserLongLeaveInfo")
    public Result getUserLongLeaveInfo(Integer userId) {
        log.info("WorkTimeOrderController[]getLongLeaveInfo[]input.param{}userId" + userId);
        List<LeaveResult> leaveResultList = workTimeOrderService.getUserLongLeaveInfo(userId);
        return getResult(leaveResultList);
    }

    @ApiOperation(value = "获取时间段内节假日的天数", notes = "获取时间段内节假日的天数")
    @GetMapping("getHolidayCountWithTimePeriod")
    public Result getHolidayCountWithTimePeriod(String startDate, String endDate){
        log.info("WorkTimeOrderController[]getHolidayCountWithTimePeriod[]input.param{}" + startDate + "," + endDate);
        Integer daysCount = workTimeOrderService.getHolidayCountWithTimePeriod(startDate, endDate);
        return getResult(daysCount);
    }

}
