package com.ruoyi.ss.model.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.model.domain.ModelQuery;
import com.ruoyi.ss.model.domain.ModelVO;
import com.ruoyi.ss.model.mapper.ModelMapper;
import com.ruoyi.ss.model.service.IModelValidator;
import com.ruoyi.ss.model.domain.ModelBO;
import com.ruoyi.ss.model.service.IModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 型号列表Service业务层处理
 *
 * @author 邱贞招
 * @date 2024-02-21
 */
@Service
public class ModelServiceImpl implements IModelService
{
    @Autowired
    private ModelMapper modelMapper;

//    @Autowired
//    private IEDeviceService deviceService;

    @Autowired
    private IModelValidator modelValidator;

    /**
     * 查询型号列表
     *
     * @param modelId 型号列表主键
     * @return 型号列表
     */
    @Override
    public ModelVO selectSmModelByModelId(Long modelId)
    {
        return modelMapper.selectSmModelByModelId(modelId);
    }

    /**
     * 查询型号列表列表
     *
     * @param smModel 型号列表
     * @return 型号列表
     */
    @Override
    public List<ModelVO> selectSmModelList(ModelQuery smModel)
    {
        return modelMapper.selectSmModelList(smModel);
    }

    /**
     * 新增型号列表
     *
     * @param data 型号列表
     * @return 结果
     */
    @Override
    public int insertSmModel(ModelBO data) {
        ServiceUtil.assertion(data == null, "参数不能为空");
        ServiceUtil.assertion(CollectionUtils.isEmptyElement(data.getTags()) && !modelValidator.hasTags(data.getTags()), "非法的标签");
        ServiceUtil.assertion(modelValidator.isRepeatModel(data.getModelId(), data.getModel()), "型号重复");
        data.setCreateTime(DateUtils.getNowDate());
        return modelMapper.insertSmModel(data);
    }

    /**
     * 修改型号
     *
     * @param data 型号
     * @return 结果
     */
    @Override
    public int updateSmModel(ModelBO data) {
        ServiceUtil.assertion(data == null, "参数不能为空");
        ServiceUtil.assertion(CollectionUtils.isEmptyElement(data.getTags()) && !modelValidator.hasTags(data.getTags()), "非法的标签");
        ServiceUtil.assertion(modelValidator.isRepeatModel(data.getModelId(), data.getModel()), "型号重复");
        data.setUpdateTime(DateUtils.getNowDate());
        return modelMapper.updateSmModel(data);
    }

    /**
     * 批量删除型号
     *
     * @param modelIds 需要删除的型号列表主键
     * @return 结果
     */
    @Override
    public int deleteSmModelByModelIds(Long[] modelIds)
    {
        return modelMapper.deleteSmModelByModelIds(modelIds);
    }

    /**
     * 删除型号列表信息
     *
     * @param modelId 型号列表主键
     * @return 结果
     */
    @Override
    public int deleteSmModelByModelId(Long modelId)
    {
        return modelMapper.deleteSmModelByModelId(modelId);
    }

    @Override
    public Integer selectCount(ModelQuery dto) {
        return modelMapper.selectCount(dto);
    }

    @Override
    public int logicDelete(List<Long> modelIds) {
        this.validatePreLogicDelete(modelIds);
        return modelMapper.logicDeleteByIds(modelIds);
    }

    @Override
    public ModelVO selectByModel(String model) {
        if (!StringUtils.hasText(model)) {
            return null;
        }
        ModelQuery query = new ModelQuery();
        query.setModel(model);
        List<ModelVO> list = modelMapper.selectSmModelList(query);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

//    @Override
//    public int addInitModel(IotDeviceInfo deviceInfo) {
//        if (deviceInfo == null) {
//            return 0;
//        }
//        EModel data = new EModel();
//        data.setModelName("未命名");
//        data.setModel(deviceInfo.getModel());
//        return modelMapper.insertSmModel(data);
//    }

    @Override
    public ModelVO selectOne(ModelQuery query) {
        PageHelper.startPage(1,1);
        List<ModelVO> list = this.selectSmModelList(query);
        if (CollectionUtils.isEmptyElement(list)) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 逻辑删除前校验
     */
    private void validatePreLogicDelete(List<Long> modelIds) {
        ServiceUtil.assertion(CollectionUtils.isEmpty(modelIds), "型号列表不允许为空");

        Map<Long, ModelVO> modelMap = this.selectMapByModelIds(modelIds);
        for (Long modelId : modelIds) {
            ModelVO model = modelMap.get(modelId);
            ServiceUtil.assertion(model == null || model.getDeleted(), String.format("型号%s不存在", modelId));
        }

//        Map<Long, List<DeviceVO>> deviceGroup = deviceService.selectGroupByModelIds(modelIds);
//        for (Long modelId : modelIds) {
//            List<DeviceVO> deviceList = deviceGroup.get(modelId);
//            ServiceUtil.assertion(!CollectionUtils.isEmpty(deviceList), String.format("型号%s下存在设备，无法删除", modelId));
//        }
    }

    /**
     * 根据型号id列表查询数据，并通过map映射
     */
    private Map<Long, ModelVO> selectMapByModelIds(List<Long> modelIds) {
        if (CollectionUtils.isEmpty(modelIds)) {
            return Collections.emptyMap();
        }
        ModelQuery dto = new ModelQuery();
        dto.setModelIds(modelIds);
        List<ModelVO> list = modelMapper.selectSmModelList(dto);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        return list.stream().collect(Collectors.toMap(ModelVO::getModelId, item -> item));
    }

}
