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

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.pms.controller.admin.maintain.record.vo.IotJdRecordPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintain.record.vo.IotJdRecordSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.maintain.IotMaintainDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.maintain.record.IotJdRecordDO;
import cn.iocoder.yudao.module.pms.dal.mysql.maintain.IotMaintainMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.maintain.record.IotJdRecordMapper;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.permission.RoleApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

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


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

/**
 * 基地维修记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotJdRecordServiceImpl implements IotJdRecordService {

    @Resource
    private IotJdRecordMapper iotJdRecordMapper;
    @Autowired
    private IotMaintainMapper iotMaintainMapper;
    @Autowired
    private DeptApi deptApi;
    @Autowired
    private AdminUserApi adminUserApi;
    @Autowired
    private RoleApi roleApi;

    @Override
    public Long createIotJdRecord(IotJdRecordSaveReqVO createReqVO) {
        // 插入
        IotJdRecordDO iotJdRecord = BeanUtils.toBean(createReqVO, IotJdRecordDO.class);
        iotJdRecord.setDeleted(false);
        iotJdRecordMapper.insert(iotJdRecord);
        // 返回
        return iotJdRecord.getId();
    }

    @Override
    public void updateIotJdRecord(IotJdRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateIotJdRecordExists(updateReqVO.getId());
        // 更新
        IotJdRecordDO updateObj = BeanUtils.toBean(updateReqVO, IotJdRecordDO.class);
        iotJdRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotJdRecord(Long id) {
        // 校验存在
        validateIotJdRecordExists(id);
        // 删除
        iotJdRecordMapper.deleteById(id);
    }

    private void validateIotJdRecordExists(Long id) {
        if (iotJdRecordMapper.selectById(id) == null) {
            throw exception(new ErrorCode(666,"不存在记录"));
        }
    }

    @Override
    public IotJdRecordDO getIotJdRecord(Long id) {
        return iotJdRecordMapper.selectById(id);
    }

    @Override
    public PageResult<IotJdRecordDO> getIotJdRecordPage(IotJdRecordPageReqVO pageReqVO) {
        return iotJdRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AdminUserRespDTO> getAssist(Long id) {
//        IotJdRecordDO iotJdRecordDO = iotJdRecordMapper.selectById(id);
//        if (Objects.isNull(iotJdRecordDO)) {
//            throw new ServiceException(new ErrorCode(111,"没有维修记录"));
//        }
//        if (Objects.isNull(iotJdRecordDO.getRepairId())){
//            throw new ServiceException(new ErrorCode(222,"维修工单id不存在"));
//        }
        IotMaintainDO maintainDO = iotMaintainMapper.selectById(id);
        AdminUserRespDTO user = adminUserApi.getUser(maintainDO.getMaintainPerson());
        List<AdminUserRespDTO> userListByDept = adminUserApi.getUserListByDeptId(user.getDeptId());
        List<Long> roleUserIds = roleApi.getRoleUserIds("维修人员");

        List<AdminUserRespDTO> collect = userListByDept.stream().filter(e -> roleUserIds.contains(e.getId())).collect(Collectors.toList());
        return collect;
    }

}
