package com.zfire.jiasm.syncdata.download.order;


import lombok.extern.slf4j.Slf4j;

/**
 * 格力品牌安装工单下载后落本地数据表
 * 3：落业务表  TL_SynDataFrom_tbl_az_assign_lc_ls_batch
 *:4：修改下载主表处理结果
 */
@Slf4j
// @Component
public class InstallBusiness {
    /*@Value("${installinc}")
    private String installinc = "";

    @Resource
    ItfTblAzAssignLcLsDownloadService itfTblAzAssignLcLsDownloadService;
    @Resource
    BaseService baseService;
    @Resource
    OrderBaseService orderBaseService;
    @Resource
    SysDictRefService sysDictRefService;
    @Resource
    AddInstallBusiness addInstallBusiness;




    // 3：工单落入本地表
    @Scheduled(fixedDelayString = "${installin}")
    public void inputOrderInstall() {
        if (installinc.equals(TaskConfigEnum.FALSE.getCode())){
            return;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<ItfTblAzAssignLcLsDownload> lcls = new ArrayList<>();
        List<String> lclslist = new ArrayList<>();
        List<OrderBase> orderBasesRecs = new ArrayList<>();
        List<String> orderBaselist = new ArrayList<>();
        Map<String, List<SysDictRef>> sysDictRefDLMap = new HashMap<>();
        Map<String, List<SysDictRef>> sysDictRefXLMap = new HashMap<>();
        Map<String, List<SysDictRef>> sysDictRefXiLieMap = new HashMap<>();

        // 提取品牌信息
        Brand brand = baseService.getBrand();
        if (brand == null) {
            return;
        }
        // 提取品牌商对应的大类信息
        sysDictRefDLMap = getStringListMap(brand);
        // 提取品牌商对应的小类信息
        sysDictRefXLMap = getListMap(brand);
        // 提取品牌商对应的系列信息
        sysDictRefXiLieMap = getMap(brand);

        // 提取待落本地表的数据
        lcls = itfTblAzAssignLcLsDownloadService.lambdaQuery()
                .eq(ItfTblAzAssignLcLsDownload::getSynStatus, SynStatusEnum.UN_SYN.getCode())
                .orderByAsc(ItfTblAzAssignLcLsDownload::getSynReqId)
                .list();

        if (lcls.size()<1){
            log.info("暂无待处理的安装工单: \uF0B7{}",sdf.format(new Date()));
            return;
        }

        log.info("安装工单落本地表开始处理: \uF0B7{}",sdf.format(new Date()));

        lclslist = lcls.stream().map(ItfTblAzAssignLcLsDownload::getPgguid).collect(Collectors.toList());
        // 提取已有的订单数据
        orderBasesRecs = orderBaseService.lambdaQuery()
                .select(OrderBase::getPgguid)
                .in(OrderBase::getPgguid,lclslist).list();
        orderBaselist = orderBasesRecs.stream().map(OrderBase::getPgguid).collect(Collectors.toList());

        // 按任务号download_task_no，按单来处理
        for (ItfTblAzAssignLcLsDownload item: lcls) {
            try{
                // 向工单子表下载任务表插入数据
                downInformation(item);

                if (orderBaselist.contains(item.getPgguid())){
                    // 修改 TL_SynDataFrom_tbl_az_assign_lc_ls_batch
                    addInstallBusiness.UpdateOrder(sysDictRefDLMap, sysDictRefXLMap, sysDictRefXiLieMap, brand, item);
                } else {
                    // 新增 TL_SynDataFrom_tbl_az_assign_lc_ls_batch
                    addInstallBusiness.addOrder(sysDictRefDLMap, sysDictRefXLMap, sysDictRefXiLieMap, brand, item);
                    orderBaselist.add(item.getPgguid());
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                log.info("安装工单落本地表失败 销售单号-派工单号：【" + item.getXsdh() + "-"+ item.getPgid()+"】");
                item.setSynErrMsg("落本地表失败");
                if (StringUtils.isNotEmpty(ex.getMessage())){
                    if (ex.getMessage().length()>1020){
                        item.setSynErrMsg(ex.getMessage().substring(0,1020));
                    } else {
                        item.setSynErrMsg(ex.getMessage());
                    }
                }
                itfTblAzAssignLcLsDownloadService.updateById(item);
            }
        }

        log.info("安装工单落本地表完成: \uF0B7{}",sdf.format(new Date()));
    }

    private Map<String, List<SysDictRef>> getListMap(Brand brand) {
        List<SysDictRef> sysDictxlRef;
        Map<String, List<SysDictRef>> sysDictRefXLMap;
        sysDictxlRef =  sysDictRefService.lambdaQuery()
                .select(SysDictRef::getDictCode,SysDictRef::getDictValue,SysDictRef::getRefDictCode)
                .eq(SysDictRef::getBrandId, brand.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_SMALL_TYPE.getCode())
                .list();
        sysDictRefXLMap = sysDictxlRef.stream().
                collect(Collectors.groupingBy(SysDictRef::getRefDictCode));
        return sysDictRefXLMap;
    }

    private Map<String, List<SysDictRef>> getStringListMap(Brand brand) {
        List<SysDictRef> sysDictRef;
        Map<String, List<SysDictRef>> sysDictRefDLMap;
        sysDictRef =  sysDictRefService.lambdaQuery()
                .select(SysDictRef::getDictCode,SysDictRef::getDictValue,SysDictRef::getRefDictCode)
                .eq(SysDictRef::getBrandId, brand.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_MAIN_TYPE.getCode())
                .list();
        sysDictRefDLMap = sysDictRef.stream().
                collect(Collectors.groupingBy(SysDictRef::getRefDictCode));
        return sysDictRefDLMap;
    }

    private void downInformation(ItfTblAzAssignLcLsDownload item) {
        ItfGreeToFaDownloadTaskList tl = new ItfGreeToFaDownloadTaskList();
        tl.setDownloadTaskNo(IdWorker.getIdStr());
        tl.setDownloadKeyValue(item.getPgguid());
        tl.setDownloadTaskType(2206);
        tl.setDownloadStatus(0);
        tl.insert();
    }
    private Map<String, List<SysDictRef>> getMap(Brand brand) {
        List<SysDictRef> sysDictxilieRef;
        Map<String, List<SysDictRef>> sysDictRefXiLieMap;
        sysDictxilieRef =  sysDictRefService.lambdaQuery()
                .select(SysDictRef::getDictCode,SysDictRef::getDictValue,SysDictRef::getRefDictCode)
                .eq(SysDictRef::getBrandId, brand.getBrandId())
                .eq(SysDictRef::getDictType, DictTypeEnum.DICT_TYPE_SERIES.getCode())
                .list();
        sysDictRefXiLieMap = sysDictxilieRef.stream().
                collect(Collectors.groupingBy(SysDictRef::getRefDictCode));
        return sysDictRefXiLieMap;
    }*/
}
