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

import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.enums.common.CommonAccountingStateEnum;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dto.param.accounting.score.customer.AccountingOrderScoreCustomerNotYetDimCreditQueryCriteria;
import cn.superdesk.uniorder.dto.param.accounting.score.customer.AccountingOrderScoreCustomerNotYetDimCreditScene1QueryCriteria;
import cn.superdesk.uniorder.dto.vo.invoice.customer.InvoiceApplyDetailDimCreditItemVo;
import cn.superdesk.uniorder.dto.vo.score.ScoreOrderReconciliationNotYetHeadVo;
import cn.superdesk.uniorder.dubbo.consumer._common.ProviderEnvConstantDubboCustomerService;
import cn.superdesk.uniorder.dubbo.consumer.accounting.customer.AccountingScoreCustomerDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.accounting.customer.AccountingUnifiedCustomerDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderCreditDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.InvoiceApplyCustomerDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.sys.SysConfigDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.sys.SysInvDubboConsumerService;
import cn.superdesk.uniorder.interceptor.logger.annotation.ResponseExcel;
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.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 6/18/20 2:32 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 6/18/20 2:32 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "积分结算(2.0,2.1)-未对账状态", tags = {"积分结算(2.0,2.1)-未对账状态"})
@Slf4j
@RestController
@RequestMapping("/accounting/score/customer/notyet")
public class ScoreCustomerNotYetDimCreditController extends BaseController {

    @Autowired
    private UnifiedOrderDubboConsumerService       unifiedOrderDubboConsumerService;
    @Autowired
    private UnifiedOrderCreditDubboConsumerService unifiedOrderCreditDubboConsumerService;

    @Autowired
    private AccountingUnifiedCustomerDubboConsumerService accountingUnifiedCustomerDubboConsumerService;
    @Autowired
    private AccountingScoreCustomerDubboConsumerService   accountingScoreCustomerDubboConsumerService;

    @Autowired
    private InvoiceApplyCustomerDubboConsumerService invoiceApplyDubboConsumerService;

    @Autowired
    private SysConfigDubboConsumerService sysConfigDubboConsumerService;
    @Autowired
    private SysInvDubboConsumerService    sysInvDubboConsumerService;
    @Autowired
    private JavaMailSender                javaMailSender;

    @Autowired
    private ProviderEnvConstantDubboCustomerService providerEnvConstantDubboCustomerService;

    @ApiOperation(value = "v2.0-订单流水管理", tags = "积分结算2.0,带数据脱敏,伪表单计划整改")
    @RequestMapping(value = "/getOrderReconciliationAll", method = RequestMethod.POST)
    @ResponseBody
    public R<List<InvoiceApplyDetailDimCreditItemVo>> getOrderReconciliationAll(@RequestBody AccountingOrderScoreCustomerNotYetDimCreditQueryCriteria queryCriteria) {

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

        try {


            // 处理到这里 已经把 table list 的数据填充了
            response = unifiedOrderDubboConsumerService.getScoreOrderReconciliationNotYetByPageQuery(queryCriteria, getToken());

//			for (ScoreOrderReconciliationNotYetVo vo : result.getData()) {
//				vo.setAccountingStateCustomerName(CommonAccountingStateEnum.getDesc(vo.getAccountingStateCustomer()));
//
//				vo.setInvoiceApplyStatusName(UnifiedOrderCreditInvoiceApplyStatusEnum.getDesc(vo.getInvoiceApplyStatus()));
//				vo.setInvoiceMakeStatusName(UnifiedOrderCreditInvoiceMakeStatusEnum.getDesc(vo.getInvoiceMakeStatus()));
//				vo.setBackStatusName(UnifiedOrderCreditBackStatusEnum.getDesc(vo.getBackStatus()));
//			}


//			订单数:131 订单金额:¥ 117566.6 应收金额:¥ 99055 退货金额:¥ 18511.6
            ScoreOrderReconciliationNotYetHeadVo scoreOrderReconciliationNotYetHeadSum = unifiedOrderDubboConsumerService.getScoreOrderReconciliationNotYetHeadSum(queryCriteria, getToken());
            response.setHeadData(AnBeanUtils.beanToMap(scoreOrderReconciliationNotYetHeadSum));

        } catch (Exception e) {

            response.setCode(String.valueOf(e.hashCode()));
            response.setSuccess(false);
            response.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }
        return response;
    }


    /***
     * 积分结算(相当于客户结算)
     * | ----> 积分结算-未结算商品
     *
     * 原名字为 getnotdzpage
     *
     * @url http://localhost:8089/web/score/getOrderReconciliationNotYet
     * @url http://localhost:8089/web/score/accounting/customer/getOrderReconciliationNotYet
     *
     * @return
     */
    @ApiOperation(value = "v2.0-未对账流水", tags = "积分结算2.0,带数据脱敏,伪表单计划整改")
    @RequestMapping(value = "/getOrderReconciliationNotYet", method = RequestMethod.POST)
    @ResponseBody
    public R<List<InvoiceApplyDetailDimCreditItemVo>> getOrderReconciliationNotYet(@RequestBody AccountingOrderScoreCustomerNotYetDimCreditQueryCriteria queryCriteria) {

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

        try {

            queryCriteria.setAccountingStateCustomer(CommonAccountingStateEnum.未对账.getKey());

            // 处理到这里 已经把 table list 的数据填充了
            response = unifiedOrderDubboConsumerService.getScoreOrderReconciliationNotYetByPageQuery(queryCriteria, getToken());

            ScoreOrderReconciliationNotYetHeadVo scoreOrderReconciliationNotYetHeadSum = unifiedOrderDubboConsumerService.getScoreOrderReconciliationNotYetHeadSum(queryCriteria, getToken());

            response.setHeadData(AnBeanUtils.beanToMap(scoreOrderReconciliationNotYetHeadSum));

        } catch (Exception e) {

            response.setCode(String.valueOf(e.hashCode()));
            response.setSuccess(false);
            response.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 积分结算(相当于客户结算)
     * | ----> 积分结算-未结算流水 | 此页面是以商品流水的维度去展示的 opt 搜索 下单时间 只能以订单维度 最终
     * | ----> 积分结算-订单流水管理
     * getexcel
     *
     * @throws IOException
     */
    @ResponseExcel
    @ApiOperation(value = "积分结算2.0-未对账流水|订单流水管理-op导出报表", tags = "积分结算2.0,带数据脱敏,伪表单计划整改")
    @RequestMapping(value = "/getOrderReconciliationNotYet2Excel", method = RequestMethod.POST)
    public R<String> exportExcelOrderReconciliationNotYet2Excel(@RequestBody AccountingOrderScoreCustomerNotYetDimCreditScene1QueryCriteria queryCriteria) throws IOException {

        log.info("\n积分结算(相当于客户结算)\n" +
                "\n| ----> 积分结算-未结算流水 | 此页面是以商品流水的维度去展示的 opt 搜索 下单时间 只能以订单维度 最终\n" +
                "\n| ----> 积分结算-订单流水管理" +
                "\n{}", JSONUtil.toJsonStr(queryCriteria));


//		getScoreOrderReconciliationNotYet additionalInfoGetScoreOrderReconciliationNotYet 已修
        return new R<String>(unifiedOrderDubboConsumerService.exportExcelScoreOrderReconciliationNotYet(queryCriteria, getToken()));


    }


}
