package com.xinelu.manage.service.trainingcategory.Impl;

import com.xinelu.common.config.XinELuConfig;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.codes.GenerateSystemCodeUtil;
import com.xinelu.common.utils.file.FileUtils;
import com.xinelu.manage.domain.trainingcategory.TrainingCategory;
import com.xinelu.manage.mapper.trainingcategory.TrainingCategoryMapper;
import com.xinelu.manage.service.trainingcategory.ITrainingCategoryService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 学习培训分类Service业务层处理
 *
 * @author xinyilu
 * @date 2023-04-20
 */
@Service
public class TrainingCategoryServiceImpl implements ITrainingCategoryService {
    @Resource
    private TrainingCategoryMapper trainingCategoryMapper;
    @Resource
    private XinELuConfig xinYiLuConfig;
    @Resource
    private GenerateSystemCodeUtil generateSystemCodeUtil;

    /**
     * 查询学习培训分类
     *
     * @param id 学习培训分类主键
     * @return 学习培训分类
     */
    @Override
    public TrainingCategory selectTrainingCategoryById(Long id) {
        return trainingCategoryMapper.selectTrainingCategoryById(id);
    }

    /**
     * 查询学习培训分类列表
     *
     * @param trainingCategory 学习培训分类
     * @return 学习培训分类
     */
    @Override
    public List<TrainingCategory> selectTrainingCategoryList(TrainingCategory trainingCategory) {
        return trainingCategoryMapper.selectTrainingCategoryList(trainingCategory);
    }

    /**
     * 新增学习培训分类
     *
     * @param trainingCategory 学习培训分类
     * @return 结果
     */
    @Override
    public int insertTrainingCategory(TrainingCategory trainingCategory) {
        trainingCategory.setCreateTime(LocalDateTime.now());
        trainingCategory.setCreateBy(SecurityUtils.getUsername());
        trainingCategory.setTrainingCategoryCode(Constants.TRAINING_CATEGORY + generateSystemCodeUtil.generateSystemCode(Constants.TRAINING_CATEGORY));
        return trainingCategoryMapper.insertTrainingCategory(trainingCategory);
    }

    /**
     * 修改学习培训分类
     *
     * @param trainingCategory 学习培训分类
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult updateTrainingCategory(TrainingCategory trainingCategory) {
        TrainingCategory initial = trainingCategoryMapper.selectTrainingCategoryById(trainingCategory.getId());
        if (Objects.isNull(initial)) {
            return AjaxResult.error("当前分类信息不存在，无法修改，请联系管理员！");
        }
        trainingCategory.setUpdateTime(LocalDateTime.now());
        trainingCategory.setUpdateBy(SecurityUtils.getUsername());
        //删除原有上传的分类图片
        if (StringUtils.isNotBlank(trainingCategory.getTrainingCategoryPictureUrl()) && StringUtils.isNotBlank(initial.getTrainingCategoryPictureUrl())
                && !trainingCategory.getTrainingCategoryPictureUrl().equals(initial.getTrainingCategoryPictureUrl())) {
            FileUtils.deleteSystemFile(initial.getTrainingCategoryPictureUrl());
        }
        return AjaxResult.success(trainingCategoryMapper.updateTrainingCategory(trainingCategory));
    }

    /**
     * 批量删除学习培训分类
     *
     * @param ids 需要删除的学习培训分类主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult deleteTrainingCategoryByIds(Long[] ids) {
        if (Objects.isNull(ids)) {
            return AjaxResult.error("请选择需要删除的信息！");
        }
        List<TrainingCategory> trainingCategories = trainingCategoryMapper.selectTrainingCategoryByIds(ids);
        if (CollectionUtils.isEmpty(trainingCategories)) {
            return AjaxResult.success();
        }
        trainingCategoryMapper.deleteTrainingCategoryByIds(ids);
        for (TrainingCategory trainingCategory : trainingCategories) {
            if (StringUtils.isBlank(trainingCategory.getTrainingCategoryPictureUrl())) {
                continue;
            }
            FileUtils.deleteSystemFile(trainingCategory.getTrainingCategoryPictureUrl());
        }
        return AjaxResult.success();
    }

    /**
     * 上传文件
     *
     * @param multipartFile 文件
     * @return AjaxResult
     * @throws Exception 异常信息
     */
    @Override
    public AjaxResult uploadTrainingCategoryPicture(MultipartFile multipartFile) throws Exception {
        String uploadPathUrl = XinELuConfig.getProfile() + xinYiLuConfig.getTrainingCategoryPictureUrl();
        return FileUtils.uploadImages(uploadPathUrl, multipartFile);
    }
}