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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
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.dto.param.invoice.customer.InvoiceApplyCustomerQueryCriteria;
import cn.superdesk.uniorder.dto.param.invoice.customer.InvoiceApplyCustomerQueryCriteriaV2_1;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.InvoiceApplyCustomerDubboConsumerService;
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.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/2/23 下午4:20
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/2/23 下午4:20
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "客户结算-发票管理-Excel导出)", tags = {"客户结算-发票管理-Excel导出"})
@Slf4j
@RestController
@RequestMapping("/invoice/apply/customer/export")
public class InvoiceApplyCustomerExportExcelController extends BaseController {

    @Autowired
    private InvoiceApplyCustomerDubboConsumerService invoiceApplyCustomerDubboConsumerService;


//	@ApiOperation(value = "op导出列表")
//	@RequestMapping(value = "/exportInvoiceApplyListExcel", method = RequestMethod.POST)
//	public R<String> exportExcelInvoiceApplyCustomerFocusScoreList(@RequestBody InvoiceApplyCustomerQueryCriteria queryCriteria) throws IOException {
//
//		log.info("{}", JSONUtil.toJsonStr(queryCriteria));
//
//		return new R<>(invoiceApplyCustomerDubboConsumerService.exportExcelInvoiceApplyCustomerFocusScoreList(queryCriteria, getToken()));
//	}

    @ApiOperation(value = "op导出列表-积分结算-发票申请管理-积分结算2.1 已适配")
    @RequestMapping(value = "/exportInvoiceApplyListExcel/V2_1", method = RequestMethod.POST)
    public R<String> exportInvoiceApplyListExcelV2_1(@RequestBody InvoiceApplyCustomerQueryCriteriaV2_1 queryCriteria) {


        log.info("\n\n\nexportInvoiceApplyListExcelV2_1\nparams \n{}\ntoken  \n{}\n\n\n\n", JSONUtil.toJsonStr(queryCriteria), getToken());


        return new R<>(invoiceApplyCustomerDubboConsumerService.exportInvoiceApplyListExcelV2_1(queryCriteria, getToken()));
    }


    /**************************************** 详情 start ****************************************/

    /**
     * 前端
     * <p>
     * 客户结算-申请开票-查看详情-opt 导出详情
     * 客户结算-发票管理-查看详情-opt 导出明细
     *
     * @return
     * @url /invoice/apply/customer/export/unified/UnifiedCustomerInvoice2GoodsDetail/{invoiceApplyId}
     * 6379
     */
    @ApiOperation(value = "客户结算-申请开票|发票管理-查看详情-opt 导出详情")
    @RequestMapping(value = "/unified/UnifiedCustomerInvoice2GoodsDetail/{invoiceApplyId}", method = RequestMethod.POST)
    public R<String> exportUnifiedCustomerInvoice2GoodsDetail(@PathVariable Long invoiceApplyId) {

        Map<String, Object> params = new HashMap<>();

        // 发票申请Id invoiceApplyId
        params.put("id", invoiceApplyId);

        log.info("客户结算-发票申请-opt 查看详情 params : \n{}", AnJsonUtils.toJson(params));

        return new R<String>(invoiceApplyCustomerDubboConsumerService.exportUnifiedCustomerInvoice2GoodsDetail(params, getToken()));
    }

    /**
     * 前端
     * <p>
     * 积分结算-申请开票-opt 查看详情
     * 积分结算-发票管理-opt 查看详情
     * mark by linjinyu at 20211112 已完成
     *
     * @return
     * @url /invoice/apply/customer/export/score/UnifiedCustomerInvoice2CreditDetail
     */
    @ApiOperation(value = "积分结算-申请开票|发票管理-查看详情-opt 导出详情 (未完成 TODO)")
    @RequestMapping(value = "/score/UnifiedCustomerInvoice2CreditDetail", method = RequestMethod.POST)
    public R<String> exportUnifiedCustomerInvoice2CreditDetail(@RequestBody InvoiceApplyCustomerQueryCriteria criteria) {

//		Map<String, Object> params = new HashMap<>();
//
//		// 发票申请Id
//		params.put("id", getLong("id"));

        log.info("积分结算-发票申请管理-opt 查看详情 params : \n{}", AnJsonUtils.toJson(criteria));

        return new R<String>(invoiceApplyCustomerDubboConsumerService.exportUnifiedCustomerInvoice2CreditDetail(criteria, getToken()));
    }

    /**************************************** 详情 start ****************************************/


    /**
     * /invoice/apply/customer/score/exportInvoiceApplyExcel
     * /invoice/apply/customer/export/exportInvoiceApplyExcel
     * 侨城汇-发票申请管理 -
     * <p>
     * // TODO mode 有逻辑 侨城汇
     * //	|---- 1. 搜索框 条件 结算集 op 导出报表
     * //	|---- 2. 搜索框 条件 结算集 后 界面上 勾选 要以 开票申请编号 分 sheet 分合并前 合并后 op 勾选导出报表
     * //	|---- 3. 搜索框 条件 结算集 后 界面上 勾选 要以 开票申请编号 分 sheet 财务专用 op 导出开票数据
     * private Integer mode;
     *
     * @param queryCriteria
     * @throws IOException
     */
    @ApiOperation(value = "op导出报表-积分结算-发票申请管理-TODO mode 有逻辑 侨城汇 - 积分结算2.0 已适配\n" +
            "\t|---- 1. 搜索框 条件 结算集 op导出报表\n" +
            "\t|---- 2. 搜索框 条件 结算集 后 界面上 勾选 要以 开票申请编号 分 sheet 分合并前 合并后 op勾选导出报表\n" +
            "\t|---- 3. 搜索框 条件 结算集 后 界面上 勾选 要以 开票申请编号 分 sheet 财务专用 op导出开票数据")
    @RequestMapping(value = "/exportInvoiceApplyExcel", method = RequestMethod.POST)
    public R<String> exportExcelInvoiceApplyCustomerFocusScoreDimCredit(@RequestBody InvoiceApplyCustomerQueryCriteria queryCriteria) throws IOException {

        log.info("{}", JSONUtil.toJsonStr(queryCriteria));

        if (ObjectUtil.isNotEmpty(queryCriteria.getMode())) {

            int count = 0;

            if (CollUtil.isNotEmpty(queryCriteria.getInvoiceApplyIdsSelected())) {
                count++;
            }
            if (CollUtil.isNotEmpty(queryCriteria.getInvoiceApplySnsSelected())) {
                count++;
            }
            if (CollUtil.isNotEmpty(queryCriteria.getFpqqlshSelected())) {
                count++;
            }

            if (count != 1) {
                return ResponseResultOuterUtils.createErrorResult("500", "参数错误");
            }
        }

        return new R<>(invoiceApplyCustomerDubboConsumerService.exportExcelInvoiceApplyCustomerFocusScoreDimCredit(queryCriteria, getToken()));
    }


}
