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

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.json.AnJsonUtils;
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.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplier;
import cn.superdesk.uniorder.dto.param.accounting.unified.supplier.AccountingSupplierNotYetDimGoodsQueryCriteria;
import cn.superdesk.uniorder.dto.param.accounting.unified.supplier.SuplierStatementOfAccountDetailQueryCriteria;
import cn.superdesk.uniorder.dto.param.accounting.unified.supplier.AccountingUnifiedSupplierOpCreateParamDimGoods;
import cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.notyet.UnifiedOrderJoinGoodsOfSupplierStatementOfAccountVo;
import cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.notyet.UnifiedOrderSupplierHeadSumVo;
import cn.superdesk.uniorder.dubbo.consumer.accounting.supplier.AccountingUnifiedSupplierDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderDubboConsumerService;
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.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/7/8 下午5:00
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/7/8 下午5:00
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "[对账单]-供应商结算-未对账商品", tags = {"[对账单]-供应商结算-未对账商品"})
@Slf4j
@RestController
@RequestMapping(value = "/accounting/unified/supplier/dim/goods/notyet")
public class UnifiedSupplierNotYetDimGoodsController extends BaseController {


    @Autowired
    private UnifiedOrderDubboConsumerService unifiedOrderDubboConsumerService;

    @Autowired
    private AccountingUnifiedSupplierDubboConsumerService accountingUnifiedSupplierDubboConsumerService;

    /**
     * 已添加 数据脱敏逻辑 20211203
     * 供应商结算-未对账商品
     *
     * @return
     * @url : http://order.avic-s.com/#/vendorUnreconOrder?id=71
     * <p>
     * /accounting/unified/supplier/dim/goods/notyet/getSupplierUnAccountingDimGoodsList
     * <p>
     * 未对账列表分页
     * , tags = {""}
     */
    @ApiOperation(value = "供应商结算-未对账商品-维度-商品", notes = "供应商结算2.0,数据脱敏")
    @RequestMapping(value = "/getSupplierUnAccountingDimGoodsList", method = RequestMethod.POST)
    @ResponseBody
    public R<List<UnifiedOrderJoinGoodsOfSupplierStatementOfAccountVo>> getSupplierUnAccountingDimGoodsList(@RequestBody AccountingSupplierNotYetDimGoodsQueryCriteria criteria) {

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

        try {

            criteria.setAccountingStateSupplier(CommonAccountingStateEnum.未对账.getKey());

            log.info("\n供应商结算-未对账商品-维度-商品-params {}\n", AnJsonUtils.toJson(criteria));

            // 已添加 数据脱敏逻辑 20211203
            response = unifiedOrderDubboConsumerService.getSupplierUnAccountingDimGoodsListByPageQuery(criteria, getToken());

            UnifiedOrderSupplierHeadSumVo supplierUnAccountingDimGoodsHeadSum = unifiedOrderDubboConsumerService.getSupplierUnAccountingDimGoodsHeadSum(criteria, getToken());

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

        } 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;
    }


    /**
     * 已添加 数据脱敏逻辑 20211203
     * 供应商结算-未对账订单 opt 导出报表 (未对账列表-导出)
     * <p>
     * /accounting/unified/supplier/notyet/exportuaccountdata
     * <p>
     * http://192.168.1.223:8089/web/accounting/unified/supplier/notyet/exportuaccountdata?orderIds=55032,55031&token=eyJhbGciOiJIUzI1NiJ9.eyJzYWxlaWQiOm51bGwsInZpcnRJZHMiOm51bGwsIm91dExvZ2luIjoiMCIsInVzZXJOYW1lIjoid2FuZ3N5Iiwicm9sZVR5cGUiOiIxIiwib2NpZCI6bnVsbCwiZXhwIjoxNjAxMDAwNzgyLCJ1c2VySWQiOiI2MCIsImlhdCI6MTYwMDkxNDM4Mn0.2L-ZP_c2fdgREcvSVMpLa_HWjoKCY0RLGFnx2BtJoiQ
     *
     * @return
     */
    @ApiOperation(value = "供应商结算-未对账商品-opt 导出报表-维度-商品", notes = "供应商结算2.0,数据脱敏")
    @RequestMapping(value = "/exportSupplierUnAccountingDimGoodsList", method = RequestMethod.POST)
    public R<String> exportExcelAccountingSupplierNotYetDimGoods(@RequestBody AccountingSupplierNotYetDimGoodsQueryCriteria criteria) {

        criteria.setAccountingStateSupplier(CommonAccountingStateEnum.未对账.getKey());

        log.info("供应商结算-未对账订单 opt 导出报表 (未对账列表-导出) params: \n{}", AnJsonUtils.toJson(criteria));

        // 已添加 数据脱敏逻辑 20211203
        return new R<String>(unifiedOrderDubboConsumerService.exportExcelAccountingSupplierNotYetDimGoods(criteria, getToken()));
    }


    /**
     * 供应商结算-未对账商品-创建对账单-维度-商品
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "供应商结算-未对账商品-创建对账单-维度-商品", notes = "积分结算2.1")
    @RequestMapping(value = "/createStatementOfAccount", method = RequestMethod.POST)
    @ResponseBody
    public R<AccountingUnifiedSupplier> createStatementOfAccount(@RequestBody AccountingUnifiedSupplierOpCreateParamDimGoods params) {

        R<AccountingUnifiedSupplier> response = new R<>();

        try {

            response = accountingUnifiedSupplierDubboConsumerService.opCreateAccountingDimGoods(getToken(), params);

        } 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;
    }

    /**
     * 已添加 数据脱敏逻辑 20211203
     * 供应商结算-对账单-opt btn 编辑对账
     * 供应商结算-对账单-opt btn 查看详情
     *
     * @return
     */
    @ApiOperation(value = "供应商结算-对账单-opt btn 编辑对账 | opt btn 查看详情-维度-商品", notes = "供应商结算2.0,数据脱敏")
    @RequestMapping(value = "/statementOfAccountDetail", method = RequestMethod.POST)
    @ResponseBody
    public R<List<UnifiedOrderJoinGoodsOfSupplierStatementOfAccountVo>> statementOfAccountDetail(@RequestBody SuplierStatementOfAccountDetailQueryCriteria queryCriteria) {

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

        try {

            // 已添加 数据脱敏逻辑 20211203
            List<UnifiedOrderJoinGoodsOfSupplierStatementOfAccountVo> resultList = unifiedOrderDubboConsumerService.getSupplierStatementOfAccountDetailDimGoods2Goods(queryCriteria, getToken());

            // 随便写个 【【结算中心-测试环境】 供应商结算 > 对账单 > 对账单详情-订单未按照下单时间升序排列】https://www.tapd.cn/33003868/bugtrace/bugs/view?bug_id=1133003868001004808
            List<UnifiedOrderJoinGoodsOfSupplierStatementOfAccountVo> resultListSortByOrderCreateTimeOrderSn = resultList.stream()
                    .sorted(
                            Comparator
                                    .comparing(UnifiedOrderJoinGoodsOfSupplierStatementOfAccountVo::getCreateTime)
                                    .thenComparing(UnifiedOrderJoinGoodsOfSupplierStatementOfAccountVo::getOrderSn)
                    )
                    .collect(Collectors.toList());


            response.setData(resultListSortByOrderCreateTimeOrderSn);

        } 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;
    }

    /**
     * 已添加 数据脱敏逻辑 20211203
     * 供应商结算-未对账订单-对账单详情-详情导出
     *
     * @param queryCriteria
     * @return
     * @throws IOException
     * @url /accounting/unified/supplier/dim/goods/notyet/exportStatementOfAccountDetail
     */
    @ApiOperation(value = "供应商结算-对账单-对账单详情-详情导出-维度-商品-带出商品-20210413-改版", notes = "供应商结算2.0,数据脱敏")
    @RequestMapping(value = "/exportStatementOfAccountDetail", method = RequestMethod.POST)
    public R<String> exportSupplierStatementOfAccountDetail(@RequestBody SuplierStatementOfAccountDetailQueryCriteria queryCriteria) throws IOException {


//		String accountingOrderSn
//		Map<String, Object> params = new HashMap<>();
//		params.put("accountingOrderSn", accountingOrderSn);

        log.info("供应商结算-未对账订单-对账单详情-详情导出 params: \n{}", JSONUtil.toJsonStr(queryCriteria));

        // 已添加 数据脱敏逻辑 20211203
        return new R<String>(unifiedOrderDubboConsumerService.exportSupplierStatementOfAccountDetailCompatible(queryCriteria, getToken()));

    }
}
