package com.codvision.workflow.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.workflow.entity.WorkFlowDelayRecord;
import com.codvision.workflow.entity.dto.WorkFlowDelayDTO;
import com.codvision.workflow.service.WorkFlowDelayRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author hxl
 */
@Slf4j
@Api(tags = "工作流延期申请")
@RestController
@RequiredArgsConstructor
@RequestMapping("/delayRecord")
public class WorkFlowDelayRecordController {

    private final WorkFlowDelayRecordService service;

    @PostMapping("/getPageResult")
    @ApiOperation("分页列表")
    public ResponseEntity<IPage<WorkFlowDelayRecord>> getPageResult(@RequestBody QueryPage<Map<String, Object>> queryPage) {
        return new ResponseEntity<IPage<WorkFlowDelayRecord>>().data(service.getPageResult(queryPage));
    }

    @ApiOperation("我的延期申请记录")
    @PostMapping("/myDelayRecords")
    public ResponseEntity<IPage<WorkFlowDelayRecord>> myDelayRecords(@RequestBody QueryPage<Map<String, Object>> queryPage) {
        return new ResponseEntity<IPage<WorkFlowDelayRecord>>().data(service.myDelayRecords(queryPage));
    }
    @ApiOperation("添加延期申请")
    @PostMapping("/add")
    public ResponseEntity<String> add(@Validated @RequestBody WorkFlowDelayDTO workFlowDelayDTO) {
        service.add(workFlowDelayDTO);
        return new ResponseEntity<String>().ok();
    }

    @ApiOperation("延期申请审核")
    @PostMapping("/audit")
    public ResponseEntity<String> audit(@RequestParam(value = "id") Integer id,
                                        @RequestParam(value = "status") Integer status,
                                        @RequestParam(required = false) String comment) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (id == null || status == null) {
            throw new BusinessException(30002);
        }

        service.audit(id, status, comment);
        return responseEntity.ok();
    }
}
