package com.jkcredit.invoice.hub.controller.web.contract;

import com.jkcredit.invoice.hub.controller.base.WebResource;
import com.jkcredit.invoice.hub.controller.web.contract.fb.ContractFB;
import com.jkcredit.invoice.hub.model.dto.contract.ContractDto;
import com.jkcredit.invoice.hub.service.contract.ContractService;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.spi.web.data.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/3/3 11:39
 * @version: V1.0
 **/
@RestController
@Slf4j
public class ContractResource extends WebResource {
    @Autowired
    ContractService contractService;

    @PostMapping("/uploadContract")
    @PreAuthorize("hasPermission('contract','upload')")
    public ResponseData uploadContract(ContractFB contractFB) {
        try {
            validate(contractFB);
            ContractDto contractDto = mapper.map(contractFB, ContractDto.class);
            contractService.uploadContract(contractDto);
            return ResponseData.success("上传协议成功");
        } catch (ServiceException e) {
            log.error(e.getMessage());
            return ResponseData.failed("上传协议失败，失败原因" + e.getMessage());
        }
    }
}
