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

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

    public Page<OrderItemMap> getPage(Integer current, Integer pageSize, String orderCode, String itemName) {
        Page<OrderItemMap> page = new Page<>(current,pageSize);

        LambdaQueryWrapper<OrderItemMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderItemMap::getHospId, UserContext.getHospId());
        queryWrapper.eq(OrderItemMap::getOrderCode, orderCode);

        if(!StringUtils.isEmpty(itemName)){
            queryWrapper.like(OrderItemMap::getItemName, itemName);

        }
        return this.page(page,queryWrapper);
    }

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

    }

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

    public List<OrderItemMap> getByOrderCode(String orderCode) {
        LambdaQueryWrapper<OrderItemMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderItemMap::getHospId, UserContext.getHospId());
        queryWrapper.eq(OrderItemMap::getOrderCode, orderCode);
        return this.list(queryWrapper);
    }
}
