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.constants.NumberConstant;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.ItemMapper;
import com.kcim.dao.model.Item;
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-11-10 14:17
 **/
@Repository
public class ItemRepository extends ServiceImpl<ItemMapper, Item> {
    public Page<Item> getPage(Integer current, Integer pageSize, String name, Integer type) {
        Page<Item> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Item::getHospId, UserContext.getHospId());
        if(!StringUtils.isEmpty(name)){
//            queryWrapper.like(Item::getName,name);
            queryWrapper.and(q->q.like(Item::getName,name).or().like(Item::getNationalCode,name).or().like(Item::getCode,name));
        }
        if(type.equals(NumberConstant.ONE)){
            queryWrapper.isNull(Item::getStandItemCode);
        }
        return this.page(page,queryWrapper);
    }

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

    /**
     * 按项目代码和科室代码查询唯一一条
     * @param code 项目代码
     * @param departmentCode 科室代码
     * @return 医疗服务项目
     */
    public Item getOneByCode(String code, String departmentCode) {
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Item::getHospId, UserContext.getHospId());
        queryWrapper.eq(Item::getCode,code);
        queryWrapper.eq(Item::getDepartmentCode,departmentCode);
        return this.getOne(queryWrapper);
    }
    public List<Item> getByCode(String code) {
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Item::getHospId, UserContext.getHospId());
        queryWrapper.eq(Item::getCode,code);
        return this.list(queryWrapper);
    }
    public List<Item> getByCode(String code, String departmentCode) {
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Item::getHospId, UserContext.getHospId());
        queryWrapper.eq(Item::getCode,code);
        queryWrapper.eq(Item::getDepartmentCode,departmentCode);
        return this.list(queryWrapper);
    }
    public void deleteItem(Integer id) {
        Item 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);
        }
    }

    /**
     * 获取责任中心的医疗服务项目字典
     * @return
     */
    public List<Item> getRespItemList(){
        return this.baseMapper.getRespItemList(UserContext.getHospId());
    }

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

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

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

        }

        return this.list(queryWrapper);
    }

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

    public List<Item> getAll() {
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        return this.list(queryWrapper);
    }



}
