package cn.iocoder.yudao.module.pms.service.iotmaterial;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterial.vo.IotMaterialPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterial.vo.IotMaterialSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmaterial.IotMaterialDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotmaterial.IotMaterialMapper;
import cn.iocoder.yudao.module.pms.service.iotmaterialgroup.IotMaterialGroupService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_MATERIAL_NOT_EXISTS;


/**
 * PMS 功能优化 物料 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class IotMaterialServiceImpl implements IotMaterialService {

    @Resource
    private IotMaterialMapper iotMaterialMapper;
    @Resource
    private IotMaterialGroupService iotMaterialGroupService;

    @Override
    public Long createIotMaterial(IotMaterialSaveReqVO createReqVO) {
        // 插入
        IotMaterialDO iotMaterial = BeanUtils.toBean(createReqVO, IotMaterialDO.class);
        iotMaterialMapper.insert(iotMaterial);
        // 返回
        return iotMaterial.getId();
    }

    @Override
    public void updateIotMaterial(IotMaterialSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMaterialExists(updateReqVO.getId());
        // 更新
        IotMaterialDO updateObj = BeanUtils.toBean(updateReqVO, IotMaterialDO.class);
        iotMaterialMapper.updateById(updateObj);
    }

    @Override
    public void updateMaterialStatus(Long id, Integer status) {
        // 校验 物料 存在
        validateIotMaterialExists(id);
        // 更新状态
        IotMaterialDO updateObj = new IotMaterialDO();
        updateObj.setId(id);
        updateObj.setStatus(status);
        iotMaterialMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotMaterial(Long id) {
        // 校验存在
        validateIotMaterialExists(id);
        // 删除
        iotMaterialMapper.deleteById(id);
    }

    private void validateIotMaterialExists(Long id) {
        if (iotMaterialMapper.selectById(id) == null) {
            throw exception(IOT_MATERIAL_NOT_EXISTS);
        }
    }

    @Override
    public IotMaterialDO getIotMaterial(Long id) {
        return iotMaterialMapper.selectById(id);
    }

    @Override
    public PageResult<IotMaterialDO> getIotMaterialPage(IotMaterialPageReqVO pageReqVO) {
        // 查询当前物料组分类及子物料组分类的所有物料
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getMaterialGroupId())) {
            ids = iotMaterialGroupService.getChildMaterialGroupList(pageReqVO.getMaterialGroupId());
            ids.add(pageReqVO.getMaterialGroupId());
        }
        return iotMaterialMapper.selectPage(pageReqVO, ids);
    }

    @Override
    public List<IotMaterialDO> getIotMaterials(IotMaterialPageReqVO reqVO) {
        return iotMaterialMapper.selectList(reqVO, null);
    }

    @Override
    public List<IotMaterialDO> getMaterialListByStatus(Integer status) {
        return iotMaterialMapper.selectListByStatus(status);
    }

    @Override
    public PageResult<IotMaterialDO> getMaterialsByBomId(IotMaterialPageReqVO reqVO) {
        return iotMaterialMapper.getMaterialsByBomId(reqVO);
    }

    @Override
    public PageResult<IotMaterialDO> getMaterialsByDeviceBomId(IotMaterialPageReqVO reqVO) {
        return iotMaterialMapper.getMaterialsByDeviceBomId(reqVO);
    }

    @Override
    public Long theMaxId() {
        List<IotMaterialDO> materials = iotMaterialMapper.theMaxOne();
        return CollUtil.isNotEmpty(materials) ? materials.get(0).getId() : 0l;
    }

}