package com.fjhx.service.warehouse.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.entity.stock.Stock;
import com.fjhx.entity.warehouse.Warehouse;
import com.fjhx.enums.warehouse.WarehouseTypeEnum;
import com.fjhx.mapper.warehouse.WarehouseMapper;
import com.fjhx.params.warehouse.WarehouseVo;
import com.fjhx.service.stock.StockService;
import com.fjhx.service.warehouse.WarehouseService;
import com.fjhx.utils.UserClientUtil;
import com.fjhx.utils.WrapperUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 仓库 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-11-24
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements WarehouseService {

    @Autowired
    private StockService stockService;

    @Override
    public Page<Warehouse> getPage(Map<String, Object> condition) {

        QueryWrapper<Warehouse> wrapper = Wrappers.query();

        WrapperUtil.init(condition, wrapper);

        //仓库类型
        wrapper.lambda().eq(Func.isNotEmpty(condition.get("type")), Warehouse::getType, condition.get("type"));

        //关键字
        wrapper.and(Func.isNotEmpty(condition.get("keyword")), o -> o.apply("instr(`name`, '" + condition.get("keyword") + "') > 0").or().apply("instr(remarks, '" + condition.get("keyword") + "') > 0"));

        //排序
        wrapper.lambda().orderByAsc(Warehouse::getType).orderByAsc(Warehouse::getName);

        Page<Warehouse> page = page(condition, wrapper);
        if (Func.isNotEmpty(page.getRecords())) {
            //仓管员名称map
            Map<Long, String> userNameMap = UserClientUtil.getUserNameMap(page.getRecords(), Warehouse::getWarehouseKeeperId);
            for (Warehouse record : page.getRecords()) {
                if (Func.isNotEmpty(userNameMap) && Func.isNotEmpty(userNameMap.get(record.getWarehouseKeeperId()))) {
                    record.setWarehouseKeeperName(userNameMap.get(record.getWarehouseKeeperId()));
                }
            }
        }
        return page;
    }

    @Override
    public void add(WarehouseVo warehouseVo) {
        save(warehouseVo);
    }

    @Override
    public void edit(WarehouseVo warehouseVo) {
        updateById(warehouseVo);
    }

    @Override
    public void delete(WarehouseVo warehouseVo) {
        //查询是否存在库存
        List<Stock> list = stockService.lambdaQuery().eq(Stock::getWarehouseId, warehouseVo.getId()).list();
        if (Func.isNotEmpty(list)) {
            throw new ServiceException("该仓库下存在库存，无法删除！");
        }

        removeById(warehouseVo.getId());
    }

    @Override
    public List<Warehouse> select() {
        return lambdaQuery().ne(Warehouse::getType, WarehouseTypeEnum.WAREHOUSE_TYPE_100.getKey()).list();
    }

    /**
     * 根据ID查询
     *
     * @param ids 仓库ID集合
     * @return
     */
    @Override
    public List<Warehouse> getByIds(List<Long> ids) {
        if (Func.isEmpty(ids)) {
            return new ArrayList<>();
        }
        return lambdaQuery().in(Warehouse::getId, ids).list();
    }

    /**
     * 根据ID查询
     *
     * @param ids 仓库ID集合
     * @return
     */
    @Override
    public Map<Long, Warehouse> getByIdsToMap(List<Long> ids) {
        List<Warehouse> list = getByIds(ids);
        if (Func.isEmpty(list)) {
            return new HashMap<>();
        }
        return list.stream().collect(Collectors.toMap(Warehouse::getId, Function.identity(), (key1, key2) -> key2));
    }
}
