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

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: CostAccount
 * @description: 药品成本数据库交互
 * @author: Wang.YS
 * @create: 2023-10-25 16:13
 **/
@Repository
public class DrugRepository extends ServiceImpl<DrugMapper, Drug> {

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

    public Map<String, BigDecimal> getCostMap() {
        List<Drug> list = getList();
        if(!CollectionUtils.isEmpty(list)){
            return list.stream().collect(Collectors.toMap(Drug::getCode, Drug::getCost, (a, b) -> b));
        }
        return new HashMap<>();
    }

    public Page<Drug> getPage(Integer current, Integer pageSize, String name, Integer stop) {
        Page<Drug> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<Drug> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Drug::getHospId, UserContext.getHospId());
        if(!stop.equals(NumberConstant.ONE)){
            queryWrapper.eq(Drug::getStatus,stop);
        }
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like(Drug::getName,name);
        }

         return this.page(page,queryWrapper);
    }

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

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


    }

    public List<Drug> getByCode(String code) {
        LambdaQueryWrapper<Drug> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Drug::getHospId, UserContext.getHospId());
        queryWrapper.eq(Drug::getCode,code);
        return this.list(queryWrapper);
    }

    public Drug getOneByCode(String code) {

        LambdaQueryWrapper<Drug> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Drug::getHospId, UserContext.getHospId());
        queryWrapper.eq(Drug::getCode,code);
        return this.getOne(queryWrapper);
    }

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

        }

        return this.list(queryWrapper);
    }

    public Map<String, BigDecimal> getCostMap(SessionUserVO currentUser) {

        LambdaQueryWrapper<Drug> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Drug::getHospId, currentUser.getHospId());
        List<Drug> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            return list.stream().collect(Collectors.toMap(Drug::getCode, Drug::getCost, (a, b) -> b));
        }
        return new HashMap<>();
    }
}
