package com.fjhx.wms.service.warehouse.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
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.wms.entity.warehouse.dto.WarehouseDto;
import com.fjhx.wms.entity.warehouse.dto.WarehouseSelectDto;
import com.fjhx.wms.entity.warehouse.po.Warehouse;
import com.fjhx.wms.entity.warehouse.vo.WarehouseVo;
import com.fjhx.wms.mapper.warehouse.WarehouseMapper;
import com.fjhx.wms.service.warehouse.WarehouseService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * <p>
 * 仓库 服务实现类
 * </p>
 *
 * @author
 * @since 2023-03-20
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements WarehouseService {

    @Autowired
    private ISysDeptService sysDeptService;

    @Override
    public Page<WarehouseVo> getPage(WarehouseSelectDto dto) {
        IWrapper<Warehouse> wrapper = getWrapper();
        wrapper.like("w", Warehouse::getName, dto.getKeyword());
        wrapper.eq("w", Warehouse::getType, dto.getType());

        //权限过滤：仓库-子公司看自己的,总公司看全部
        Long companyId = SecurityUtils.getCompanyId();
        if (!Objects.equals(companyId, 100L)) {
            wrapper.eq("w", Warehouse::getCompanyId, companyId);
        } else {
            wrapper.eq("w", Warehouse::getCompanyId, dto.getCompanyId());
        }

        wrapper.orderByDesc("w", Warehouse::getId);
        Page<WarehouseVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<WarehouseVo> records = page.getRecords();

        if (ObjectUtil.isEmpty(records)) {
            return page;
        }

        //赋值仓管员名称
        UserUtil.assignmentNickName(records, Warehouse::getKeeperId, WarehouseVo::setKeeperName);

        //获取归属公司信息
        Map<Long, String> sysDeptMap = new HashMap<>();
        List<Long> companyIds = records.stream().map(Warehouse::getCompanyId).collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(companyIds)) {
            List<SysDept> deptList = sysDeptService.list(Wrappers.<SysDept>query().in("dept_id", companyIds));
            sysDeptMap = deptList.stream().collect(Collectors.toMap(SysDept::getDeptId, SysDept::getDeptName));
        }

        for (WarehouseVo record : records) {
            //赋值归属公司信息
            record.setCompanyName(sysDeptMap.get(record.getCompanyId()));
        }

        return page;
    }

    @Override
    public WarehouseVo detail(Long id) {
        Warehouse Warehouse = this.getById(id);
        WarehouseVo result = BeanUtil.toBean(Warehouse, WarehouseVo.class);
        //赋值仓管员名称
        Map<Long, String> nickNameMapByIds = UserUtil.getNickNameMapByIds(Arrays.asList(result.getKeeperId()));
        result.setKeeperName(nickNameMapByIds.get(result.getKeeperId()));
        return result;
    }

    @Override
    public void add(WarehouseDto warehouseDto) {
        this.nameDuplication(Warehouse::getName, warehouseDto.getName(), "仓库名称重复");

        //赋值产品归属公司
        Long companyId = warehouseDto.getCompanyId();
        if (ObjectUtil.isEmpty(companyId)) {
            warehouseDto.setCompanyId(SecurityUtils.getCompanyId());
        }

        this.save(warehouseDto);
    }

    @Override
    public void edit(WarehouseDto warehouseDto) {
        this.nameDuplication(Warehouse::getName, warehouseDto.getName(), warehouseDto.getId(), "仓库名称重复");

        //赋值产品归属公司
        Long companyId = warehouseDto.getCompanyId();
        if (ObjectUtil.isEmpty(companyId)) {
            warehouseDto.setCompanyId(SecurityUtils.getCompanyId());
        }

        this.updateById(warehouseDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
