package com.sw.mes.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.dto.MaterialModelDto;
import com.sw.mes.core.exception.BusinessException;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.store.dto.WarehouseMaterialDto;
import com.sw.mes.store.entity.WarehouseMaterial;
import com.sw.mes.store.entity.WarehouseModel;
import com.sw.mes.store.entity.WarehouseQueryParam;
import com.sw.mes.store.mapper.WarehouseMaterialMapper;
import com.sw.mes.store.service.WarehouseMaterialService;
import com.sw.mes.store.service.WarehouseModelService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author wupeng
 * @Date 2023/7/25 14:13
 **/
@Service
@Transactional
public class WarehouseMaterialServiceImpl extends ServiceImpl<WarehouseMaterialMapper, WarehouseMaterial> implements WarehouseMaterialService {

    @Autowired
    private WarehouseMaterialMapper warehouseMaterialMapper;

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private WarehouseModelService warehouseModelService;

    @Override
    public WarehouseMaterial getByMaterialId(Long materialId) {
        return warehouseMaterialMapper.getByMaterialId(materialId);
    }

    @Override
    public WarehouseMaterial getByMaterialCode(String materialCode) {
        LambdaQueryWrapper<WarehouseMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WarehouseMaterial::getMaterialCode, materialCode);
        List<WarehouseMaterial> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public WarehouseMaterial getMaterialByMaterialCode(String materialCode) {
        QueryWrapper<WarehouseMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("material_code", materialCode);
        List<WarehouseMaterial> list = this.list(queryWrapper);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void create(WarehouseMaterialDto dto) {
        List<MaterialModelDto> materials = dto.getMaterials();
        if (CollectionUtils.isEmpty(materials)) {
            throw new BusinessException("物料数据为空，请重新选择物料数据！");
        }
        Long defaultLocationId = dto.getDefaultLocationId();
        WarehouseModel warehouseModel = warehouseModelService.getById(defaultLocationId);
        if (warehouseModel == null) {
            throw new BusinessException("默认仓库数据不存在，请重新选择！");
        }
        dto.setDefaultLocationId(warehouseModel.getId());
        dto.setDefaultLocation(warehouseModel.getName());
        List<WarehouseMaterial> warehouseMaterials = new ArrayList<>(materials.size());
        for (MaterialModelDto material : materials) {
            WarehouseMaterial warehouseMaterial = modelMapper.map(dto, WarehouseMaterial.class);
            Long id = Long.valueOf(material.getId());
            WarehouseMaterial byMaterialId = this.getByMaterialId(id);
            if (byMaterialId != null) {
                throw new BusinessException(material.getName() + "物料配置已存在，请重新选择物料数据！");
            }
            warehouseMaterial.setMaterialId(id);
            warehouseMaterial.setMaterialCode(material.getCode());
            warehouseMaterial.setMaterialName(material.getName());
            warehouseMaterial.setSafetyStock(dto.getLowerLimit() + "-" + dto.getUpperLimit());
            warehouseMaterial.setCreateTime(new Date());
            warehouseMaterial.setCreateUserId(BaseContextHandler.getUserID());
            warehouseMaterial.setCreateUserName(BaseContextHandler.getUsername());
            warehouseMaterials.add(warehouseMaterial);
        }
        this.saveOrUpdateBatch(warehouseMaterials);
    }

    @Override
    public List<WarehouseMaterial> getByMaterialIds(List<Long> materialIds) {
        return warehouseMaterialMapper.getByMaterialIds(materialIds);
    }

    @Override
    public Page<WarehouseMaterial> listByPage(ServletRequest request, RequestPage page) {
        WarehouseQueryParam param = new WarehouseQueryParam();
        param.setMaterialName(request.getParameter("search_LIKE_materialName"));
        param.setMaterialCode(request.getParameter("search_LIKE_materialCode"));
        param.setDefaultLocation(request.getParameter("search_LIKE_defaultLocation"));
        String materialTypeId = request.getParameter("search_IN_materialTypeId");
        param.setMaterialTypeName(request.getParameter("search_LIKE_materialTypeName"));
        param.setMaterialTypeCode(request.getParameter("search_LIKE_materialTypeCode"));
        if (StringUtils.isNotBlank(materialTypeId)) {
            String[] split = materialTypeId.split(",");
            param.setMaterialTypeId(Arrays.asList(split));
        }
        Page<WarehouseMaterial> warehouseMaterialIPage = warehouseMaterialMapper.listByPage(new Page<>(page.getPage(), page.getRows()), param);
        return warehouseMaterialIPage;
    }
}
