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.MaterialMapper;
import com.kcim.dao.model.Material;
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:53
 **/
@Repository
public class MaterialRepository extends ServiceImpl<MaterialMapper, Material> {
    public List<Material> getList() {
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Material::getHospId, UserContext.getHospId());
        return this.list(queryWrapper);
    }

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

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

        return this.page(page,queryWrapper);
    }

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

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

    }

    public List<Material> getByCode(String code) {
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Material::getHospId, UserContext.getHospId());
        queryWrapper.eq(Material::getCode, code);

        return this.list(queryWrapper);
    }

    public Material getOneByCode(String code) {
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Material::getHospId, UserContext.getHospId());
        queryWrapper.eq(Material::getCode, code);

        return this.getOne(queryWrapper);

    }

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

        }

        return this.list(queryWrapper);
    }

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