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

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.skr.mdm.annotation.CmdHandler;
import com.skr.mdm.constant.CmdTypeConstants;
import com.skr.mdm.dao.DeviceEquipmentDao;
import com.skr.mdm.dao.DeviceEquipmentInfoDao;
import com.skr.mdm.dto.EquipmentInfoDTO;
import com.skr.mdm.dto.LoginUserDTO;
import com.skr.mdm.entity.DeviceEquipmentDO;
import com.skr.mdm.entity.DeviceEquipmentInfoDO;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.service.CmdHandlerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author nfq
 * @date 2020/7/30 17:08
 */
@Slf4j
@CmdHandler(cmdType = CmdTypeConstants.DEV_INFO)
public class EquipmentInfoServiceImpl implements CmdHandlerService {

    private static final String SPLIT = "-";

    @Autowired
    private DeviceEquipmentDao equipmentDao;
    @Autowired
    private DeviceEquipmentInfoDao equipmentInfoDao;

    @Override
    public ResponseResult cmdHandler(Integer equipmentId, String data) {
        try {
            LoginUserDTO loginUserDTO = (LoginUserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            EquipmentInfoDTO equipmentInfoDTO = JSONUtil.toBean(data, EquipmentInfoDTO.class);
            DeviceEquipmentInfoDO equipmentInfoDO = new DeviceEquipmentInfoDO();
            BeanUtils.copyProperties(equipmentInfoDTO, equipmentInfoDO);
            equipmentInfoDO.setEquipmentId(equipmentId);
            if (StringUtils.isNotBlank(equipmentInfoDTO.getEquipmentModel()) || StringUtils.isNotBlank(equipmentInfoDTO.getPhoneNumber())) {
                new LambdaUpdateChainWrapper<>(equipmentDao)
                        .set(StringUtils.isNotBlank(equipmentInfoDTO.getEquipmentModel()), DeviceEquipmentDO::getEquipmentModel, equipmentInfoDTO.getEquipmentModel())
                        .set(StringUtils.isNotBlank(equipmentInfoDTO.getPhoneNumber()), DeviceEquipmentDO::getPhoneNumber, equipmentInfoDTO.getPhoneNumber())
                        .eq(DeviceEquipmentDO::getId, equipmentId)
                        .update();
            }
            equipmentInfoDO.setImei2(getImei2(equipmentInfoDO.getImei2(), equipmentId, loginUserDTO.getImei()));
            DeviceEquipmentInfoDO deviceEquipmentInfoDO = new LambdaQueryChainWrapper<>(equipmentInfoDao)
                    .eq(DeviceEquipmentInfoDO::getEquipmentId, equipmentId)
                    .one();
            if (deviceEquipmentInfoDO == null) {
                // 添加设备信息
                equipmentInfoDao.insert(equipmentInfoDO);
                new LambdaUpdateChainWrapper<>(equipmentDao)
                        .set(DeviceEquipmentDO::getOaid, equipmentInfoDTO.getOaid())
                        .set(DeviceEquipmentDO::getUuid, equipmentInfoDTO.getUuid())
                        .eq(DeviceEquipmentDO::getId, equipmentId)
                        .update();
                return ResponseResult.success();
            }
            // 修改设备信息
            equipmentInfoDO.setId(deviceEquipmentInfoDO.getId());
            equipmentInfoDao.updateById(equipmentInfoDO);
            return ResponseResult.success();
        } catch (Exception e) {
            log.error("上传设备信息异常: " + equipmentId + data, e);
            return ResponseResult.failure();
        }
    }

    /**
     * 获取IMEI2
     *
     * @param imei
     * @param equipmentId
     * @return
     */
    private String getImei2(String imei, Integer equipmentId, String imeiLogin) {
        if (StringUtils.isNotBlank(imei)) {
            if (imei.contains(SPLIT)) {
                imei+="";
                String[] split = StringUtils.split(imei, SPLIT);
                String imeiFirst = split[0];
                String imeiEnd = split[1];
                if (StringUtils.isBlank(imeiLogin)) {
                    // 同步IMEI1
                    new LambdaUpdateChainWrapper<>(equipmentDao)
                            .set(DeviceEquipmentDO::getImei, imeiFirst)
                            .eq(DeviceEquipmentDO::getId, equipmentId)
                            .update();
                    return imeiEnd;
                }
                if (imeiLogin.equals(imeiFirst)) {
                    return imeiEnd;
                }
                return imeiFirst;
            }
        }
        return null;
    }
}