package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.constants.NumberConstant;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.CostAccountShareTargetMapper;
import com.kcim.dao.model.CostAccountShareTarget;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

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

/**
 * @program: CostAccount
 * @description:
 * @author: Wang.YS
 * @create: 2024-04-03 14:49
 **/
@Repository
public class CostAccountShareTargetRepository extends ServiceImpl<CostAccountShareTargetMapper, CostAccountShareTarget> {
    /**
     * 获取指定分摊设置对应的目标责任中心
     * @param accountShareId
     * @return
     */
    public List<CostAccountShareTarget> getCostAccountShareTarget(Long accountShareId) {
        LambdaQueryWrapper<CostAccountShareTarget> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostAccountShareTarget::getHospId, UserContext.getHospId());
        queryWrapper.eq(CostAccountShareTarget::getDelFlag, NumberConstant.ZERO);
        if(!ObjectUtils.isEmpty(accountShareId)) {
            queryWrapper.eq(CostAccountShareTarget::getAccountShareId, accountShareId);
        }
        return this.list(queryWrapper);
    }

    /**
     * 作废指定分摊设置对应的目标责任中心
     * @param accountShareId
     * @return
     */
    public boolean delCostAccountShareTarget(Long accountShareId) {
        LambdaUpdateWrapper<CostAccountShareTarget> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CostAccountShareTarget::getDelFlag,NumberConstant.ONE);
        updateWrapper.set(CostAccountShareTarget::getDeleteUser,UserContext.getCurrentUser().getId());
        updateWrapper.set(CostAccountShareTarget::getDeleteTime,new Date());

        updateWrapper.eq(CostAccountShareTarget::getHospId, UserContext.getHospId());
        updateWrapper.eq(CostAccountShareTarget::getDelFlag, NumberConstant.ZERO);
        if(!ObjectUtils.isEmpty(accountShareId)) {
            updateWrapper.eq(CostAccountShareTarget::getAccountShareId, accountShareId);
        }
        return this.update(updateWrapper);
    }

    /**
     * 批量删除指定分摊设置对应的目标责任中心
     * @param accountShareIdList
     * @return
     */
    public boolean delCostAccountShareTargetList(List<Long> accountShareIdList) {
        LambdaUpdateWrapper<CostAccountShareTarget> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CostAccountShareTarget::getDelFlag,NumberConstant.ONE);
        updateWrapper.set(CostAccountShareTarget::getDeleteUser,UserContext.getCurrentUser().getId());
        updateWrapper.set(CostAccountShareTarget::getDeleteTime,new Date());

        updateWrapper.eq(CostAccountShareTarget::getHospId, UserContext.getHospId());
        updateWrapper.eq(CostAccountShareTarget::getDelFlag, NumberConstant.ZERO);
        if(!CollectionUtils.isEmpty(accountShareIdList)) {
            updateWrapper.in(CostAccountShareTarget::getAccountShareId, accountShareIdList);
        }
        return this.update(updateWrapper);
    }
}
