package com.xinelu.manage.service.materialsinfo.impl;

import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.manage.domain.materialsinfo.MaterialsInfo;
import com.xinelu.manage.domain.propagandamaterials.PropagandaMaterials;
import com.xinelu.manage.dto.materialsinfo.MaterialsInfoDto;
import com.xinelu.manage.mapper.materialsinfo.MaterialsInfoMapper;
import com.xinelu.manage.mapper.propagandamaterials.PropagandaMaterialsMapper;
import com.xinelu.manage.service.materialsinfo.IMaterialsInfoService;
import java.time.LocalDateTime;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

/**
 * 素材信息Service业务层处理
 *
 * @author haown
 * @date 2024-02-26
 */
@Service
public class MaterialsInfoServiceImpl implements IMaterialsInfoService {
    @Resource
    private MaterialsInfoMapper materialsInfoMapper;
	@Resource
	private PropagandaMaterialsMapper propagandaMaterialsMapper;

    /**
     * 查询素材信息
     *
     * @param id 素材信息主键
     * @return 素材信息
     */
    @Override
    public MaterialsInfo selectMaterialsInfoById(Long id) {
        return materialsInfoMapper.selectMaterialsInfoById(id);
    }

    /**
     * 查询素材信息列表
     *
     * @param materialsInfo 素材信息
     * @return 素材信息
     */
    @Override
    public List<MaterialsInfo> selectMaterialsInfoList(MaterialsInfoDto materialsInfo) {
        return materialsInfoMapper.selectMaterialsInfoList(materialsInfo);
    }

    /**
     * 新增素材信息
     *
     * @param materialsInfo 素材信息
     * @return 结果
     */
    @Override
    public int insertMaterialsInfo(MaterialsInfo materialsInfo) {
	    materialsInfo.setDelFlag(0);
	    materialsInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        materialsInfo.setCreateTime(LocalDateTime.now());
        return materialsInfoMapper.insertMaterialsInfo(materialsInfo);
    }

    /**
     * 修改素材信息
     *
     * @param materialsInfo 素材信息
     * @return 结果
     */
    @Override
    public int updateMaterialsInfo(MaterialsInfo materialsInfo) {
	    materialsInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        materialsInfo.setUpdateTime(LocalDateTime.now());
        return materialsInfoMapper.updateMaterialsInfo(materialsInfo);
    }

    /**
     * 批量删除素材信息
     *
     * @param ids 需要删除的素材信息主键
     * @return 结果
     */
    @Override
    public int deleteMaterialsInfoByIds(Long[] ids) {
		// 查询素材是否被使用
	    int used = 0;
	    for(Long id : ids) {
		    PropagandaMaterials queryObj = new PropagandaMaterials();
		    queryObj.setMaterialsId(id);
		    List<PropagandaMaterials> pmList = propagandaMaterialsMapper.selectPropagandaMaterialsList(queryObj);
			if (CollectionUtils.isNotEmpty(pmList)) {
				used++;
			}
	    }
		if (used > 0) {
			throw new ServiceException("选中的素材已在宣教库中被占用不能删除");
		}
        return materialsInfoMapper.deleteMaterialsInfoByIds(ids);
    }

    /**
     * 删除素材信息信息
     *
     * @param id 素材信息主键
     * @return 结果
     */
    @Override
    public int deleteMaterialsInfoById(Long id) {
	    PropagandaMaterials queryObj = new PropagandaMaterials();
	    queryObj.setMaterialsId(id);
	    List<PropagandaMaterials> pmList = propagandaMaterialsMapper.selectPropagandaMaterialsList(queryObj);
	    if (CollectionUtils.isNotEmpty(pmList)) {
		    throw new ServiceException("该素材已在宣教库中被使用不能删除");
	    } else {
		    return materialsInfoMapper.deleteMaterialsInfoById(id);
	    }
    }
}
