package org.springblade.modules.app.web;


import com.wn.common.datamodel.ResultDTO;
import org.springblade.modules.app.common.dto.ContractDTO;
import org.springblade.modules.app.common.dto.UserDTO;
import org.springblade.modules.app.common.vo.ApprovalVO;
import org.springblade.modules.app.common.vo.ContractVO;
import org.springblade.modules.app.manager.ContractManager;
import org.springblade.modules.app.utils.UserUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.wn.common.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 用户合同信息表 前端控制器
 * </p>
 *
 * @author fuxinsheng
 * @since 2021-11-05
 */
@RestController
@RequestMapping("app/contract")
public class AppContractController extends BaseController {

    @Autowired
    private ContractManager contractManager;

    @Autowired
    private UserUtils userUtils;

    //@ApiOperation(value = "查询合同列表")
    @GetMapping("/list")
    public ResponseEntity<ResultDTO<List<ContractVO>>> getContractList(String token) {
        Long id = userUtils.getUser(token).getId();
        Integer type = userUtils.getUser(token).getType();
        return success(contractManager.getContractList(id, type));
    }

    //@ApiOperation(value = "创建合同")
    @PostMapping
    public ResponseEntity<ResultDTO<ContractVO>> addContract(@RequestBody ContractDTO contractDTO) {
        UserDTO userDTO = userUtils.getUser(contractDTO.getToken());
        return success(contractManager.addContract(contractDTO, userDTO));
    }

    //@ApiOperation(value = "查询合同详情")
    @GetMapping("/{token}/{contractId}")
    public ResponseEntity<ResultDTO<ContractDTO>> getContractDetail(@PathVariable Long contractId,@PathVariable String token) {
        UserDTO userDTO = userUtils.getUser(token);
        return success(contractManager.getContractDetail(contractId, userDTO));
    }

    //@ApiOperation(value = "审批合同")
    @PutMapping("approval")
    public ResponseEntity<ResultDTO<Boolean>> approvalContract(@RequestBody ApprovalVO approvalVO) {
        UserDTO userDTO = userUtils.getUser(approvalVO.getToken());
        return success(contractManager.approvalContract(approvalVO, userDTO));
    }

    //@ApiOperation(value = "重新提交合同")
    @PutMapping("/resubmit")
    public ResponseEntity<ResultDTO<Boolean>> resubmit(@RequestBody ContractDTO contractDTO) {
        UserDTO userDTO = userUtils.getUser(contractDTO.getToken());
        return success(contractManager.resubmit(contractDTO, userDTO));
    }
}

