package com.ruoyi.mall.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.Vo.SchoolRulesVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallSchoolAcceptRulesMapper;
import com.ruoyi.mall.domain.MallSchoolAcceptRules;
import com.ruoyi.mall.service.IMallSchoolAcceptRulesService;

import javax.annotation.Resource;

/**
 * 招生简章Service业务层处理
 *
 * @author lsm
 * @date 2023-10-19
 */
@Service
public class MallSchoolAcceptRulesServiceImpl implements IMallSchoolAcceptRulesService
{
    @Resource
    private MallSchoolAcceptRulesMapper mallSchoolAcceptRulesMapper;

    /**
     * 查询招生简章
     *
     * @param acceptRulesId 招生简章主键
     * @return 招生简章
     */
    @Override
    public MallSchoolAcceptRules selectMallSchoolAcceptRulesByAcceptRulesId(Long acceptRulesId)
    {
        return mallSchoolAcceptRulesMapper.selectMallSchoolAcceptRulesByAcceptRulesId(acceptRulesId);
    }

    /**
     * 查询招生简章列表
     *
     * @param mallSchoolAcceptRules 招生简章
     * @return 招生简章
     */
    @Override
    public List<MallSchoolAcceptRules> selectMallSchoolAcceptRulesList(MallSchoolAcceptRules mallSchoolAcceptRules)
    {
        return mallSchoolAcceptRulesMapper.selectMallSchoolAcceptRulesList(mallSchoolAcceptRules);
    }

    /**
     * 新增招生简章
     *
     * @param mallSchoolAcceptRules 招生简章
     * @return 结果
     */
    @Override
    public int insertMallSchoolAcceptRules(MallSchoolAcceptRules mallSchoolAcceptRules)
    {
        mallSchoolAcceptRules.setView(0);
        mallSchoolAcceptRules.setCreateAt(DateUtils.getTime());
        mallSchoolAcceptRules.setDisplayStatus(0);
        mallSchoolAcceptRules.setDelFlag("0");
        return mallSchoolAcceptRulesMapper.insertMallSchoolAcceptRules(mallSchoolAcceptRules);
    }

    /**
     * 修改招生简章
     *
     * @param mallSchoolAcceptRules 招生简章
     * @return 结果
     */
    @Override
    public int updateMallSchoolAcceptRules(MallSchoolAcceptRules mallSchoolAcceptRules)
    {
        return mallSchoolAcceptRulesMapper.updateMallSchoolAcceptRules(mallSchoolAcceptRules);
    }

    /**
     * 批量删除招生简章
     *
     * @param acceptRulesIds 需要删除的招生简章主键
     * @return 结果
     */
    @Override
    public int deleteMallSchoolAcceptRulesByAcceptRulesIds(Long[] acceptRulesIds)
    {
        return mallSchoolAcceptRulesMapper.deleteMallSchoolAcceptRulesByAcceptRulesIds(acceptRulesIds);
    }

    /**
     * 删除招生简章信息
     *
     * @param acceptRulesId 招生简章主键
     * @return 结果
     */
    @Override
    public int deleteMallSchoolAcceptRulesByAcceptRulesId(Long acceptRulesId)
    {
        return mallSchoolAcceptRulesMapper.deleteMallSchoolAcceptRulesByAcceptRulesId(acceptRulesId);
    }

    /**
     * 招生简章列表
     * @param searchValue 搜索词
     * @param pageNum 页码
     * @param pageSize 每页个数
     * @return 结果
     */
    @Override
    public PageInfo<SchoolRulesVO> selectRulesList(String searchValue, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SchoolRulesVO> list = mallSchoolAcceptRulesMapper.selectRulesList(searchValue);
        if (!list.isEmpty()){
            for (SchoolRulesVO schoolRulesVO : list) {
                if (schoolRulesVO.getView() >= 100){
                    schoolRulesVO.setHot(1);
                }else{
                    schoolRulesVO.setHot(0);
                }
            }
        }
        return new PageInfo<>(list);
    }

    /**
     * 简章详情
     * @param acceptRulesId 简章id
     * @return 结果
     */
    @Override
    public SchoolRulesVO selectRulesDetail(Long acceptRulesId) {
        SchoolRulesVO schoolRulesVO = mallSchoolAcceptRulesMapper.selectRulesDetail(acceptRulesId);

        // 更新浏览量
        if (ObjectUtils.isNotEmpty(schoolRulesVO)){
            MallSchoolAcceptRules mallSchoolAcceptRules = new MallSchoolAcceptRules();
            mallSchoolAcceptRules.setAcceptRulesId(schoolRulesVO.getAcceptRulesId());
            mallSchoolAcceptRules.setView(schoolRulesVO.getView() + 1);
            mallSchoolAcceptRules.setUpdateAt(DateUtils.getTime());
            mallSchoolAcceptRulesMapper.updateMallSchoolAcceptRules(mallSchoolAcceptRules);
        }


        return schoolRulesVO;
    }
}
