/**
 * 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 org.springblade.common.cache.DictCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.fitting.pojo.entity.OperationOrderMaterialEntity;
import org.springblade.modules.fitting.pojo.entity.PlatformFittingApplyEntity;
import org.springblade.modules.material.excel.MaterialOperationExcel;
import org.springblade.modules.material.pojo.dto.MaterialDTO;
import org.springblade.modules.material.pojo.entity.MaterialEntity;
import org.springblade.modules.material.pojo.entity.MaterialOperationEntity;
import org.springblade.modules.material.pojo.entity.MaterialOutboundEntity;
import org.springblade.modules.material.pojo.vo.MaterialOutboundVO;
import org.springblade.modules.material.excel.MaterialOutboundExcel;
import org.springblade.modules.material.mapper.MaterialOutboundMapper;
import org.springblade.modules.material.service.IMaterialOutboundService;
import org.springblade.modules.order.pojo.entity.OperationOrderEntity;
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 java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 出库明细 服务实现类
 *
 * @author cswang
 * @since 2024-08-25
 */
@Service
public class MaterialOutboundServiceImpl extends BaseServiceImpl<MaterialOutboundMapper, MaterialOutboundEntity> implements IMaterialOutboundService {

	@Override
	public IPage<MaterialOutboundVO> selectMaterialOutboundPage(IPage<MaterialOutboundVO> page, MaterialOutboundVO materialOutbound) {
		return page.setRecords(baseMapper.selectMaterialOutboundPage(page, materialOutbound));
	}


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

	@Override
	public boolean saveMaterialOutbound(PlatformFittingApplyEntity apply, MaterialEntity materialEntity,String out_direct,String companyId, BladeUser user,String keepType,String relateName,String keepForm){
		MaterialOutboundEntity entity = new MaterialOutboundEntity();
		entity.setCreateTime(new Date());
		entity.setCreateUser(user.getUserId());
		entity.setIsDeleted(0);
		entity.setMaterialId(materialEntity.getId());
		entity.setCompanyId(companyId);
		entity.setRelateName(relateName);
		entity.setCode(materialEntity.getCode());
		entity.setName(materialEntity.getName());
		entity.setType(materialEntity.getType());
		entity.setModel(materialEntity.getModel());
		entity.setPower(materialEntity.getPower());
		entity.setRetailPrice(materialEntity.getRetailPrice());
		entity.setDescription(materialEntity.getDescription());
		entity.setBrand(materialEntity.getBrand());
		entity.setStock(apply.getApplyNum());
		entity.setOutCount(apply.getApplyNum());
		entity.setOutDirect(out_direct);
		entity.setOutType(out_direct);
		entity.setOutAmount(apply.getApplyNum()*materialEntity.getRetailPrice());
		entity.setApplyId(apply.getId());
        entity.setKeepType(keepType);
        entity.setKeepFrom(keepForm);
		return save(entity);
	}

	@Override
	public boolean saveEmpMaterialInVsOut(OperationOrderMaterialEntity orderMaterialEntity, MaterialEntity materialEntity, String out_direct, BladeUser user,String keepForm,String orderId){
		MaterialOutboundEntity entity = new MaterialOutboundEntity();
		entity.setKeepType("1");   //类型：1.工程师 2.运维商
		entity.setEmpUserId(orderMaterialEntity.getEmpUserId());
		entity.setCreateTime(new Date());
		entity.setCreateUser(user.getUserId());
		entity.setIsDeleted(0);
		entity.setMaterialId(materialEntity.getId());
		entity.setCompanyId(orderMaterialEntity.getSiteId());
		entity.setCode(materialEntity.getCode());
		entity.setName(materialEntity.getName());
		entity.setType(materialEntity.getType());
		entity.setModel(materialEntity.getModel());
		entity.setPower(materialEntity.getPower());
		entity.setRetailPrice(materialEntity.getRetailPrice());
		entity.setDescription(materialEntity.getDescription());
		entity.setBrand(materialEntity.getBrand());
		entity.setStock(orderMaterialEntity.getUserAmount());
		entity.setOutCount(orderMaterialEntity.getUserAmount());
		entity.setOutDirect(out_direct);
		entity.setOutType(out_direct);
		entity.setOutAmount(orderMaterialEntity.getUserAmount()*materialEntity.getRetailPrice());
		entity.setApplyId(orderMaterialEntity.getId());
		entity.setKeepFrom(keepForm);
		entity.setRelateName(user.getNickName());
		entity.setRelateOrderId(orderId);
		return save(entity);
	}

	/**
	 * 添加物料时-初始入库数量
	*/
	@Override
	public void saveMateInkeep(MaterialEntity materialEntity, String out_direct, BladeUser user,String keepForm){
		MaterialOutboundEntity entity = new MaterialOutboundEntity();
		entity.setKeepType("3");   //类型：1.工程师 2.运维商 3.平台
		entity.setEmpUserId("");
		entity.setCreateTime(new Date());
		entity.setCreateUser(user.getUserId());
		entity.setIsDeleted(0);
		entity.setMaterialId(materialEntity.getId());
		entity.setCompanyId("-1");
		entity.setCode(materialEntity.getCode());
		entity.setName(materialEntity.getName());
		entity.setType(materialEntity.getType());
		entity.setModel(materialEntity.getModel());
		entity.setPower(materialEntity.getPower());
		entity.setRetailPrice(materialEntity.getRetailPrice());
		entity.setDescription(materialEntity.getDescription());
		entity.setBrand(materialEntity.getBrand());
		entity.setStock(materialEntity.getStock());
		entity.setOutCount(materialEntity.getStock());
		entity.setOutDirect(out_direct);
		entity.setOutType(out_direct);
		entity.setOutAmount(materialEntity.getStock()*materialEntity.getRetailPrice());
		entity.setApplyId(materialEntity.getId().toString());
		entity.setKeepFrom(keepForm);
		entity.setAllowUser(null);
		save(entity);
	}

	//工程师领取出库
	@Override
	public void savekeep(MaterialDTO materialEntity, String out_direct, BladeUser user, String keepForm, String keepType,
						 String siteId, String empUseId, String relateName, Integer amount, String orderId, String remarks){
		MaterialOutboundEntity entity = new MaterialOutboundEntity();
		entity.setKeepType(keepType);   //类型：1.工程师 2.运维商 3.平台
		if("3".equals(keepType)){
			entity.setCompanyId("-1");
		}else {
			entity.setCompanyId(siteId);
		}
		entity.setKeepFrom(keepForm);
		entity.setEmpUserId(empUseId);
        entity.setRelateName(relateName);
		entity.setCode(materialEntity.getCode());
		entity.setName(materialEntity.getName());
		entity.setType(materialEntity.getType());
		entity.setModel(materialEntity.getModel());
		entity.setPower(materialEntity.getPower());
		entity.setRetailPrice(materialEntity.getRetailPrice());
		entity.setDescription(materialEntity.getDescription());
		entity.setBrand(materialEntity.getBrand());
		entity.setStock(amount);
		entity.setCreateTime(new Date());
		entity.setCreateUser(user.getUserId());
		entity.setIsDeleted(0);
		entity.setMaterialId(materialEntity.getId());

		entity.setOutCount(amount);
		entity.setOutDirect(out_direct);
		entity.setOutType(out_direct);
		entity.setOutAmount(amount*materialEntity.getRetailPrice());
		entity.setApplyId(orderId);
		entity.setRemarks(remarks);
		entity.setRelateOrderId(orderId);
		save(entity);
	}

}
