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

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.hazard.vo.IotHazardPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.hazard.vo.IotHazardRectifyVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.hazard.vo.IotHazardSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.hazard.IotHazardDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.hazard.IotHazardTypeDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.hazard.IotHazardMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.hazard.IotHazardTypeMapper;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import org.apache.commons.lang3.StringUtils;
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.HashSet;
import java.util.Objects;
import java.util.Set;

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

/**
 * QHSE隐患排查及整改 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotHazardServiceImpl implements IotHazardService {

    @Resource
    private IotHazardMapper iotHazardMapper;
    @Autowired
    private DeptService deptService;
    @Autowired
    private IotHazardTypeMapper iotHazardTypeMapper;

    @Override
    public Long createIotHazard(IotHazardSaveReqVO createReqVO) {
        // 插入
        IotHazardDO iotHazard = BeanUtils.toBean(createReqVO, IotHazardDO.class);
        iotHazard.setDeleted(false);
        //未整改
        iotHazard.setStatus("todo");
        Long deptId;
        if (createReqVO.getDeptId()==null) {
            deptId = SecurityFrameworkUtils.getLoginUserDeptId();
        } else {
            deptId = createReqVO.getDeptId();
        }
        iotHazard.setDeptId(deptId);
        if (Objects.nonNull(createReqVO.getClassify())&& StringUtils.isNotBlank(createReqVO.getClassify())) {
            IotHazardTypeDO iotHazardTypeDO = iotHazardTypeMapper.selectById(Long.valueOf(createReqVO.getClassify()));
            if (Objects.isNull(iotHazardTypeDO)) {
                throw new ServiceException(new ErrorCode(2,"不存在隐患排查分类"));
            }
            iotHazard.setClassifyName(iotHazardTypeDO.getName());
        }
        iotHazardMapper.insert(iotHazard);
        // 返回
        return iotHazard.getId();
    }

    @Override
    public void updateIotHazard(IotHazardSaveReqVO updateReqVO) {
        Long deptId;
        if (updateReqVO.getDeptId()==null) {
            deptId = SecurityFrameworkUtils.getLoginUserDeptId();
        } else {
            deptId = updateReqVO.getDeptId();
        }
        updateReqVO.setDeptId(deptId);
        // 校验存在
        validateIotHazardExists(updateReqVO.getId());
        // 更新
        IotHazardDO updateObj = BeanUtils.toBean(updateReqVO, IotHazardDO.class);
        if (Objects.nonNull(updateObj.getClassify())&& StringUtils.isNotBlank(updateObj.getClassify())) {
            IotHazardTypeDO iotHazardTypeDO = iotHazardTypeMapper.selectById(Long.valueOf(updateObj.getClassify()));
            if (Objects.isNull(iotHazardTypeDO)) {
                throw new ServiceException(new ErrorCode(2,"不存在隐患排查分类"));
            }
            updateObj.setClassifyName(iotHazardTypeDO.getName());
        }
        iotHazardMapper.updateById(updateObj);
    }

    @Override
    public void rectifyHazard(IotHazardRectifyVO updateReqVO) {
        if (Objects.isNull(updateReqVO.getId())) throw new ServiceException(new ErrorCode(2,"不存在"));
        IotHazardDO iotHazardDO = iotHazardMapper.selectById(updateReqVO.getId());
        iotHazardDO.setStatus("finished");
        iotHazardDO.setRectifyDesc(updateReqVO.getRectifyDesc());
        iotHazardDO.setRectifyFile(updateReqVO.getRectifyFile());
        iotHazardMapper.updateById(iotHazardDO);
    }

    @Override
    public void deleteIotHazard(Long id) {
        // 校验存在
        validateIotHazardExists(id);
        // 删除
        iotHazardMapper.deleteById(id);
    }

    private void validateIotHazardExists(Long id) {
        if (iotHazardMapper.selectById(id) == null) {
            throw exception(new ErrorCode(22,"不存在"));
        }
    }

    @Override
    public IotHazardDO getIotHazard(Long id) {
        return iotHazardMapper.selectById(id);
    }

    @Override
    public PageResult<IotHazardDO> getIotHazardPage(IotHazardPageReqVO pageReqVO) {
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeptId())) {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
        }
        return iotHazardMapper.selectPage(pageReqVO, ids);
    }

}