package cn.iocoder.yudao.module.pms.service.qhse.safe;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
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.qhse.safe.vo.QhseSafeDayPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.safe.vo.QhseSafeDaySaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.safe.QhseSafeDayDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.safe.QhseSafeDayMapper;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * QHSE_安全生产天数 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class QhseSafeDayServiceImpl implements QhseSafeDayService {

    @Resource
    private QhseSafeDayMapper qhseSafeDayMapper;
    @Autowired
    private DeptApi deptApi;

    @Override
    public Long createQhseSafeDay(QhseSafeDaySaveReqVO createReqVO) {
        // 插入
        QhseSafeDayDO qhseSafeDay = BeanUtils.toBean(createReqVO, QhseSafeDayDO.class);
        qhseSafeDay.setDeleted(false);
        qhseSafeDayMapper.insert(qhseSafeDay);
        // 返回
        return qhseSafeDay.getId();
    }

    @Override
    public void updateQhseSafeDay(QhseSafeDaySaveReqVO updateReqVO) {
        // 校验存在
        validateQhseSafeDayExists(updateReqVO.getId());
        // 更新
        QhseSafeDayDO updateObj = BeanUtils.toBean(updateReqVO, QhseSafeDayDO.class);
        qhseSafeDayMapper.updateById(updateObj);
    }

    @Override
    public void deleteQhseSafeDay(Long id) {
        // 校验存在
        validateQhseSafeDayExists(id);
        // 删除
        qhseSafeDayMapper.deleteById(id);
    }

    private void validateQhseSafeDayExists(Long id) {
        if (qhseSafeDayMapper.selectById(id) == null) {
            throw exception(new ErrorCode(1,"不存在"));
        }
    }

    @Override
    public QhseSafeDayDO getQhseSafeDay(Long id) {
        return qhseSafeDayMapper.selectById(id);
    }

    @Override
    public PageResult<QhseSafeDayDO> getQhseSafeDayPage(QhseSafeDayPageReqVO pageReqVO) {
        return qhseSafeDayMapper.selectPage(pageReqVO);
    }

    @Override
    public List<QhseSafeDayDO> getDayByDept(Long deptId) {
        List<Long> collect = deptApi.getChildDeptList(deptId).stream().map(DeptRespDTO::getId).collect(Collectors.toList());
        QhseSafeDayPageReqVO qhseSafeDayPageReqVO = new QhseSafeDayPageReqVO();
        List<QhseSafeDayDO> qhseSafeDayDOS = qhseSafeDayMapper.selectListByDept(qhseSafeDayPageReqVO, collect);
        return qhseSafeDayDOS;
    }

    @Override
    public List<QhseSafeDayDO> getByName(String name) {
        return qhseSafeDayMapper.selectList("dept_name", name);
    }

}