package cn.superdesk.uniorder.controller.invoice.customer;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.enums.invoice.customer.InvoiceApplyCustomerInvoiceProgressingStatusEnum;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.rest.response.ResponseResultOuterUtils;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrderGoods;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dto.param.accounting.score.customer.AccountingOrderScoreCustomerNotYetDimCreditScene4QueryCriteria;
import cn.superdesk.uniorder.dto.param.order.base.UnifiedOrderGoodsOpSetOperationTaxCodeForm;
import cn.superdesk.uniorder.dto.vo.invoice.customer.InvoiceApplyDetailDimCreditItemVo;
import cn.superdesk.uniorder.dubbo.consumer.accounting.customer.AccountingScoreCustomerDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.InvoiceApplyCustomerDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.InvoiceApplyXGoodsDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderGoodsDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysEnterpriseDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysEnterpriseExtInvoiceDubboConsumerService;
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.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2022/7/14 11:52 上午
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/7/14 11:52 上午
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "积分结算-发票单-v2.5.1", tags = {"积分结算-发票单-v2.5.1"})
@Slf4j
@RestController
@RequestMapping("/invoice/apply/customer/score/v2_5_1")
public class InvoiceApplyCustomerScoreExV_2_5_1Controller extends BaseController {

    @Autowired
    private InvoiceApplyCustomerDubboConsumerService invoiceApplyCustomerDubboConsumerService;
    @Autowired
    private InvoiceApplyXGoodsDubboConsumerService   invoiceApplyXGoodsDubboConsumerService;

    @Autowired
    private AccountingScoreCustomerDubboConsumerService accountingScoreCustomerDubboConsumerService;

    @Autowired
    private UnifiedOrderDubboConsumerService      unifiedOrderDubboConsumerService;
    @Autowired
    private UnifiedOrderGoodsDubboConsumerService unifiedOrderGoodsDubboConsumerService;

    @Autowired
    private SysEnterpriseDubboConsumerService           sysEnterpriseDubboConsumerService;
    @Autowired
    private SysEnterpriseExtInvoiceDubboConsumerService sysEnterpriseExtInvoiceDubboConsumerService;

    @ApiOperation(value = "op修改平台税务编码-op列表(分页 搜索)-(积分结算-发票申请管理)")
    @RequestMapping(value = "/opListOperationTaxCode", method = RequestMethod.POST)
    public R<List<InvoiceApplyDetailDimCreditItemVo>> opListOperationTaxCode(
            @RequestBody AccountingOrderScoreCustomerNotYetDimCreditScene4QueryCriteria queryCriteria) {

        log.info("积分结算-发票申请管理-op列表-op修改平台税务编码-op列表 params : \n{}", JSONUtil.toJsonStr(queryCriteria));

        R<List<InvoiceApplyDetailDimCreditItemVo>> response = new R<>();

        response = unifiedOrderDubboConsumerService
                .getScoreOrderReconciliationNotYetScene4(
                        queryCriteria,
                        getToken()
                );

        return response;
    }

    @ApiOperation(value = "op修改平台税务编码-op修改-(积分结算-发票申请管理)")
    @RequestMapping(value = "/opSetOperationTaxCode", method = RequestMethod.POST)
    @ResponseBody
    public R<String> opSetOperationTaxCode(
            @RequestBody UnifiedOrderGoodsOpSetOperationTaxCodeForm form) {

        log.info("积分结算-发票申请管理-op列表-op修改平台税务编码-op修改 params : \n{}", JSONUtil.toJsonStr(form));

        InvoiceApply entityExistDb = invoiceApplyCustomerDubboConsumerService.findById(form.getInvoiceApplyId());

        if (ObjectUtil.isNull(entityExistDb)) {
            return ResponseResultOuterUtils.createErrorResult("500", "该开票申请不存在");
        }

        if (InvoiceApplyCustomerInvoiceProgressingStatusEnum.待开票.getKey().compareTo(entityExistDb.getInvoiceStatus()) != 0) {
            return ResponseResultOuterUtils.createErrorResult("500", "只允许修改待开票的开票申请数据，请返回检查");
        }

        UnifiedOrderGoods unifiedOrderGoodsExistDb = unifiedOrderGoodsDubboConsumerService.findById(form.getUnifiedOrderGoodsId());

        if (ObjectUtil.isNull(unifiedOrderGoodsExistDb)) {
            return ResponseResultOuterUtils.createErrorResult("500", "该订单流水不存在，请返回检查");
        }

        UnifiedOrderGoods forUpdate = new UnifiedOrderGoods();

        forUpdate.setId(form.getUnifiedOrderGoodsId());

        forUpdate.setOperationTaxCode(form.getOperationTaxCode());

        int ret = unifiedOrderGoodsDubboConsumerService.updateByPrimaryKeySelective(forUpdate);

        if (ret == 0) {
            return ResponseResultOuterUtils.createErrorResult("500", "修改失败");
        } else {
            return ResponseResultOuterUtils.createSuccesResult("修改成功");
        }
    }
}
