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

import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.rest.response.R;
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.dao.entity.sys.notify.SysNotify;
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.AccountingOrderUnifiedSupplierStatisticsFixVoDimGoods;
import cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.AccountingOrderUnifiedSupplierStatisticsFixVoDimOrder;
import cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.AccountingOrderUnifiedSupplierListItemVo;
import cn.superdesk.uniorder.dubbo.facade.accounting.supplier.DubboAccountingUnifiedSupplierService;
import cn.superdesk.uniorder.dubbo.facade.sys.notify.DubboSysNotifyService;
import cn.superdesk.uniorder.service.accounting.unified.supplier.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;


/**
 * @Description: 供应商
 * @Author: linjinyu 补充 修改
 * @CreateDate: 4/28/20 5:02 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 4/28/20 5:02 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboAccountingUnifiedSupplierServiceImpl implements DubboAccountingUnifiedSupplierService {


    @Autowired
    private AccountingUnifiedSupplierService                      accountingUnifiedSupplierService;
    @Autowired
    private AccountingUnifiedSupplierFieldAccountingStatusService accountingUnifiedSupplierFieldAccountingStatusService;
    @Autowired
    private AccountingUnifiedSupplierServiceOpCreateDimOrder      accountingUnifiedSupplierServiceOpCreateDimOrder;
    @Autowired
    private AccountingUnifiedSupplierServiceOpCreateDimGoods      accountingUnifiedSupplierServiceOpCreateDimGoods;
    @Autowired
    private AccountingUnifiedSupplierServiceOpEditDimOrder        accountingUnifiedSupplierServiceOpEditDimOrder;
    @Autowired
    private AccountingUnifiedSupplierServiceOpEditDimGoods        accountingUnifiedSupplierServiceOpEditDimGoods;
    @Autowired
    private AccountingUnifiedSupplierServiceOpStatistics          accountingUnifiedSupplierServiceOpStatistics;
    @Autowired
    private AccountingUnifiedSupplierExportExcelService           accountingUnifiedSupplierExportExcelService;

    @Reference(version = "1.0.0", timeout = 10000, retries = -1)
    private DubboSysNotifyService dubboSysNotifyService;

    /**
     * 供应商结算-对账单列表
     *
     * @param params
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public R<List<AccountingOrderUnifiedSupplierListItemVo>> getStatementOfAccountList(Map<String, Object> params, int pageNum, int pageSize) {

        return accountingUnifiedSupplierService.pageQueryAccountingOrderUnifiedSupplierList(params, pageNum, pageSize);
    }

    @Override
    public R<List<AccountingOrderUnifiedSupplierListItemVo>> getStatementOfAccountList(AccountingOrderUnifiedSupplierQueryCriteria queryCriteria) {

        log.info("供应商结算->对账单列表 params {}", JSONUtil.toJsonStr(queryCriteria));
        return accountingUnifiedSupplierService.pageQueryAccountingOrderUnifiedSupplierList(queryCriteria);
    }

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

        return accountingUnifiedSupplierService.getStatementOfAccountHeadSum(params);
    }

    @Override
    public AccountingOrderUnifiedSuplierHeadVo getStatementOfAccountHeadSum(AccountingOrderUnifiedSupplierQueryCriteria queryCriteria) {

        return accountingUnifiedSupplierService.getStatementOfAccountHeadSum(queryCriteria);
    }


    @Override
    public AccountingUnifiedSupplier findById(Long id) {

        return accountingUnifiedSupplierService.findById(id);
    }

    @Override
    public AccountingUnifiedSupplier findByAccountingOrderSn(String accountingOrderSn) {
        return accountingUnifiedSupplierService.findByAccountingOrderSn(accountingOrderSn);
    }

    @Override
    public void updateObj(AccountingUnifiedSupplier update) {

        accountingUnifiedSupplierService.updateByPrimaryKeySelective(update);
    }

    @Override
    public void opEditAccountingDimOrder(AccountingUnifiedSupplier statementOfAccount, UnifiedOrder unifiedOrder, String add, LoginSysUser loginSysUser) {

        accountingUnifiedSupplierServiceOpEditDimOrder.opEditAccountingDimOrder(statementOfAccount, unifiedOrder, add, loginSysUser);
    }

    @Override
    public void opEditAccountingDimGoods(AccountingUnifiedSupplier statementOfAccount, UnifiedOrder unifiedOrder, UnifiedOrderGoods unifiedOrderGoods, String add, LoginSysUser loginSysUser) {
        accountingUnifiedSupplierServiceOpEditDimGoods.opEditAccountingDimGoods(statementOfAccount, unifiedOrder, unifiedOrderGoods, add, loginSysUser);
    }

    /**
     * 供应商结算 > 未对账订单 opt 创建对账单
     *
     * @param unifiedSupplierCreateDimOrderParam
     * @param loginSysUser
     * @return
     */
    @Deprecated
    @Override
    public AccountingUnifiedSupplier opCreateAccountingDimOrder(UnifiedSupplierCreateDimOrderParam unifiedSupplierCreateDimOrderParam, LoginSysUser loginSysUser) {

        return accountingUnifiedSupplierServiceOpCreateDimOrder.opCreateAccountingDimOrderPreProcess(unifiedSupplierCreateDimOrderParam, loginSysUser);
    }


    /**
     * 供应商结算 > 未对账商品 opt 创建对账单 商品维度
     *
     * @param unifiedSupplierCreateDimGoodsParam
     * @param loginSysUser
     * @return
     */
    @Override
    public R<AccountingUnifiedSupplier> opCreateAccountingDimGoods(AccountingUnifiedSupplierOpCreateParamDimGoods unifiedSupplierCreateDimGoodsParam, LoginSysUser loginSysUser) {

        return accountingUnifiedSupplierServiceOpCreateDimGoods.opCreateAccountingDimGoodsPreProcess(unifiedSupplierCreateDimGoodsParam, loginSysUser);
    }

//	@Override
//	public void opRejectAccounting(AccountingUnifiedSupplier statementOfAccount) {
//		accountingUnifiedSupplierService.opRejectAccounting(statementOfAccount);
//	}
//
//	@Override
//	public void opCancelAccounting(AccountingUnifiedSupplier statementOfAccount) {
//		accountingUnifiedSupplierService.opCancelAccounting(statementOfAccount);
//	}
//
//	@Override
//	public void opAgainAccounting(AccountingUnifiedSupplier statementOfAccount) {
//		accountingUnifiedSupplierService.opAgainAccounting(statementOfAccount);
//	}

    @Override
    public int opDelete(AccountingUnifiedSupplier statementOfAccount, LoginSysUser loginSysUser) {
        return accountingUnifiedSupplierFieldAccountingStatusService.opDelete(statementOfAccount, loginSysUser);
    }

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

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

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

    public AccountingOrderUnifiedSupplierStatisticsFixVoDimOrder checkAmountErrorForFixDimOrder(Map<String, Object> params) {
        return accountingUnifiedSupplierServiceOpStatistics.checkAmountErrorForFixDimOrder(params);
    }

    public AccountingOrderUnifiedSupplierStatisticsFixVoDimGoods checkAmountErrorForFixDimGoods(Map<String, Object> params) {
        return accountingUnifiedSupplierServiceOpStatistics.checkAmountErrorForFixDimGoods(params);
    }

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

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

    @Override
    public String exportExcelAccountingSupplierList(AccountingOrderUnifiedSupplierQueryCriteria queryCriteria, LoginSysUser loginSysUser) {

        log.info("\n异步操作 包裹 外层 exportExcelAccountingSupplierList params: \n{}", JSONUtil.toJsonStr(queryCriteria));
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {

            log.info("\n异步操作 包裹 内层 exportExcelAccountingSupplierList params: \n{}", JSONUtil.toJsonStr(queryCriteria));
            List<AccountingOrderUnifiedSupplierListItemVo> list = queryAccountingSupplierAccountList(queryCriteria, loginSysUser);

            SysNotify sysNotify = accountingUnifiedSupplierExportExcelService.exportExcelAccountingSupplierList(list);

            dubboSysNotifyService.notifyExportExcelUnifiedSupplierList(sysNotify, loginSysUser);

        });
        return "导出任务已经接收,正在处理中...";
    }

    private List<AccountingOrderUnifiedSupplierListItemVo> queryAccountingSupplierAccountList(AccountingOrderUnifiedSupplierQueryCriteria queryCriteria, LoginSysUser loginSysUser) {
        return accountingUnifiedSupplierService.queryAccountingOrderUnifiedSupplierList(queryCriteria, loginSysUser);
    }
}
