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

import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.enums.login.SysLoginTypeEnum;
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.common.rest.response.ResponseResultOuterUtils;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dto.param.accounting.unified.supplier.AccountingOrderUnifiedSupplierQueryCriteria;
import cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.AccountingOrderUnifiedSuplierHeadVo;
import cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.AccountingOrderUnifiedSupplierListItemVo;
import cn.superdesk.uniorder.dubbo.consumer.accounting.supplier.AccountingUnifiedSupplierDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysSupplierDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.sys.SysUserDubboConsumerService;
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: 2020/11/3 下午5:41
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020/11/3 下午5:41
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "[对账单]-供应商结算-对账单v2", tags = {"[对账单]-供应商结算-对账单v2"})
@Slf4j
@RestController
@RequestMapping("/accounting/unified/supplier/statement/v2")
public class AccountingUnifiedSupplierVer2Controller extends BaseController {


    @Autowired
    private UnifiedOrderDubboConsumerService              unifiedOrderDubboConsumerService;
    @Autowired
    private SysSupplierDubboConsumerService               unifiedSupplierDubboConsumerService;
    @Autowired
    private AccountingUnifiedSupplierDubboConsumerService accountingUnifiedSupplierDubboConsumerService;

    @Autowired
    private SysUserDubboConsumerService sysUserDubboConsumerService;

    /***
     * 供应商结算-对账单
     *
     * @url: http://order.avic-s.com/#/vendorAccountStatement?id=72
     * @return
     */
    @ApiOperation(value = "供应商结算-对账单-op列表", notes = "供应商结算2.0")
    @RequestMapping(value = "/getStatementOfAccountList", method = RequestMethod.POST)
    @ResponseBody
    public R<List<AccountingOrderUnifiedSupplierListItemVo>> getStatementOfAccountList(
            @RequestBody AccountingOrderUnifiedSupplierQueryCriteria queryCriteria) {

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

        try {


            log.info("供应商结算2.0-对账单列表 : \n{}", JSONUtil.toJsonStr(queryCriteria));

            // 前端 列表数据
            response = accountingUnifiedSupplierDubboConsumerService
                    .getStatementOfAccountList(
                            queryCriteria,
                            getToken()
                    );

            // 前端 头部 对账单数:1 订单数:3 成本金额:¥ 10312.92
            AccountingOrderUnifiedSuplierHeadVo accountingUnifiedSuplierHeadVo = accountingUnifiedSupplierDubboConsumerService
                    .getStatementOfAccountHeadSum(
                            queryCriteria,
                            getToken()
                    );

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

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

    /**
     * 供应商结算-对账单-导出报表
     *
     * @param queryCriteria
     * @return
     */
    @ApiOperation(value = "供应商结算-对账单-op列表-op勾选导出报表")
    @RequestMapping(value = "/getAccountingList2Excel", method = RequestMethod.POST)
    public R<String> exportExcelAccountingSupplierList(@RequestBody AccountingOrderUnifiedSupplierQueryCriteria queryCriteria) {


        return new R<String>(accountingUnifiedSupplierDubboConsumerService.exportExcelAccountingSupplierList(queryCriteria, getToken()));
    }


    @ApiOperation(value = "供应商结算-对账单-op列表-引用 2.商户端专用;取当前供应商下在侨城汇结算中心所有有效(未确认|已确认|已申诉)的对账单_不含对账单状态为_待提交_已取消_的对账单_不含非当前供应商的对账单", notes = "供应商结算2.0")
    @RequestMapping(value = "/getStatementOfAccountList/loginType/Supplier", method = RequestMethod.POST)
    @ResponseBody
    public R<List<AccountingOrderUnifiedSupplierListItemVo>> getStatementOfAccountListFacadeSupplier(@RequestBody AccountingOrderUnifiedSupplierQueryCriteria queryCriteria) {


        /****************************************** 先这样做啦,虽然丑 差异 加参 start ******************************************/

        String token = getToken();

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        if (loginSysUser.getOutLogin().compareTo(SysLoginTypeEnum.福利商城_侨城汇.getKey()) != 0) {
            return ResponseResultOuterUtils.createErrorResult("500", "商户端专用");
        } else {

            // 对账单状态:3-待提交;0-未确认;1-已确认;2-已拒绝;-1-已取消;
            // 供应商 | 商户 专用
            // 指定

//			<!-- 对账单状态:3-待提交;0-未确认;1-已确认;2-已拒绝;-1-已取消; -->
//		<!-- and outLogin != '' -->
//		<!-- and accountingStatus != '' -->
//		<choose>
//			<when test="outLogin != null and outLogin == 0">
//
//				<choose>
//					<when test="accountingStatus != null ">
//					and ausu.accounting_status = #{accountingStatus,jdbcType=TINYINT}
//					</when>
//					<otherwise>
//					and ausu.accounting_status not in (-1)
//					</otherwise>
//				</choose>
//
//			</when>
//			<when test="outLogin != null and outLogin == 2"> 0-统一订单;1-服务之窗;2-福利商城_侨城汇
//
//				<choose>
//					<when test="accountingStatus != null ">
//					and ausu.accounting_status = #{accountingStatus,jdbcType=TINYINT}
//					</when>
//					<otherwise>
//					and ausu.accounting_status in (0,1,2)
//					</otherwise>
//				</choose>
//
//			</when>
//			<when test="outLogin == null ">
//				<if test="accountingStatus != null ">
//					and ausu.accounting_status = #{accountingStatus,jdbcType=TINYINT}
//				</if>
//			</when>
//			<otherwise>
//					and ausu.accounting_status not in (-1)
//					</otherwise>
//		</choose>
//			<!-- and invoiceStatus != '' -->
//		<if test="invoiceStatus != null ">
//					and ausu.invoice_status = #{invoiceStatus,jdbcType=TINYINT}
//		</if>
//			未申请(0, "未申请"),// 还没有申请时 还是对账单时
//					待开票(1, "待开票"),
//
//					已作废(2, "已作废"),
//
//					待寄票(20, "待寄票"),
//					待收票(30, "待收票"),
//					待付款(40, "待付款"),
//					已完成(99, "已完成"),

            queryCriteria.setOutLogin(loginSysUser.getOutLogin());// sql 写死 (accountingStatus == null) && (outLogin == 2) => ausu.accounting_status in (0,1,2)
//			queryCriteria.setOutSaleId(loginSysUser.getSaleId());
            queryCriteria.setSupplierId(loginSysUser.getSaleId());
//			queryCriteria.setInvoiceStatus(InvoiceApplySupplierInvoiceProgressingStatusEnum.未申请.getKey()); // 按这个逻辑 是把这地方屏蔽
        }

        /****************************************** 先这样做啦,虽然丑 差异 加参   end ******************************************/

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

        try {


            log.info("供应商结算2.0-对账单列表 : \n{}", JSONUtil.toJsonStr(queryCriteria));

            // 前端 列表数据
            response = accountingUnifiedSupplierDubboConsumerService.getStatementOfAccountList(queryCriteria, getToken());

            // 前端 头部 对账单数:1 订单数:3 成本金额:¥ 10312.92
            AccountingOrderUnifiedSuplierHeadVo accountingUnifiedSuplierHeadVo = accountingUnifiedSupplierDubboConsumerService.getStatementOfAccountHeadSum(queryCriteria, getToken());

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

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


}
