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.CostChildColumnMapper;
import com.kcim.dao.model.CostChildColumn;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

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

/**
 * @program: CostAccount
 * @description: 成本子列数据库交互
 * @author: Wang.YS
 * @create: 2023-11-01 14:16
 **/
@Repository
public class CostChildColumnRepository extends ServiceImpl<CostChildColumnMapper, CostChildColumn> {
    public List<CostChildColumn> getByParentCode(List<Long> collect) {
        LambdaQueryWrapper<CostChildColumn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostChildColumn::getHospId, UserContext.getHospId());
        queryWrapper.in(CostChildColumn::getParentColumnCode,collect);
        return this.list(queryWrapper);
    }

    public void removeByParentCode(Long parentCode) {
        LambdaQueryWrapper<CostChildColumn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostChildColumn::getHospId, UserContext.getCurrentLoginHospId());
        queryWrapper.eq(CostChildColumn::getParentColumnCode,parentCode);
        List<CostChildColumn> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            for (CostChildColumn column:list){
                column.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
                column.setDeleteTime(new Date());
            }
            this.updateBatchById(list);
            List<Integer> collect = list.stream().map(CostChildColumn::getId).collect(Collectors.toList());
            this.removeByIds(collect);
        }
    }
}
