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.CostAccountShareDetailMapper;
import com.kcim.dao.model.CostAccountShareDetail;
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 CostAccountShareDetailRepository extends ServiceImpl<CostAccountShareDetailMapper, CostAccountShareDetail> {

    /**
     * 获取指定分摊设置对应的会计科目
     * @param accountShareId
     * @return
     */
    public List<CostAccountShareDetail> getCostAccountShareDetail(Long accountShareId) {
        LambdaQueryWrapper<CostAccountShareDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostAccountShareDetail::getHospId, UserContext.getHospId());
        queryWrapper.eq(CostAccountShareDetail::getDelFlag, NumberConstant.ZERO);
        if(!ObjectUtils.isEmpty(accountShareId)) {
            queryWrapper.eq(CostAccountShareDetail::getAccountShareId, accountShareId);
        }
        return this.list(queryWrapper);
    }

    /**
     * 作废指定分摊设置对应的会计科目
     * @param accountShareId
     * @return
     */
    public boolean delCostAccountShareDetail(Long accountShareId) {
        LambdaUpdateWrapper<CostAccountShareDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CostAccountShareDetail::getDelFlag,NumberConstant.ONE);
        updateWrapper.set(CostAccountShareDetail::getDeleteUser,UserContext.getCurrentUser().getId());
        updateWrapper.set(CostAccountShareDetail::getDeleteTime,new Date());

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

    /**
     * 批量删除指定分摊设置对应的会计科目
     * @param accountShareIdList
     * @return
     */
    public boolean delCostAccountShareDetailList(List<Long> accountShareIdList) {
        LambdaUpdateWrapper<CostAccountShareDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CostAccountShareDetail::getDelFlag,NumberConstant.ONE);
        updateWrapper.set(CostAccountShareDetail::getDeleteUser,UserContext.getCurrentUser().getId());
        updateWrapper.set(CostAccountShareDetail::getDeleteTime,new Date());

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