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.StandItemMapper;
import com.kcim.dao.model.StandItem;
import com.kcim.vo.SessionUserVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

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

/**
 * @program: CostAccount
 * @description: 标准项目字典数据库交互
 * @author: Wang.YS
 * @create: 2023-10-26 10:41
 **/
@Repository
public class StandItemRepository extends ServiceImpl<StandItemMapper, StandItem> {
    /**
     * 标准项目列表
     * @return  列表
     */
    public List<StandItem> getList() {
        LambdaQueryWrapper<StandItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StandItem::getHospId, UserContext.getHospId());
        return this.list(queryWrapper);
    }

    public List<StandItem> getList(String name) {
        LambdaQueryWrapper<StandItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StandItem::getHospId, UserContext.getHospId());
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like(StandItem::getName, name);
        }
        return this.list(queryWrapper);
    }

    public Page<StandItem> getPage(Integer current, Integer pageSize, String name, String kcClassCode) {
        Page<StandItem> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<StandItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StandItem::getHospId, UserContext.getHospId());
        if(!StringUtils.isEmpty(name)){
//            queryWrapper.like(StandItem::getName,name);
            queryWrapper.and(q->q.like(StandItem::getName,name).or().like(StandItem::getNationalCode,name).or().like(StandItem::getCode,name));
        }
        if(!StringUtils.isEmpty(kcClassCode)){
            queryWrapper.eq(StandItem::getKcClassCode,kcClassCode);
        }
        return this.page(page,queryWrapper);
    }

    public StandItem getOneByCode(String code) {
        LambdaQueryWrapper<StandItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StandItem::getHospId, UserContext.getHospId());
        queryWrapper.eq(StandItem::getCode,code);
        return this.getOne(queryWrapper);
    }

    public void deleteStandItem(Integer id) {
        StandItem item = this.getById(id);
        if(Objects.nonNull(item)){
            item.setDeleteTime(new Date());
            item.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            this.updateById(item);
            this.removeById(id);
        }
    }

    public void removeList(List<StandItem> list) {
        for (StandItem item : list) {
            item.setDeleteTime(new Date());
            item.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
        }
        this.updateBatchById(list,100);
        List<Integer> collect = list.stream().map(StandItem::getId).collect(Collectors.toList());
        this.removeBatchByIds(collect,100);
    }

    public List<StandItem> getList(SessionUserVO currentUser) {
        LambdaQueryWrapper<StandItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StandItem::getHospId, currentUser.getHospId());
        return this.list(queryWrapper);
    }
}
