package cn.iocoder.yudao.module.server.service.materialshareuser;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.server.controller.admin.materialshareuser.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.materialshareuser.MaterialShareUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.server.dal.mysql.materialshareuser.MaterialShareUserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.server.enums.ErrorCodeConstants.*;

/**
 * 资料共享用户 Service 实现类
 *
 * @author TRS
 */
@Service
@Validated
public class MaterialShareUserServiceImpl implements MaterialShareUserService {

    @Resource
    private MaterialShareUserMapper materialShareUserMapper;

    // 新增方法：根据用户ID获取部门ID

    @Override
    public Long createMaterialShareUser(MaterialShareUserSaveReqVO createReqVO) {
        Long materialId =createReqVO.getMaterialId();
        List<Long> userIdsList = createReqVO.getUserIds();

        List<String> deptNames = createReqVO.getDeptNames(); // 获取部门名称

        Map<String, Object> map = new HashMap<>();
        map.put("material_id", materialId);
        List<MaterialShareUserDO> materialShareUserDOS = materialShareUserMapper.selectByMap(map);
        if(materialShareUserDOS.size()>0){
            materialShareUserMapper.deleteByMap(map);
        }
        for (int i = 0; i < userIdsList.size(); i++) {
            materialShareUserMapper.insert(new MaterialShareUserDO().setMaterialId(materialId).setUserId(userIdsList.get(i)).setDeptName(deptNames.get(i)));
        }
        // 返回
        return 0L;
    }

    @Override
    public void updateMaterialShareUser(MaterialShareUserSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialShareUserExists(updateReqVO.getId());
        // 更新
        MaterialShareUserDO updateObj = BeanUtils.toBean(updateReqVO, MaterialShareUserDO.class);
        materialShareUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialShareUser(Long id) {
        // 校验存在
        //validateMaterialShareUserExists(id);
        // 删除
        Map<String, Object> map = new HashMap<>();
        map.put("material_id", id);
        materialShareUserMapper.deleteByMap(map);
    }

    private void validateMaterialShareUserExists(Long id) {
        if (materialShareUserMapper.selectById(id) == null) {
            throw exception(MATERIAL_SHARE_USER_NOT_EXISTS);
        }
    }

    @Override
    public List<MaterialShareUserDO> getMaterialShareUser(Long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("material_id", id);
        List<MaterialShareUserDO> materialShareUserDOS = materialShareUserMapper.selectByMap(map);
        return materialShareUserDOS;
    }

    @Override
    public PageResult<MaterialShareUserDO> getMaterialShareUserPage(MaterialShareUserPageReqVO pageReqVO) {
        return materialShareUserMapper.selectPage(pageReqVO);
    }

}