package jnpf.workflow.controller;

import cn.hutool.core.date.DateUtil;
import jnpf.base.ActionResult;
import jnpf.constant.MsgCode;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.entity.workflow.AttendanceBusinessTripApprove;
import jnpf.entity.workflow.AttendanceGoOutApprove;
import jnpf.model.workflow.dto.AttendanceBusinessTripApproveDto;
import jnpf.model.workflow.dto.AttendanceGoOutApproveDto;
import jnpf.model.workflow.vo.AttendanceBusinessTripApproveVo;
import jnpf.model.workflow.vo.AttendanceGoOutApproveVo;
import jnpf.util.JsonUtil;
import jnpf.workflow.service.BusinessTripApproveService;
import jnpf.workflow.service.GoOutApproveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 审批触发接口
 *
 * @Author huanglinpan
 * @Date 2024/5/14 18:15
 * @Version 1.0 （版本号）
 */
@Slf4j
@RestController
@RequestMapping(value = "/apply")
public class ApplyController {


    @Resource
    private BusinessTripApproveService businessTripApproveService;

    @Resource
    private GoOutApproveService goOutApproveService;

    /**
     * 新建出差申请
     *
     * @param attendanceBusinessTripApproveDto 表单对象
     * @return java.lang.Object
     */
    @PostMapping("/businessTrip/{id}")
    public Object createGoOut(@RequestBody @Valid AttendanceBusinessTripApproveDto attendanceBusinessTripApproveDto, @PathVariable("id") String id) {
        log.error("开始出差了参数................: {}", attendanceBusinessTripApproveDto);
        AttendanceBusinessTripApprove entity = JsonUtil.getJsonToBean(attendanceBusinessTripApproveDto, AttendanceBusinessTripApprove.class);
        if (!entity.getEndTime().after(entity.getStartTime())) {
            entity.setEndTime(DateUtil.endOfDay(entity.getEndTime()));
        }
        if (FlowStatusEnum.save.getMessage().equals(attendanceBusinessTripApproveDto.getStatus())) {
            businessTripApproveService.save(id, entity, attendanceBusinessTripApproveDto);
            return ActionResult.success(MsgCode.SU002.get());
        }
        businessTripApproveService.submit(id, entity, attendanceBusinessTripApproveDto);
        return ActionResult.success(MsgCode.SU006.get());
    }


    /**
     * 查询出差审批记录
     *
     * @param id 审批id
     * @return jnpf.base.ActionResult<jnpf.model.workflow.vo.ApplyAttendanceChangeVo>
     */
    @GetMapping(value = "/businessTrip/{id}")
    public ActionResult<AttendanceBusinessTripApproveVo> getGoOut(@PathVariable(value = "id") String id) {

        AttendanceBusinessTripApproveVo vo = businessTripApproveService.getOne(id);
        return ActionResult.success(vo);
    }


    /**
     * 修改出差申请
     *
     * @param attendanceBusinessTripApproveDto 表单对象
     * @param id                               主键
     * @return java.lang.Object
     */
    @PutMapping("/businessTrip/{id}")
    public Object updateGoOut(@RequestBody @Valid AttendanceBusinessTripApproveDto attendanceBusinessTripApproveDto, @PathVariable("id") String id) {

        AttendanceBusinessTripApprove entity = JsonUtil.getJsonToBean(attendanceBusinessTripApproveDto, AttendanceBusinessTripApprove.class);
        entity.setId(id);
        if (FlowStatusEnum.save.getMessage().equals(attendanceBusinessTripApproveDto.getStatus())) {
            businessTripApproveService.save(id, entity, attendanceBusinessTripApproveDto);
            return ActionResult.success(MsgCode.SU002.get());
        }
        businessTripApproveService.submit(id, entity, attendanceBusinessTripApproveDto);
        return ActionResult.success(MsgCode.SU006.get());
    }


    /**
     * 新建外出申请
     *
     * @param attendanceGoOutApproveDto 表单对象
     * @return java.lang.Object
     */
    @PostMapping("/out/{id}")
    public Object createBusinessTrip(@RequestBody @Valid AttendanceGoOutApproveDto attendanceGoOutApproveDto, @PathVariable("id") String id) {
        log.error("开始外出申请了参数................: {}", attendanceGoOutApproveDto);
        AttendanceGoOutApprove entity = JsonUtil.getJsonToBean(attendanceGoOutApproveDto, AttendanceGoOutApprove.class);
        if (!entity.getEndTime().after(entity.getStartTime())) {
            entity.setEndTime(DateUtil.endOfDay(entity.getEndTime()));
        }
        if (FlowStatusEnum.save.getMessage().equals(attendanceGoOutApproveDto.getStatus())) {
            goOutApproveService.save(id, entity, attendanceGoOutApproveDto);
            return ActionResult.success(MsgCode.SU002.get());
        }
        goOutApproveService.submit(id, entity, attendanceGoOutApproveDto);
        return ActionResult.success(MsgCode.SU006.get());
    }


    /**
     * 查询外出审批记录
     *
     * @param id 审批id
     * @return jnpf.base.ActionResult<jnpf.model.workflow.vo.ApplyAttendanceChangeVo>
     */
    @GetMapping(value = "/out/{id}")
    public ActionResult<AttendanceGoOutApproveVo> getBusinessTrip(@PathVariable(value = "id") String id) {

        AttendanceGoOutApproveVo vo = goOutApproveService.getOne(id);
        return ActionResult.success(vo);
    }


    /**
     * 修改外出申请
     *
     * @param attendanceGoOutApproveDto 表单对象
     * @param id                        主键
     * @return java.lang.Object
     */
    @PutMapping("/out/{id}")
    public Object updateBusinessTrip(@RequestBody @Valid AttendanceGoOutApproveDto attendanceGoOutApproveDto, @PathVariable("id") String id) {

        AttendanceGoOutApprove entity = JsonUtil.getJsonToBean(attendanceGoOutApproveDto, AttendanceGoOutApprove.class);
        entity.setId(id);
        if (FlowStatusEnum.save.getMessage().equals(attendanceGoOutApproveDto.getStatus())) {
            goOutApproveService.save(id, entity, attendanceGoOutApproveDto);
            return ActionResult.success(MsgCode.SU002.get());
        }
        goOutApproveService.submit(id, entity, attendanceGoOutApproveDto);
        return ActionResult.success(MsgCode.SU006.get());
    }
}
