package com.skr.mdm.service.impl.app;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.skr.mdm.annotation.CmdHandler;
import com.skr.mdm.constant.CmdTypeConstants;
import com.skr.mdm.dao.AppInstallDao;
import com.skr.mdm.dto.AppInstallDTO;
import com.skr.mdm.entity.AppInstallDO;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.service.CmdHandlerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author nfq
 * @date 2020/8/3 10:09
 */
@Slf4j
@CmdHandler(cmdType = CmdTypeConstants.APP_INSTALL_INFO)
public class AppInstallInfoServiceImpl implements CmdHandlerService {

    @Autowired
    private AppInstallDao appInstallDao;

    @Override
    public ResponseResult cmdHandler(Integer equipmentId, String data) {
        try {
            JSONArray jsonArray = JSONUtil.parseArray(data);
            List<AppInstallDTO> appInstallDTOList = JSONUtil.toList(jsonArray, AppInstallDTO.class);
            List<Integer> cidList = new ArrayList<>(appInstallDTOList.size());
            if (CollectionUtils.isNotEmpty(appInstallDTOList)) {
                appInstallDao.delete(Wrappers.lambdaQuery(AppInstallDO.class).eq(AppInstallDO::getEquipmentId, equipmentId));
//                Set<String> packageNameSet = appInstallDTOList.stream().map(AppInstallDTO::getPackageName).collect(Collectors.toSet());
//                // 包名查重
//                List<AppInstallDO> list = new LambdaQueryChainWrapper<>(appInstallDao)
//                        .eq(AppInstallDO::getEquipmentId, equipmentId)
//                        .in(AppInstallDO::getPackageName, packageNameSet)
//                        .list();
                List<AppInstallDO> appInstallDOList = new ArrayList<>(appInstallDTOList.size());
                for (AppInstallDTO appInstallDTO : appInstallDTOList) {
//                    if (CollectionUtils.isNotEmpty(list)) {
//                        List<String> packageNameList = list.stream().map(AppInstallDO::getPackageName).collect(Collectors.toList());
//                        if (packageNameList.contains(appInstallDTO.getPackageName())) {
//                            continue;
//                        }
//                    }
                    if (StringUtils.isNotBlank(appInstallDTO.getAppName()) && appInstallDTO.getAppName().length() < 50) {
                        AppInstallDO appInstallDO = new AppInstallDO();
                        BeanUtils.copyProperties(appInstallDTO, appInstallDO);
                        appInstallDO.setEquipmentId(equipmentId);
                        appInstallDOList.add(appInstallDO);
                    }
                    cidList.add(appInstallDTO.getCid());
                }
                if (CollectionUtils.isNotEmpty(appInstallDOList)) {
                    appInstallDao.insertBatch(appInstallDOList);
                }
            }
            return ResponseResult.success(JSONUtil.toJsonStr(cidList));
        } catch (Exception e) {
            log.error("客户端应用安装信息上传异常：" + equipmentId + data, e);
            return ResponseResult.failure();
        }
    }
}
