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

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.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.danger.vo.IotDangerSourcePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.danger.vo.IotDangerSourceSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.stat.DeptUtil;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.danger.IotDangerSourceDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.danger.IotDangerSourceMapper;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
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;

/**
 * 危险源 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotDangerSourceServiceImpl implements IotDangerSourceService {

    @Resource
    private IotDangerSourceMapper iotDangerSourceMapper;
    @Autowired
    private DeptService deptService;
    @Autowired
    private DeptUtil deptUtil;

    @Override
    public Long createIotDangerSource(IotDangerSourceSaveReqVO createReqVO) {
        // 插入
        IotDangerSourceDO iotDangerSource = BeanUtils.toBean(createReqVO, IotDangerSourceDO.class);
        iotDangerSource.setDeleted(false);
        iotDangerSourceMapper.insert(iotDangerSource);
        // 返回
        return iotDangerSource.getId();
    }

    @Override
    public void updateIotDangerSource(IotDangerSourceSaveReqVO updateReqVO) {
        // 校验存在
        validateIotDangerSourceExists(updateReqVO.getId());
        // 更新
        IotDangerSourceDO updateObj = BeanUtils.toBean(updateReqVO, IotDangerSourceDO.class);
        iotDangerSourceMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotDangerSource(Long id) {
        // 校验存在
        validateIotDangerSourceExists(id);
        // 删除
        iotDangerSourceMapper.deleteById(id);
    }

    private void validateIotDangerSourceExists(Long id) {
        if (iotDangerSourceMapper.selectById(id) == null) {
            throw exception(new ErrorCode(222,"不存在"));
        }
    }

    @Override
    public IotDangerSourceDO getIotDangerSource(Long id) {
        return iotDangerSourceMapper.selectById(id);
    }

    @Override
    public PageResult<IotDangerSourceDO> getIotDangerSourcePage(IotDangerSourcePageReqVO pageReqVO) {
        Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
        String companyCode = deptUtil.getCompanyCode(loginUserDeptId);
        Set<Long> ids = new HashSet<>();
        if ("rh".equals(companyCode)) {
            ids.add(157L);
        } else if ("ry".equals(companyCode)) {
            ids.add(158L);
        } else if ("rd".equals(companyCode)) {
            ids.add(163L);
        } else {
            if (Objects.nonNull(pageReqVO.getDeptId())) {
                ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
                ids.add(pageReqVO.getDeptId());
            }
        }
        return iotDangerSourceMapper.selectPage(pageReqVO, ids);
    }

}