package com.ruoyi.materialClass.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.materialClass.mapper.MaterialClassSubMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;
import java.util.List;
import java.util.Map;
	import java.util.Arrays;
	import org.springframework.transaction.annotation.Transactional;
	import com.ruoyi.materialClass.domain.MaterialClassSub;
import com.ruoyi.materialClass.mapper.MaterialClassMasterMapper;
import com.ruoyi.materialClass.domain.MaterialClassMaster;
import com.ruoyi.materialClass.service.IMaterialClassMasterService;

/**
 * 物资分类一级分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-05
 */
@Service
public class MaterialClassMasterServiceImpl extends ServiceImpl<MaterialClassMasterMapper, MaterialClassMaster> implements IMaterialClassMasterService {

	@Autowired
	private MaterialClassMasterMapper materialClassMastMapper;
	@Autowired
	private MaterialClassSubMapper materialClassSubMapper;
	/**
	 * 查询物资分类一级分类
	 *
	 * @param classMastId 物资分类一级分类主键
	 * @return 物资分类一级分类
	 */
	@Override
	public MaterialClassMaster selectMaterialClassMastByClassMastId(String classMastId)
	{
		MaterialClassMaster materialClassMaster = materialClassMastMapper.selectMaterialClassMastByClassMastId(classMastId);
		/*MaterialClassSub sub = new MaterialClassSub();
		sub.setClassMastId(materialClassMaster.getClassMastId());
		List<MaterialClassSub> subs = materialClassSubMapper.selectMaterialClassSubList(sub);
		materialClassMaster.setMaterialClassSubList(subs);*/
		return materialClassMaster;
	}

	/**
	 * 查询物资分类一级分类列表
	 *
	 * @param materialClassMast 物资分类一级分类
	 * @return 物资分类一级分类
	 */
	@Override
	public List<MaterialClassMaster> selectMaterialClassMastList(MaterialClassMaster materialClassMast)
	{
		return materialClassMastMapper.selectMaterialClassMastList(materialClassMast);
	}

		/**
		 * 新增物资分类一级分类
		 *
		 * @param materialClassMast 物资分类一级分类
		 * @return 结果
		 */
		@Transactional
		@Override
		public int insertMaterialClassMast(MaterialClassMaster materialClassMast)
		{
			int rows = materialClassMastMapper.insert(materialClassMast);
			insertMaterialClassSub(materialClassMast);
			return rows;
		}

		/**
		 * 修改物资分类一级分类
		 *
		 * @param materialClassMast 物资分类一级分类
		 * @return 结果
		 */
		@Transactional
		@Override
		public int updateMaterialClassMast(MaterialClassMaster materialClassMast)
		{
			materialClassSubMapper.deleteMaterialClassSubByClassMastId( materialClassMast.getId().toString());
			insertMaterialClassSub(materialClassMast);
			return materialClassMastMapper.updateById(materialClassMast);
		}

		/**
		 * 批量删除物资分类一级分类
		 *
		 * @param classMastIds 需要删除的物资分类一级分类主键
		 * @return 结果
		 */
		@Transactional
		@Override
		public int deleteMaterialClassMastByClassMastIds(String[] classMastIds)
		{
			List<String> classMastIdList = Arrays.asList(classMastIds);
			for (String classMastId : classMastIdList) {
				materialClassSubMapper.deleteMaterialClassSubByClassMastId(classMastId);
		}
			return materialClassMastMapper.deleteBatchIds(classMastIdList);
		}

		/**
		 * 删除物资分类一级分类信息
		 *
		 * @param classMastId 物资分类一级分类主键
		 * @return 结果
		 */
		@Transactional
		@Override
		public int deleteMaterialClassMastByClassMastId(String classMastId)
		{
			materialClassSubMapper.deleteMaterialClassSubByClassMastId(classMastId);
			return materialClassMastMapper.deleteById(classMastId);
		}

		/**
		 * 新增物资分类-二级分类信息
		 *
		 * @param materialClassMast 物资分类一级分类对象
		 */
		private void insertMaterialClassSub(MaterialClassMaster materialClassMast)
		{
			List<MaterialClassSub> materialClassSubList = materialClassMast.getMaterialClassSubList();
            String classMastId = materialClassMast.getClassMastId();
			Long mastId = materialClassMast.getId();
			String classMastType = materialClassMast.getClassMastType();
			if (StringUtils.isNotNull(materialClassSubList)) {
				for (MaterialClassSub materialClassSub : materialClassSubList) {
                    materialClassSub.setClassMastId(classMastId);
					materialClassSub.setMastId(mastId);
					materialClassSub.setClassMastType(classMastType);
					materialClassSubMapper.insert(materialClassSub);
				}
                    //materialClassMastMapper.batchInsertMaterialClassSub(materialClassSubList);
			}
		}

	private LambdaQueryWrapper<MaterialClassMaster> buildQueryWrapper(MaterialClassMaster query) {
		Map<String, Object> params = query.getParams();
		LambdaQueryWrapper<MaterialClassMaster> lqw = Wrappers.lambdaQuery();
					lqw.eq(StringUtils.isNotBlank(query.getClassMastId()), MaterialClassMaster::getClassMastId, query.getClassMastId());
					lqw.eq(StringUtils.isNotBlank(query.getClassMastType()), MaterialClassMaster::getClassMastType, query.getClassMastType());
					lqw.eq(StringUtils.isNotBlank(query.getClassNameMast()), MaterialClassMaster::getClassNameMast, query.getClassNameMast());
				lqw.orderByDesc(MaterialClassMaster::getCreateTime);
					lqw.eq(query.getDeptId() != null, MaterialClassMaster::getDeptId, query.getDeptId());
					lqw.eq(query.getCreateUserId() != null, MaterialClassMaster::getCreateUserId, query.getCreateUserId());
					lqw.eq(query.getUpdateUserId() != null, MaterialClassMaster::getUpdateUserId, query.getUpdateUserId());
		return lqw;
	}

}
