/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.pl.goods.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.pl.goods.entity.GoodsEntity;
import org.springblade.modules.pl.goods.entity.GoodsMaterialEntity;
import org.springblade.modules.pl.goods.service.IGoodsMaterialService;
import org.springblade.modules.pl.goods.vo.GoodsVO;
import org.springblade.modules.pl.goods.excel.GoodsExcel;
import org.springblade.modules.pl.goods.mapper.GoodsMapper;
import org.springblade.modules.pl.goods.service.IGoodsService;
import org.springblade.modules.pl.product.entity.ProductEntity;
import org.springblade.modules.pl.productBatch.entity.ProductBatchEntity;
import org.springblade.modules.pl.productModel.entity.ProductModelEntity;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 商品表 服务实现类
 *
 * @author BladeX
 * @since 2024-02-03
 */
@Service
public class GoodsServiceImpl extends BaseServiceImpl<GoodsMapper, GoodsEntity> implements IGoodsService {

	@Resource
	private IGoodsMaterialService goodsMaterialService;

	@Override
	public IPage<GoodsVO> selectGoodsPage(IPage<GoodsVO> page, GoodsVO goods) {
		return page.setRecords(baseMapper.selectGoodsPage(page, goods));
	}


	@Override
	public List<GoodsExcel> exportGoods(Wrapper<GoodsEntity> queryWrapper) {
		List<GoodsExcel> goodsList = baseMapper.exportGoods(queryWrapper);
		//goodsList.forEach(goods -> {
		//	goods.setTypeName(DictCache.getValue(DictEnum.YES_NO, Goods.getType()));
		//});
		return goodsList;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R submit(GoodsVO goodsVO) {
		BladeUser user = AuthUtil.getUser();
		if(Objects.requireNonNull(user).getUserCategory()==2){
			goodsVO.setFactoryId(user.getFactoryId());
		}
		if(goodsVO==null || goodsVO.getDetails().size()==0){
			return R.fail("请填写商品物料信息");
		}
		if(goodsVO.getId()!=null){
			goodsMaterialService.update(Wrappers.<GoodsMaterialEntity>lambdaUpdate().eq(GoodsMaterialEntity::getGoodsId,goodsVO.getId()).set(GoodsMaterialEntity::getStatus,0));
		}
		this.saveOrUpdate(goodsVO);
		for(GoodsMaterialEntity goodsMaterial : goodsVO.getDetails()){
			goodsMaterial.setGoodsId(goodsVO.getId());
			goodsMaterial.setStatus(1);
		}
		goodsMaterialService.saveOrUpdateBatch(goodsVO.getDetails());
		goodsMaterialService.remove(Wrappers.<GoodsMaterialEntity>lambdaUpdate().eq(GoodsMaterialEntity::getGoodsId,goodsVO.getId()).eq(GoodsMaterialEntity::getStatus,0));
		return R.success("操作成功");
	}

}
