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.UserReportRightMapper;
import com.kcim.dao.model.UserReportRight;
import org.springframework.stereotype.Repository;

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

/**
 * @program: CostAccount
 * @description: 用户有权限的损益报表数据库交互
 * @author: Wang.YS
 * @create: 2023-10-26 10:41
 **/
@Repository
public class UserReportRightRepository extends ServiceImpl<UserReportRightMapper, UserReportRight> {
    /**
     * 获取当前用户有权限的损益报表数据
     * @return  列表
     */
    public List<UserReportRight> getList() {
        LambdaQueryWrapper<UserReportRight> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserReportRight::getHospId, UserContext.getHospId());
        queryWrapper.eq(UserReportRight::getUserId, UserContext.getCurrentUser().getId());
        return this.list(queryWrapper);
    }


    /**
     * 添加一个用户损益报表权限
     * @param uerReportRight
     */
    public void addUserReportRight(UserReportRight uerReportRight) {
        uerReportRight.setHospId(UserContext.getHospId());
        uerReportRight.setCreateTime(new Date());
        uerReportRight.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
        this.save(uerReportRight);
    }

    /**
     * 添加一个用户损益报表权限
     * @param userId
     * @param reportCode
     */
    public void addUserReportRight(Long userId,String  reportCode) {
        UserReportRight uerReportRight = new UserReportRight();
        uerReportRight.setHospId(UserContext.getHospId());
        uerReportRight.setUserId(userId);
        uerReportRight.setReportCode(reportCode);
        uerReportRight.setCreateTime(new Date());
        uerReportRight.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
        this.save(uerReportRight);
    }

    /**
     * 删除一个用户损益报表权限
     * @param uerReportRight
     */
    public void deleteUserReportRight(UserReportRight uerReportRight) {
        LambdaQueryWrapper<UserReportRight> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserReportRight::getHospId, UserContext.getHospId());
        queryWrapper.eq(UserReportRight::getUserId, uerReportRight.getUserId());
        queryWrapper.eq(UserReportRight::getReportCode, uerReportRight.getReportCode());
        UserReportRight 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 reportCode
     */
    public void deleteUserReportRight(Long userId,String  reportCode) {
        LambdaQueryWrapper<UserReportRight> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserReportRight::getHospId, UserContext.getHospId());
        queryWrapper.eq(UserReportRight::getUserId,userId);
        queryWrapper.eq(UserReportRight::getReportCode, reportCode);
        UserReportRight 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());
        }
    }


}
