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

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.score.customer.AccountingScoreCustomer;
import cn.superdesk.uniorder.dto.param.accounting.score.customer.AccountingOrderScoreCustomerCreateParam;
import cn.superdesk.uniorder.dto.param.accounting.score.customer.AccountingOrderScoreCustomerOpDetailQueryCriteria;
import cn.superdesk.uniorder.dto.param.accounting.score.customer.AccountingOrderScoreCustomerQueryCriteria;
import cn.superdesk.uniorder.dto.param.invoice.customer.status.OpMailTickStatusFormV2_2;
import cn.superdesk.uniorder.dto.vo.accounting.score.customer.AccountingOrderScoreCustomerDetailVo;
import cn.superdesk.uniorder.dto.vo.accounting.score.customer.AccountingOrderScoreCustomerHeadVo;
import cn.superdesk.uniorder.dto.vo.accounting.score.customer.AccountingOrderScoreCustomerListItemVo;
import cn.superdesk.uniorder.dto.vo.invoice.customer.InvoiceApplyDetailDimCreditItemVo;
import cn.superdesk.uniorder.dubbo.consumer._permission.PermissionDubboCustomerService;
import cn.superdesk.uniorder.dubbo.facade.accounting.customer.DubboAccountingScoreCustomerService;
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;

/**
 * @Description: 积分结算
 * @Author: linjinyu
 * @CreateDate: 6/28/20 1:06 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 6/28/20 1:06 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class AccountingScoreCustomerDubboConsumerService {


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

    @Resource
    private PermissionDubboCustomerService permissionDubboCustomerService;

    /****************************************** 积分结算 对账单 start ******************************************/


    public List<Long> getAccountingXCreditIds(Long id) {
        return dubboAccountingScoreCustomerService.getAccountingXCreditIdsByAccountingOrderId(id);
    }

    //
//	public ScoreAccountingCustomerHeadVo getAccountingListHeadSum(Map<String, Object> params) {
//
//		return dubboAccountingScoreCustomerService.getAccountingListHeadSum(params);
//
//	}
    public R<List<AccountingOrderScoreCustomerListItemVo>> pageQueryAccountingOrderList(AccountingOrderScoreCustomerQueryCriteria queryCriteria, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        // 打开 20210721 mark by linjinyu
        Boolean isPass = permissionDubboCustomerService.addOnPerm(token, queryCriteria);
        if (isPass.equals(false)) {
            return null;
        }

        return dubboAccountingScoreCustomerService.pageQueryAccountingOrderList(queryCriteria, loginSysUser);
    }

    public List<AccountingOrderScoreCustomerListItemVo> queryAccountingOrderList(AccountingOrderScoreCustomerQueryCriteria queryCriteria, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        return dubboAccountingScoreCustomerService.queryAccountingOrderList(queryCriteria, loginSysUser);
    }

    public AccountingOrderScoreCustomerListItemVo getAccountingOrderListByAccountingOrderSn(String accountingOrderSn, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
        return dubboAccountingScoreCustomerService.getAccountingOrderListByAccountingOrderSn(accountingOrderSn, loginSysUser);
    }

    public String exportExcelAccountingOrderScoreCustomerList(AccountingOrderScoreCustomerQueryCriteria queryCriteria, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        return dubboAccountingScoreCustomerService.exportExcelAccountingOrderScoreCustomerList(queryCriteria, loginSysUser);
    }

    public AccountingOrderScoreCustomerHeadVo getAccountingOrderListHeadSum(String token, AccountingOrderScoreCustomerQueryCriteria queryCriteria) {

        // 打开 20210721 mark by linjinyu
        Boolean isPass = permissionDubboCustomerService.addOnPerm(token, queryCriteria);
        if (isPass.equals(false)) {
            return null;
        }

        return dubboAccountingScoreCustomerService.getAccountingOrderListHeadSum(queryCriteria);
    }

    /****************************************** 积分结算 对账单   end ******************************************/

    /****************************************** 积分结算 对账单 详情 start ******************************************/

    public AccountingOrderScoreCustomerDetailVo commonStatementOfAccountDetailDimCredit(AccountingOrderScoreCustomerOpDetailQueryCriteria queryCriteria, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

//		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, params);
//		if (isPass.equals(false)) {
//			return null;
//		}

        return dubboAccountingScoreCustomerService.commonStatementOfAccountDetailDimCredit(queryCriteria, loginSysUser);
    }

    public String exportExcelAccountingOrderScoreCustomerDetailDimCredit(AccountingOrderScoreCustomerOpDetailQueryCriteria queryCriteria, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

//		Boolean isPass = permissionDubboCustomerService.addOnPerm(token, params);
//		if (isPass.equals(false)) {
//			return null;
//		}

        return dubboAccountingScoreCustomerService.exportExcelAccountingOrderScoreCustomerDetailDimCredit(queryCriteria, loginSysUser);
    }

    /****************************************** 积分结算 对账单 详情   end ******************************************/


    /**
     * 积分订单 的 客户结算单
     * 效率优化 mark by linjinyu at 20211203
     * 与 createProduceNew 对映
     *
     * @param token
     * @param scoreOrderReconciliationNotYetVoList
     * @param scoreAccountingCustomerCreateParam
     * @return
     * @throws NumberFormatException
     * @throws Exception
     */
    public AccountingScoreCustomer createScoreAccountingCustomer(String token, List<InvoiceApplyDetailDimCreditItemVo> scoreOrderReconciliationNotYetVoList, AccountingOrderScoreCustomerCreateParam scoreAccountingCustomerCreateParam) throws NumberFormatException, Exception {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        return dubboAccountingScoreCustomerService.createScoreAccountingCustomer(
                scoreOrderReconciliationNotYetVoList,
                scoreAccountingCustomerCreateParam, loginSysUser);
    }

    /**
     * *********************************************** 积分结算单 end  ******************************************************
     **/


    /**************************************** 对账单操作部分 start ****************************************/


    /**
     * 重新对账
     *
     * @param accountingOrderId
     * @return
     */
    public int opCancelAccounting(Long accountingOrderId, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        return dubboAccountingScoreCustomerService.opCancelAccounting(accountingOrderId, loginSysUser);
    }

    /**
     * 拒绝对账
     *
     * @param accountingOrderId
     * @param token
     * @return
     */
    public int opRefuseAccounting(Long accountingOrderId, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        return dubboAccountingScoreCustomerService.opRefuseAccounting(accountingOrderId, loginSysUser);

    }

    /**
     * 确认对账
     *
     * @param accountingOrderId
     * @param token
     * @return
     */
    public int opConfirmAccounting(Long accountingOrderId, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        return dubboAccountingScoreCustomerService.opConfirmAccounting(accountingOrderId, loginSysUser);
    }

    /**
     * 删除 对账单 及 对账单X积分流水 上位去处理 是否能删除吧 这里不管 物理删除
     *
     * @param accountingOrderId
     * @param token
     * @return
     */
    public int opDeleteAccounting(Long accountingOrderId, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        return dubboAccountingScoreCustomerService.opDeleteAccounting(accountingOrderId, loginSysUser);
    }

    /**************************************** 对账单操作部分   end ****************************************/


    /**
     * 删除 对账单 物理删除 不删除 对账单X积分流水
     *
     * @param id
     */
    public void deleteObj(Long id) {

        dubboAccountingScoreCustomerService.deleteObj(id);
    }

    public int opUpdateByPrimaryKeySelective(AccountingScoreCustomer statementOfAccount4Update, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        return dubboAccountingScoreCustomerService.opUpdateByPrimaryKeySelective(statementOfAccount4Update, loginSysUser);
    }

    public int opUpdateFieldRemark(AccountingScoreCustomer statementOfAccount4Update, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
        return dubboAccountingScoreCustomerService.opUpdateFieldRemark(statementOfAccount4Update, loginSysUser);
    }

    /**
     * 联合使用
     * invoiceApplyFieldMailTickStatusDubboConsumerService.opCompleteV2_1(entitysOnlyOffLine, formV2_1, getToken());
     * int ret = accountingScoreCustomerDubboConsumerService.opExpressRecord(formV2_2, accountingScoreCustomer, getToken());
     *
     * @param formV2_2
     * @param source
     * @param token
     * @return
     */
    public int opExpressRecord(OpMailTickStatusFormV2_2 formV2_2, AccountingScoreCustomer source, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        AccountingScoreCustomer forUpdate = new AccountingScoreCustomer();

        forUpdate.setId(source.getId());
        forUpdate.setExpressCompanyId(formV2_2.getExpressCompanyId());
        forUpdate.setExpressNo(formV2_2.getExpressNo());

        return dubboAccountingScoreCustomerService.opUpdateByPrimaryKeySelective(forUpdate, loginSysUser);
    }

    public AccountingScoreCustomer findById(Long id) {
        return dubboAccountingScoreCustomerService.findById(id);
    }

    public AccountingScoreCustomer findByAccountingOrderSn(String accountingOrderSn) {
        return dubboAccountingScoreCustomerService.findByAccountingOrderSn(accountingOrderSn);
    }

    public List<AccountingScoreCustomer> findByIds(List<Long> accountingOrderIds) {
        return dubboAccountingScoreCustomerService.findByIds(accountingOrderIds);
    }

    @Deprecated
    public void addObj(AccountingScoreCustomer scoreAccountingCustomer) {

    }

    @Deprecated
    public List<AccountingScoreCustomer> findAll(Map<String, Object> queryMap) {

        return null;
    }

}
