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

import com.jkcredit.invoice.hub.controller.base.WebResource;
import com.jkcredit.invoice.hub.controller.web.contractQuery.fb.ContractQueryFB;
import com.jkcredit.invoice.hub.model.dto.contractQuery.ContractQueryDto;
import com.jkcredit.invoice.hub.model.vo.contractQuery.ContractQueryVo;
import com.jkcredit.invoice.hub.service.contractQuery.ContractQueryService;
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 16:31
 * @version: V1.0
 **/
@RestController
@Slf4j
public class ContractQueryResource extends WebResource {
    @Autowired
    ContractQueryService contractQueryService;

    @PostMapping("/contractQuery")
    @PreAuthorize("hasPermission('contract','query')")
    public ResponseData contractQuery(ContractQueryFB contractQueryFB) {
        try {
            validate(contractQueryFB);
            ContractQueryDto contractQueryDto = mapper.map(contractQueryFB, ContractQueryDto.class);
            ContractQueryVo contractQueryVo = contractQueryService.queryContract(contractQueryDto);
            return ResponseData.success(contractQueryVo);
        } catch (ServiceException e) {
            log.error(e.getMessage());
            return ResponseData.failed("协议查询失败，失败原因" + e.getMessage());
        }
    }
}
