package cn.superdesk.uniorder.openapi.api._thirdpart.qchjc.accounting.supplier;

import cn.superdesk.uniorder.openapi.annotation.QchjcRequestLog;
import cn.superdesk.uniorder.openapi.api._thirdpart.qchjc._dto.accounting.QchjcSupplierStatementDto;
import cn.superdesk.uniorder.openapi.api._thirdpart.qchjc._dto.accounting.QchjcSupplierStatementStatusDto;
import cn.superdesk.uniorder.openapi.api._thirdpart.qchjc._dto.order.QchjcJdOrderParam;
import cn.superdesk.uniorder.openapi.api.base.O;
import cn.superdesk.uniorder.openapi.config.exception.ParameterNotValidException;
import cn.superdesk.uniorder.openapi.service.qchjc.accounting.SupplierStatementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2022/3/4 下午5:14
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/3/4 下午5:14
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "thirdpart-qchjc-accounting-supplier", tags = {"thirdpart-qchjc-accounting-supplier"})
@Slf4j
@RestController
@RequestMapping("/v0/thirdpart/qchjc/accounting/supplier")
public class SupplierStatementRestApi {

    @Autowired
    private SupplierStatementService supplierStatementService;

    @ApiOperation(value = "供应商对账单接收")
    @RequestMapping(value = "receive", method = RequestMethod.POST)
    @ResponseBody
    @QchjcRequestLog(value = "供应商对账单接收")
    public O<QchjcSupplierStatementDto> receive(@RequestBody @Validated QchjcSupplierStatementDto dto, BindingResult result) {
        if (result.hasErrors()) {
            throw new ParameterNotValidException(result);
        }
        //log.info("receive qchjc supplier statement, param : {}", JSON.toJSONString(dto));
        supplierStatementService.receive(dto);

        return new O<QchjcSupplierStatementDto>()
                .setResultCode(200)
                .setResultMessage("同步成功")
                .setSuccess(true);
    }


    @ApiOperation(value = "供应商对账单状态同步")
    @RequestMapping(value = "/syncStatus", method = RequestMethod.POST)
    @ResponseBody
    @QchjcRequestLog("供应商对账单状态同步")
    public O<QchjcSupplierStatementStatusDto> syncStatus(@RequestBody @Validated QchjcSupplierStatementStatusDto dto, BindingResult result) {

        if (result.hasErrors()) {
            throw new ParameterNotValidException(result);
        }
        supplierStatementService.syncStatus(dto);
        return new O<QchjcSupplierStatementStatusDto>()
                .setResultCode(200)
                .setResultMessage("同步成功")
                .setSuccess(true);
    }

    @ApiOperation(value = "京东对账订单接收")
    @RequestMapping(value = "/receive/jd", method = RequestMethod.POST)
    @ResponseBody
    @QchjcRequestLog(value = "京东对账订单接收")
    public O<QchjcJdOrderParam> receiveJdOrder(@RequestBody @Validated QchjcJdOrderParam param, BindingResult result) {
        if (result.hasErrors()) {
            throw new ParameterNotValidException(result);
        }
        supplierStatementService.receiveJdOrder(param);

        return new O<QchjcJdOrderParam>()
                .setResultCode(200)
                .setResultMessage("同步成功")
                .setSuccess(true);
    }


    @ResponseBody
    @RequestMapping(value = "/fix", method = RequestMethod.POST)
    public String fix(String key,Integer limit, String supplierAccountingOrderSn) {
        if ("fix2022".equals(key)) {
            supplierStatementService.fix(limit,supplierAccountingOrderSn);
            return "success";
        }
        return "fail";
    }
}
