/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.modules.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springblade.common.cache.DictCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.material.pojo.entity.MaterialEngineerEntity;
import org.springblade.modules.material.pojo.vo.MaterialEngineerVO;
import org.springblade.modules.material.excel.MaterialEngineerExcel;
import org.springblade.modules.material.mapper.MaterialEngineerMapper;
import org.springblade.modules.material.service.IMaterialEngineerService;
import org.springblade.modules.operation.constant.SourceConstants;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.management.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 工程师库存 服务实现类
 *
 * @author cswang
 * @since 2024-08-25
 */
@Service
public class MaterialEngineerServiceImpl extends BaseServiceImpl<MaterialEngineerMapper, MaterialEngineerEntity> implements IMaterialEngineerService {

	@Override
	public IPage<MaterialEngineerVO> selectMaterialEngineerPage(IPage<MaterialEngineerVO> page, MaterialEngineerVO materialEngineer) {
		return page.setRecords(baseMapper.selectMaterialEngineerPage(page, materialEngineer));
	}


	@Override
	public List<MaterialEngineerExcel> exportMaterialEngineer(Wrapper<MaterialEngineerEntity> queryWrapper) {
		List<MaterialEngineerEntity> materialEngineerList = this.list(queryWrapper);
		List<MaterialEngineerExcel> excelList = new ArrayList<>();
		materialEngineerList.forEach(material -> {
			MaterialEngineerExcel excel = new MaterialEngineerExcel();
			BeanUtil.copyProperties(material,excel);
			excel.setBrandStr(DictCache.getValue(DictEnum.BRAND, material.getBrand()));
			excel.setTypeStr(DictCache.getValue(DictEnum.WL_TYPE, material.getType()));
			excelList.add(excel);
//			materialEngineer.setTypeName(DictCache.getValue(DictEnum.YES_NO, MaterialEngineer.getType()));
		});
		return excelList;
	}

	@Override
	public List<MaterialEngineerVO> engineerStockList(IPage<MaterialEngineerVO> page, Map<String, Object> params) {
		if(params.get("engineerId")==null && params.get("empId")==null){ //engineerId 工程师userId,empId 员工表id
			throw new ServiceException("工程师id不能为空");
		}
		List<MaterialEngineerVO> list=baseMapper.engineerStockList(page,params);
		return list;
	}

	@Override
	public Long countEngineerStockList(Map<String, Object> params) {
		Long count=baseMapper.countEngineerStockList(params);
		return count;
	}


	//获取工程师库存
	@Override
	public MaterialEngineerEntity getEngineerStock(String engineerUserId, String materialId) {
		LambdaQueryWrapper<MaterialEngineerEntity> queryWrapper=new LambdaQueryWrapper<>();
		queryWrapper.eq(MaterialEngineerEntity::getEngineerId,engineerUserId);
		queryWrapper.eq(MaterialEngineerEntity::getMaterialId,materialId);
		queryWrapper.eq(MaterialEngineerEntity::getIsDeleted, SourceConstants.ZERO);
		return getOne(queryWrapper);
	}

}
