package cn.iocoder.yudao.module.pms.service.maintain.material;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.pms.controller.admin.maintain.material.vo.IotMaintainMaterialPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintain.material.vo.IotMaintainMaterialSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.maintain.material.IotMaintainMaterialDO;
import cn.iocoder.yudao.module.pms.dal.mysql.maintain.material.IotMaintainMaterialMapper;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


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

/**
 * 工单物料 Service 实现类
 *
 * @author 李鹏辉
 */
@Service
@Validated
public class IotMaintainMaterialServiceImpl implements IotMaintainMaterialService {

    @Resource
    private IotMaintainMaterialMapper iotMaintainMaterialMapper;
    @Autowired
    private DeptService deptService;

    @Override
    public List<IotMaintainMaterialDO> getListByMaintainId(Long maintainId) {
        return iotMaintainMaterialMapper.selectList("maintain_id", maintainId);
    }

    @Override
    public Long createIotMaintainMaterial(IotMaintainMaterialSaveReqVO createReqVO) {
        // 插入
        IotMaintainMaterialDO iotMaintainMaterial = BeanUtils.toBean(createReqVO, IotMaintainMaterialDO.class);
        iotMaintainMaterialMapper.insert(iotMaintainMaterial);
        // 返回
        return iotMaintainMaterial.getId();
    }

    @Override
    public void updateIotMaintainMaterial(IotMaintainMaterialSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMaintainMaterialExists(updateReqVO.getId());
        // 更新
        IotMaintainMaterialDO updateObj = BeanUtils.toBean(updateReqVO, IotMaintainMaterialDO.class);
        iotMaintainMaterialMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotMaintainMaterial(Long id) {
        // 校验存在
        validateIotMaintainMaterialExists(id);
        // 删除
        iotMaintainMaterialMapper.deleteById(id);
    }

    private void validateIotMaintainMaterialExists(Long id) {
        if (iotMaintainMaterialMapper.selectById(id) == null) {
            throw exception(IOT_MAINTAIN_MATERIAL_NOT_EXISTS);
        }
    }

    @Override
    public IotMaintainMaterialDO getIotMaintainMaterial(Long id) {
        return iotMaintainMaterialMapper.selectById(id);
    }

    @Override
    public PageResult<IotMaintainMaterialDO> getIotMaintainMaterials(IotMaintainMaterialPageReqVO pageReqVO) {
        DeptDO dept = deptService.getDept(pageReqVO.getDeptId());
        if (Objects.isNull(dept)) {throw new ServiceException();}
        IPage<IotMaintainMaterialDO> iotMaintainMaterialDOIPage = iotMaintainMaterialMapper.selectMaterialsByDept(Page.of(pageReqVO.getPageNo(), pageReqVO.getPageSize()), pageReqVO.getDeptId(), dept.getParentId());
        return new PageResult<>(iotMaintainMaterialDOIPage.getRecords(), iotMaintainMaterialDOIPage.getTotal());
    }

    @Override
    public PageResult<IotMaintainMaterialDO> getIotMaintainMaterialPage(IotMaintainMaterialPageReqVO pageReqVO) {
        return iotMaintainMaterialMapper.selectPage(pageReqVO);
    }

}
