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

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.environment.vo.IotEnvironmentRecognizePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.environment.vo.IotEnvironmentRecognizeSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.stat.DeptUtil;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.environment.IotEnvironmentRecognizeDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.environment.IotEnvironmentRecognizeMapper;
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 IotEnvironmentRecognizeServiceImpl implements IotEnvironmentRecognizeService {

    @Resource
    private IotEnvironmentRecognizeMapper iotEnvironmentRecognizeMapper;
    @Autowired
    private DeptUtil deptUtil;
    @Autowired
    private DeptService deptService;

    @Override
    public Long createIotEnvironmentRecognize(IotEnvironmentRecognizeSaveReqVO createReqVO) {
        // 插入
        IotEnvironmentRecognizeDO iotEnvironmentRecognize = BeanUtils.toBean(createReqVO, IotEnvironmentRecognizeDO.class);
        iotEnvironmentRecognize.setDeleted(false);
        iotEnvironmentRecognizeMapper.insert(iotEnvironmentRecognize);
        // 返回
        return iotEnvironmentRecognize.getId();
    }

    @Override
    public void updateIotEnvironmentRecognize(IotEnvironmentRecognizeSaveReqVO updateReqVO) {
        // 校验存在
        validateIotEnvironmentRecognizeExists(updateReqVO.getId());
        // 更新
        IotEnvironmentRecognizeDO updateObj = BeanUtils.toBean(updateReqVO, IotEnvironmentRecognizeDO.class);
        iotEnvironmentRecognizeMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotEnvironmentRecognize(Long id) {
        // 校验存在
        validateIotEnvironmentRecognizeExists(id);
        // 删除
        iotEnvironmentRecognizeMapper.deleteById(id);
    }

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

    @Override
    public IotEnvironmentRecognizeDO getIotEnvironmentRecognize(Long id) {
        return iotEnvironmentRecognizeMapper.selectById(id);
    }

    @Override
    public PageResult<IotEnvironmentRecognizeDO> getIotEnvironmentRecognizePage(IotEnvironmentRecognizePageReqVO 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 iotEnvironmentRecognizeMapper.selectPage(pageReqVO, ids);
    }

}