package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.UserResponsibilityRightMapper;
import com.kcim.dao.model.UserResponsibilityRight;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @program: CostAccount
 * @description: 用户有权限的责任中心数据库交互
 * @author: Wang.YS
 * @create: 2023-10-26 10:41
 **/
@Repository
public class UserResponsibilityRightRepository extends ServiceImpl<UserResponsibilityRightMapper, UserResponsibilityRight> {
    /**
     * 获取当前用户有权限的责任中心数据
     * @return  列表
     */
    public List<UserResponsibilityRight> getList() {
        return getListByUserId(UserContext.getCurrentUser().getId());
    }

    /**
     * 获取指定用户有权限的列表
     * @param userId
     * @return
     */
    public List<UserResponsibilityRight> getListByUserId(Long userId) {
        LambdaQueryWrapper<UserResponsibilityRight> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserResponsibilityRight::getHospId, UserContext.getHospId());
        queryWrapper.eq(UserResponsibilityRight::getUserId, userId);
        return this.list(queryWrapper);
    }

    /**
     * 批量删除指定用户责任中心权限
     * @param list
     */
    public void removeUserResponsibilityRightList(List<UserResponsibilityRight> list){
        if(!CollectionUtils.isEmpty(list)){
            list.stream().forEach(userResponsibility-> {
                userResponsibility.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
                userResponsibility.setDeleteTime(new Date());
            });
            this.updateBatchById(list);
            List<Integer> collect = list.stream().map(UserResponsibilityRight::getId).collect(Collectors.toList());
            this.removeByIds(collect);
        }
    }

    /**
     * 添加一个用户责任中心权限
     * @param userResponsibilityRight
     */
    public void addUserResponsibilityRight(UserResponsibilityRight userResponsibilityRight) {
        userResponsibilityRight.setHospId(UserContext.getHospId());
        userResponsibilityRight.setCreateTime(new Date());
        userResponsibilityRight.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
        this.save(userResponsibilityRight);
    }

    /**
     * 添加一个用户责任中心权限
     * @param userId
     * @param responsibilityCode
     */
    public void addUserResponsibilityRight(Long userId,String  responsibilityCode) {
        UserResponsibilityRight userResponsibilityRight = new UserResponsibilityRight();
        userResponsibilityRight.setHospId(UserContext.getHospId());
        userResponsibilityRight.setUserId(userId);
        userResponsibilityRight.setResponsibilityCode(responsibilityCode);
        userResponsibilityRight.setCreateTime(new Date());
        userResponsibilityRight.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
        this.save(userResponsibilityRight);
    }

    /**
     * 删除一个用户责任中心权限
     * @param userResponsibilityRight
     */
    public void deleteUserResponsibilityRight(UserResponsibilityRight userResponsibilityRight) {
        LambdaQueryWrapper<UserResponsibilityRight> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserResponsibilityRight::getHospId, UserContext.getHospId());
        queryWrapper.eq(UserResponsibilityRight::getUserId, userResponsibilityRight.getUserId());
        queryWrapper.eq(UserResponsibilityRight::getResponsibilityCode, userResponsibilityRight.getResponsibilityCode());
        UserResponsibilityRight item = this.getOne(queryWrapper);
        if(Objects.nonNull(item)){
            item.setDeleteTime(new Date());
            item.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            this.updateById(item);
            this.removeById(item.getId());
        }
    }

    /**
     * 删除指定用户的责任中心权限
     * @param userId
     * @param responsibilityCode
     */
    public void deleteUserResponsibilityRight(Long userId,String  responsibilityCode) {
        LambdaQueryWrapper<UserResponsibilityRight> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserResponsibilityRight::getHospId, UserContext.getHospId());
        queryWrapper.eq(UserResponsibilityRight::getUserId, userId);
        queryWrapper.eq(UserResponsibilityRight::getResponsibilityCode, responsibilityCode);
        UserResponsibilityRight item = this.getOne(queryWrapper);
        if(Objects.nonNull(item)){
            item.setDeleteTime(new Date());
            item.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            this.updateById(item);
            this.removeById(item.getId());
        }
    }

}
