package cn.superdesk.uniorder.dubbo.consumer.accounting.supplier;

import cn.hutool.core.util.ObjectUtil;
import cn.superdesk.uniorder.common.numerical.AnLongUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplier;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrderGoods;
import cn.superdesk.uniorder.dto.param.accounting.unified.supplier.AccountingOrderUnifiedSupplierQueryCriteria;
import cn.superdesk.uniorder.dto.param.accounting.unified.supplier.AccountingUnifiedSupplierOpCreateParamDimGoods;
import cn.superdesk.uniorder.dto.param.accounting.unified.supplier.UnifiedSupplierCreateDimOrderParam;
import cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.AccountingOrderUnifiedSuplierHeadVo;
import cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.AccountingOrderUnifiedSupplierListItemVo;
import cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.AccountingOrderUnifiedSupplierStatisticsFixVoDimGoods;
import cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.AccountingOrderUnifiedSupplierStatisticsFixVoDimOrder;
import cn.superdesk.uniorder.dubbo.consumer._permission.PermissionDubboCustomerService;
import cn.superdesk.uniorder.dubbo.facade.accounting.supplier.DubboAccountingUnifiedSupplierService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Slf4j
@Component
public class AccountingUnifiedSupplierDubboConsumerService {

    @Reference(version = "1.0.0", timeout = 30000, retries = -1)
    private DubboAccountingUnifiedSupplierService dubboAccountingUnifiedSupplierService;

    @Resource
    private PermissionDubboCustomerService permissionDubboCustomerService;

    /**
     * 供应商结算-对账单列表
     * 引用
     * 1. 后端端
     * 2. 商户端
     *
     * @param params
     * @param pageNum
     * @param pageSize
     * @param token
     * @return
     */
    @Deprecated
    public R<List<AccountingOrderUnifiedSupplierListItemVo>> getStatementOfAccountList(Map<String, Object> params, int pageNum, int pageSize, String token) {

        // 20191205 add 数据权限
        Boolean isPass = permissionDubboCustomerService.addOnPerm(token, params);
        if (isPass.equals(false)) {
            R<List<AccountingOrderUnifiedSupplierListItemVo>> responseResult = new R<>();
            responseResult.setSuccess(false);
            responseResult.setMessage("您没有企业数据权限！");
            return responseResult;
        }

        Long supplierId = AnLongUtils.getLong(permissionDubboCustomerService.getPermOutSaleId(token));
        params.put("supplierId", supplierId);

        return dubboAccountingUnifiedSupplierService.getStatementOfAccountList(params, pageNum, pageSize);
    }

    /**
     * 供应商结算2.0-对账单列表
     * 引用
     * 1. 后端端
     * 2. 商户端
     *
     * @param queryCriteria
     * @param token
     * @return
     */
    public R<List<AccountingOrderUnifiedSupplierListItemVo>> getStatementOfAccountList(
            AccountingOrderUnifiedSupplierQueryCriteria queryCriteria,
            String token) {

        // 20191205 add 数据权限
        Boolean isPass = permissionDubboCustomerService.addOnPerm(token, queryCriteria);
        if (isPass.equals(false)) {
            R<List<AccountingOrderUnifiedSupplierListItemVo>> responseResult = new R<>();
            responseResult.setSuccess(false);
            responseResult.setMessage("您没有企业数据权限！");
            return responseResult;
        }

        Long outSaleId = AnLongUtils.getLong(permissionDubboCustomerService.getPermOutSaleId(token));

        if(ObjectUtil.isNotNull(queryCriteria.getSupplierId())){

        } else {
            queryCriteria.setSupplierId(outSaleId);
        }




        return dubboAccountingUnifiedSupplierService.getStatementOfAccountList(queryCriteria);
    }

    @Deprecated
    public AccountingOrderUnifiedSuplierHeadVo getStatementOfAccountHeadSum(Map<String, Object> params, String token) {

        // 20191205 add 数据权限
        Boolean isPass = permissionDubboCustomerService.addOnPerm(token, params);
        if (isPass.equals(false)) {
            return null;
        }

        return dubboAccountingUnifiedSupplierService.getStatementOfAccountHeadSum(params);
    }

    public AccountingOrderUnifiedSuplierHeadVo getStatementOfAccountHeadSum(
            AccountingOrderUnifiedSupplierQueryCriteria queryCriteria,
            String token) {

        // 20191205 add 数据权限
        Boolean isPass = permissionDubboCustomerService.addOnPerm(token, queryCriteria);
        if (isPass.equals(false)) {
            return null;
        }

        Long outSaleId = AnLongUtils.getLong(permissionDubboCustomerService.getPermOutSaleId(token));

        if(ObjectUtil.isNotNull(queryCriteria.getSupplierId())){

        } else {
            queryCriteria.setSupplierId(outSaleId);
        }

        return dubboAccountingUnifiedSupplierService.getStatementOfAccountHeadSum(queryCriteria);
    }


    public AccountingUnifiedSupplier findById(Long id) {

        return dubboAccountingUnifiedSupplierService.findById(id);
    }

    public AccountingUnifiedSupplier findByAccountingOrderSn(String accountingOrderSn) {

        return dubboAccountingUnifiedSupplierService.findByAccountingOrderSn(accountingOrderSn);
    }

    public void updateObj(String token, AccountingUnifiedSupplier update) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        update.setUpdateUserId(loginSysUser.getSysUserId());

        dubboAccountingUnifiedSupplierService.updateObj(update);
    }

    public void opEditAccountingDimOrder(String token, AccountingUnifiedSupplier statementOfAccount, UnifiedOrder unifiedOrder, String add) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
        dubboAccountingUnifiedSupplierService.opEditAccountingDimOrder(statementOfAccount, unifiedOrder, add, loginSysUser);
    }

    /**
     * 未实现
     *
     * @param token
     * @param statementOfAccount
     * @param unifiedOrder
     * @param add
     */
    public void opEditAccountingDimGoods(String token, AccountingUnifiedSupplier statementOfAccount, UnifiedOrder unifiedOrder, UnifiedOrderGoods unifiedOrderGoods, String add) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
        dubboAccountingUnifiedSupplierService.opEditAccountingDimGoods(statementOfAccount, unifiedOrder, unifiedOrderGoods, add, loginSysUser);
    }


    public AccountingUnifiedSupplier opCreateAccountingDimOrder(String token, UnifiedSupplierCreateDimOrderParam unifiedSupplierCreateDimOrderParam) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
        return dubboAccountingUnifiedSupplierService.opCreateAccountingDimOrder(unifiedSupplierCreateDimOrderParam, loginSysUser);
    }

    public R<AccountingUnifiedSupplier> opCreateAccountingDimGoods(String token, AccountingUnifiedSupplierOpCreateParamDimGoods unifiedSupplierCreateDimGoodsParam) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
        return dubboAccountingUnifiedSupplierService.opCreateAccountingDimGoods(unifiedSupplierCreateDimGoodsParam, loginSysUser);
    }


    /**
     * 拒绝对账 (供应商端 申诉 同理)
     *
     * @param token
     * @param statementOfAccount
     */
//	@Deprecated
//	public void opRejectAccounting(String token, AccountingUnifiedSupplier statementOfAccount) {
//
//		statementOfAccount.setAccountingStatus(AccountingOrderCustomerAccountingStatusEnum.已拒绝.getKey());
//		statementOfAccount.setUpdateTime(DateUtil.date());
//
//		dubboAccountingUnifiedSupplierService.opRejectAccounting(statementOfAccount);
//	}

    /**
     * 重新对账 也就是 取消这个对账单
     *
     * @param token
     * @param statementOfAccount
     */
//	@Deprecated
//	public void opAgainAccounting(String token, AccountingUnifiedSupplier statementOfAccount) {
//
//
//		statementOfAccount.setAccountingStatus(AccountingOrderCustomerAccountingStatusEnum.已取消.getKey());
//		statementOfAccount.setUpdateTime(DateUtil.date());
//
//		dubboAccountingUnifiedSupplierService.opAgainAccounting(statementOfAccount);
//	}

    /**
     * 取消对账
     *
     * @param token
     * @param statementOfAccount
     */
//	@Deprecated
//	public void opCancelAccounting(String token, AccountingUnifiedSupplier statementOfAccount) {
//
//		statementOfAccount.setAccountingStatus(AccountingOrderCustomerAccountingStatusEnum.已取消.getKey());
//		statementOfAccount.setUpdateTime(DateUtil.date());
//
//		dubboAccountingUnifiedSupplierService.opCancelAccounting(statementOfAccount);
//	}


    /****************************************** 状态机 start ******************************************/

//	// 创建操作 -> 对账单状态 -> 待提交(3, "待提交"),
//	public void opCreate(){}
//
//	// 提交操作 -> 对账单状态 -> 未确认(0, "未确认"),// 已提交
//	public void opSubmit();
//	// 取消操作 -> 对账单状态 -> 已取消(-1, "已取消"),
//	public void opCancel();
//
//	// 重新对账操作 -> 对账单状态 -> 已取消(-1, "已取消"),
//	public void opAgain();
//
//	// 确认操作 -> 对账单状态 -> 已确认(1, "已确认"),
//	public void opConfirm();
//	// 申诉操作 -> 对账单状态 -> 已拒绝(2, "已拒绝"),
//	public void opAppeal();

//	// 创建操作 -> 对账单状态 -> 待提交(3, "待提交"),
//	public void opCreate(Long accountingOrderId, String token) {
//
//		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
//
//	}
//
//	// 提交操作 -> 对账单状态 -> 未确认(0, "未确认"),// 已提交
//	public void opSubmit(Long accountingOrderId, String token) {
//
//		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
//
//		AccountingUnifiedSupplier statementOfAccount = dubboAccountingUnifiedSupplierService.findById(accountingOrderId);
//
//		dubboAccountingUnifiedSupplierService.opSubmit(statementOfAccount, loginSysUser);
//	}
//
//	// 取消操作 -> 对账单状态 -> 已取消(-1, "已取消"),
//	public void opCancel(Long accountingOrderId, String token) {
//
//		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
//
//		AccountingUnifiedSupplier statementOfAccount = dubboAccountingUnifiedSupplierService.findById(accountingOrderId);
//		dubboAccountingUnifiedSupplierService.opCancel(statementOfAccount, loginSysUser);
//	}
//
//	public void opDelete(Long accountingOrderId, String token) {
//
//		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
//
//		AccountingUnifiedSupplier statementOfAccount = dubboAccountingUnifiedSupplierService.findById(accountingOrderId);
//
//		dubboAccountingUnifiedSupplierService.opDelete(statementOfAccount, loginSysUser);
//	}
//
//
//	// 重新对账操作 -> 对账单状态 -> 已取消(-1, "已取消"),
//	public void opAgain(String token, Long accountingOrderId) {
//
//		LoginSysUser              loginSysUser       = JwtUtil.getLoginSysUser(token);
//		AccountingUnifiedSupplier statementOfAccount = dubboAccountingUnifiedSupplierService.findById(accountingOrderId);
//		dubboAccountingUnifiedSupplierService.opAgain(statementOfAccount, loginSysUser);
//	}
//
//	// 供应商端
//	// 确认操作 -> 对账单状态 -> 已确认(1, "已确认"),
//	public void opConfirm(Long accountingOrderId, String token) {
//
//		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
//
//		AccountingUnifiedSupplier statementOfAccount = dubboAccountingUnifiedSupplierService.findById(accountingOrderId);
//
//		dubboAccountingUnifiedSupplierService.opConfirm(statementOfAccount, loginSysUser);
//	}
//
//	// 供应商端
//	// 申诉操作 -> 对账单状态 -> 已拒绝(2, "已拒绝"),
//	public void opAppeal(String token, Long accountingOrderId) {
//
//		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
//
//		AccountingUnifiedSupplier statementOfAccount = dubboAccountingUnifiedSupplierService.findById(accountingOrderId);
//
//		dubboAccountingUnifiedSupplierService.opAppeal(statementOfAccount, loginSysUser);
//
//	}

    /****************************************** 状态机   end ******************************************/

    public AccountingOrderUnifiedSupplierStatisticsFixVoDimOrder checkAmountErrorForFixDimOrder(Map<String, Object> params) {

        return dubboAccountingUnifiedSupplierService.checkAmountErrorForFixDimOrder(params);
    }

    public AccountingOrderUnifiedSupplierStatisticsFixVoDimGoods checkAmountErrorForFixDimGoods(Map<String, Object> params) {

        return dubboAccountingUnifiedSupplierService.checkAmountErrorForFixDimGoods(params);
    }


    /****************************************** 补丁程序 start ******************************************/

    public void patchXDimOrder2DimGoods(String accountingOrderSn) {
        dubboAccountingUnifiedSupplierService.patchXDimOrder2DimGoods(accountingOrderSn);
    }

    public String patchXCheckAndFix(String accountingOrderSn) {
        return dubboAccountingUnifiedSupplierService.patchXCheckAndFix(accountingOrderSn);
    }

    public String exportExcelAccountingSupplierList(AccountingOrderUnifiedSupplierQueryCriteria queryCriteria, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
        return dubboAccountingUnifiedSupplierService.exportExcelAccountingSupplierList(queryCriteria, loginSysUser);
    }

    /****************************************** 补丁程序   end ******************************************/


}
