package com.ruoyi.mall.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.MallFacultyInfo;
import com.ruoyi.mall.domain.MallMajorClass;
import com.ruoyi.mall.mapper.MallFacultyInfoMapper;
import com.ruoyi.mall.mapper.MallMajorClassMapper;
import com.ruoyi.mall.service.IMallFacultyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 学院介绍Service业务层处理
 *
 * @author Lsm
 * @date 2023-10-18
 */
@Service
public class MallFacultyInfoServiceImpl implements IMallFacultyInfoService
{
    @Resource
    private MallFacultyInfoMapper mallFacultyInfoMapper;

    @Resource
    private MallMajorClassMapper mallMajorClassMapper;

    /**
     * 查询学院介绍
     *
     * @param facultyId 学院介绍主键
     * @return 学院介绍
     */
    @Override
    public MallFacultyInfo selectMallFacultyInfoByFacultyId(Long facultyId)
    {
        return mallFacultyInfoMapper.selectMallFacultyInfoByFacultyId(facultyId);
    }

    /**
     * 查询学院介绍列表
     *
     * @param mallFacultyInfo 学院介绍
     * @return 学院介绍
     */
    @Override
    public List<MallFacultyInfo> selectMallFacultyInfoList(MallFacultyInfo mallFacultyInfo)
    {
        List<MallFacultyInfo> list = mallFacultyInfoMapper.selectMallFacultyInfoList(mallFacultyInfo);
        if (!list.isEmpty()){
            for (MallFacultyInfo facultyInfo : list) {
                if (StringUtils.isNotBlank(facultyInfo.getFacultyMajor())){
                    List<String> majorArr = mallMajorClassMapper.selectMajorStr(facultyInfo.getFacultyMajor());
                    if (majorArr.size() > 0){
                        StringBuilder sb = new StringBuilder();
                        for (String s : majorArr) {
                            sb.append(s).append(",");
                        }
                        String result = sb.toString();
                        facultyInfo.setMajorStr(result.substring(0, result.length()-1));;
                    }
                }

            }
        }
        return list;
    }

    /**
     * 新增学院介绍
     *
     * @param mallFacultyInfo 学院介绍
     * @return 结果
     */
    @Override
    public int insertMallFacultyInfo(MallFacultyInfo mallFacultyInfo)
    {
        mallFacultyInfo.setCreateAt(DateUtils.getTime());
        mallFacultyInfo.setDelFlag("0");
        return mallFacultyInfoMapper.insertMallFacultyInfo(mallFacultyInfo);
    }

    /**
     * 修改学院介绍
     *
     * @param mallFacultyInfo 学院介绍
     * @return 结果
     */
    @Override
    public int updateMallFacultyInfo(MallFacultyInfo mallFacultyInfo)
    {
        mallFacultyInfo.setUpdateAt(DateUtils.getTime());
        return mallFacultyInfoMapper.updateMallFacultyInfo(mallFacultyInfo);
    }

    /**
     * 批量删除学院介绍
     *
     * @param facultyIds 需要删除的学院介绍主键
     * @return 结果
     */
    @Override
    public int deleteMallFacultyInfoByFacultyIds(Long[] facultyIds)
    {
        return mallFacultyInfoMapper.deleteMallFacultyInfoByFacultyIds(facultyIds);
    }

    /**
     * 删除学院介绍信息
     *
     * @param facultyId 学院介绍主键
     * @return 结果
     */
    @Override
    public int deleteMallFacultyInfoByFacultyId(Long facultyId)
    {
        return mallFacultyInfoMapper.deleteMallFacultyInfoByFacultyId(facultyId);
    }
}
