package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.AccountingItemMapMapper;
import com.kcim.dao.model.AccountingItemMap;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * @program: CostAccount
 * @description:
 * @author: Wang.YS
 * @create: 2023-11-27 17:52
 **/
@Repository
public class AccountingItemMapRepository extends ServiceImpl<AccountingItemMapMapper, AccountingItemMap> {
    public List<AccountingItemMap> getList() {
        LambdaQueryWrapper<AccountingItemMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountingItemMap::getHospId, UserContext.getHospId());
        return this.list(queryWrapper);

    }

    public void batchDelete(List<Integer> ids) {
        LambdaQueryWrapper<AccountingItemMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountingItemMap::getHospId, UserContext.getHospId());
        queryWrapper.in(AccountingItemMap::getId,ids);
        List<AccountingItemMap> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            for (AccountingItemMap itemMap : list) {
                itemMap.setDeleteTime(new Date());
                itemMap.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            }
            this.updateBatchById(list);
            List<Integer> collect = list.stream().map(AccountingItemMap::getId).collect(Collectors.toList());
            this.removeBatchByIds(collect);
        }



    }

    public Page<AccountingItemMap> getPage(Integer current, Integer pageSize, String accountingCode, String itemName) {
        Page<AccountingItemMap> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<AccountingItemMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountingItemMap::getHospId, UserContext.getHospId());
        queryWrapper.eq(AccountingItemMap::getAccountingCode,accountingCode);
        if(!StringUtils.isEmpty(itemName)){
            queryWrapper.like(AccountingItemMap::getItemName,itemName);
        }
        return this.page(page, queryWrapper);


    }
}
