package cn.superdesk.uniorder.queue.delayed;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.superdesk.uniorder.common.bizScene.invoice.customer.InvoiceApplyCustomerScene;
import cn.superdesk.uniorder.common.enums.common.CommonServiceNoEnum;
import cn.superdesk.uniorder.common.enums.accounting.AccountingOrderTypeDimEnum;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.queue.delayed.DelayQueueManager;
import cn.superdesk.uniorder.common.queue.delayed.DelayTaskAsync;
import cn.superdesk.uniorder.common.queue.delayed.instance.DelayTaskInstanceAccountingOrderInvoiceStatusTailUpdate;
import cn.superdesk.uniorder.common.queue.delayed.instance.DelayTaskInstanceRoleExtData;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dao.entity.sys.role.SysRoleExtData;
import cn.superdesk.uniorder.dao.entity.sys.role.SysRoleExtDataExample;
import cn.superdesk.uniorder.dao.entity.sys.role.SysRoleExtDataSample;
import cn.superdesk.uniorder.dao.entity.sys.role.SysRoleExtDataSampleExample;
import cn.superdesk.uniorder.dao.mapper.sys.role.SysRoleExtDataMapper;
import cn.superdesk.uniorder.dao.mapper.sys.role.SysRoleExtDataSampleMapper;
import cn.superdesk.uniorder.service.invoice.customer.InvoiceApplyCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 延迟队列处理类 生产环境下不好用 存在一定的问题 计划弃用 mark by linjinyu at 20220512
 * 这东西还得再验证一下
 * @Author: linjinyu
 * @CreateDate: 2022/3/2 下午7:33
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/3/2 下午7:33
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Deprecated
@Slf4j
@Component
public class DelayQueueHandler extends DelayQueueManager {

    @Autowired
    private SysRoleExtDataMapper       sysRoleExtDataMapper;
    @Autowired
    private SysRoleExtDataSampleMapper sysRoleExtDataSampleMapper;


    @Autowired
    private InvoiceApplyCustomerService invoiceApplyCustomerService;

    /**
     * 内部执行延时任务 关键字 运营商 渠道商 数据权限
     *
     * @param taskAsync
     */
    public void processTask(DelayTaskAsync taskAsync) {

        log.info("执行延时任务:{}", taskAsync);

        // 根据task中的data自定义数据来处理相关逻辑,例 if (task.getData() instanceof XXX) {} 扩展

        if (taskAsync.getData() instanceof DelayTaskInstanceAccountingOrderInvoiceStatusTailUpdate) {

            DelayTaskInstanceAccountingOrderInvoiceStatusTailUpdate form = (DelayTaskInstanceAccountingOrderInvoiceStatusTailUpdate) taskAsync.getData();

            List<Long>                 affectedAccountingOrderIds = form.getAffectedAccountingOrderIds();
            AccountingOrderTypeDimEnum dim                        = AccountingOrderTypeDimEnum.get(form.getDim());

            Long invoiceApplyId = form.getInvoiceApplyId();

            LoginSysUser loginSysUser = AnJsonUtils.toObject(form.getLoginSysUser(), LoginSysUser.class);


            if (CollUtil.isNotEmpty(affectedAccountingOrderIds)) {
                invoiceApplyCustomerService.processTailUpdateAccountingOrderInvoiceStatus(affectedAccountingOrderIds, dim, loginSysUser);
            }

            if (ObjectUtil.isNotNull(invoiceApplyId)) {

                InvoiceApply invoiceApplyCustomerEntity = invoiceApplyCustomerService.findById(invoiceApplyId);

                if (ObjectUtil.isNotNull(invoiceApplyCustomerEntity)) {

                    if (ArrayUtil.contains(
                            InvoiceApplyCustomerScene.statusV2_1_Array_InvoiceProgressingSceneRevise(),
                            invoiceApplyCustomerEntity.getInvoiceStatus())
                    ) {
                        invoiceApplyCustomerService.patchFixInfo(invoiceApplyCustomerEntity.getId(), loginSysUser);
                        invoiceApplyCustomerService.patchFixStatistics(invoiceApplyCustomerEntity.getId(), loginSysUser);
                    }
                }
            }


        } else if (taskAsync.getData() instanceof DelayTaskInstanceRoleExtData) {

            Long outChannelId = Long.valueOf(taskAsync.getData().getIdentifier());

            SysRoleExtDataExample sysRoleExtDataExample = new SysRoleExtDataExample();
            sysRoleExtDataExample
                    .createCriteria()
                    .andSourceFromQuoteIdEqualTo(outChannelId);

            List<SysRoleExtData> sysRoleExtDataList = sysRoleExtDataMapper.selectByExample(sysRoleExtDataExample);

            SysRoleExtDataSampleExample sysRoleExtDataSampleExample = new SysRoleExtDataSampleExample();
            sysRoleExtDataSampleExample.createCriteria()
                    .andSourceFromQuoteParentIdEqualTo(outChannelId);

            List<SysRoleExtDataSample> sysRoleExtDataSampleList = sysRoleExtDataSampleMapper.selectByExample(sysRoleExtDataSampleExample);

            // 其中一个为空就没意义
            if (CollUtil.isNotEmpty(sysRoleExtDataList) && CollUtil.isNotEmpty(sysRoleExtDataSampleList)) {

                List<Long>   sysRoleExtSampleIds             = sysRoleExtDataSampleList.stream().map(SysRoleExtDataSample::getId).collect(Collectors.toList());
                List<String> sysRoleExtSampleServiceNoMult   = sysRoleExtDataSampleList.stream().map(SysRoleExtDataSample::getServiceNo).distinct().collect(Collectors.toList());
                List<String> sysRoleExtSampleServiceNameMult = new ArrayList<>();

                sysRoleExtSampleServiceNoMult.forEach(item -> {
                    sysRoleExtSampleServiceNameMult.add(CommonServiceNoEnum.getByServiceNo(item).getServiceName());
                });

                SysRoleExtData forUpdate = sysRoleExtDataList.get(0);
                forUpdate.setSysRoleExtSampleIds(StringUtils.join(sysRoleExtSampleIds.toArray(), ","));
                // 这个是有问题 TODO 20200921 by linjinyu
                forUpdate.setServiceNoMult(StringUtils.join(sysRoleExtSampleServiceNoMult.toArray(), ","));
                forUpdate.setServiceNameMult(StringUtils.join(sysRoleExtSampleServiceNameMult.toArray(), ","));

                sysRoleExtDataMapper.updateByPrimaryKeySelective(forUpdate);

            } else {
                log.error("\n\n\n\n任务-{}\n数据权限角色 : {}\n数据权限许可 : {}\n\n\n\n", taskAsync, AnJsonUtils.toJson(sysRoleExtDataList), AnJsonUtils.toJson(sysRoleExtDataSampleList));
            }


        }

        // ...
    }
}
