package com.ruoyi.mall.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.mall.domain.Bo.CollectSchoolBO;
import com.ruoyi.mall.domain.MallAcceptRateSetting;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.domain.MallSchoolAccept;
import com.ruoyi.mall.domain.Vo.MallMemberCollectSchoolVo;
import com.ruoyi.mall.domain.Vo.SchoolVO;
import com.ruoyi.mall.mapper.*;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.domain.MallMemberCollectSchool;
import com.ruoyi.mall.service.IMallMemberCollectSchoolService;

import javax.annotation.Resource;

/**
 * 用户学校收藏Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-26
 */
@Service
public class MallMemberCollectSchoolServiceImpl implements IMallMemberCollectSchoolService
{
    @Resource
    private MallMemberCollectSchoolMapper mallMemberCollectSchoolMapper;
    @Resource
    private MallSchoolTagMapper mallSchoolTagMapper;
    @Resource
    private MallMemberUserMapper mallMemberUserMapper;
    @Resource
    private MallAcceptRateSettingMapper mallAcceptRateSettingMapper;
    @Resource
    private MallCountMethodServiceImpl mallCountMethodService;
    @Resource
    private MallSchoolAcceptMapper mallSchoolAcceptMapper;

    /**
     * 查询用户学校收藏
     *
     * @param collectSchoolId 用户学校收藏主键
     * @return 用户学校收藏
     */
    @Override
    public MallMemberCollectSchool selectMallMemberCollectSchoolByCollectSchoolId(Long collectSchoolId)
    {
        return mallMemberCollectSchoolMapper.selectMallMemberCollectSchoolByCollectSchoolId(collectSchoolId);
    }

    /**
     * 查询用户学校收藏列表
     *
     * @param mallMemberCollectSchool 用户学校收藏
     * @return 用户学校收藏
     */
    @Override
    public List<MallMemberCollectSchool> selectMallMemberCollectSchoolList(MallMemberCollectSchool mallMemberCollectSchool)
    {
        return mallMemberCollectSchoolMapper.selectMallMemberCollectSchoolList(mallMemberCollectSchool);
    }


    /**
     * 查询 我的-收藏学校列表
     * @param mallMemberCollectSchool 用户学校收藏
     * @return 用户学校收藏集合 包含学校详情
     */
    @Override
    public List<MallMemberCollectSchoolVo> selectVoForMineList(MallMemberCollectSchool mallMemberCollectSchool)
    {
        List<MallMemberCollectSchoolVo> result = mallMemberCollectSchoolMapper.selectVoForMineList(mallMemberCollectSchool);


        // 查询录取率设定表
        //List<MallAcceptRateSetting> rateSettings = mallAcceptRateSettingMapper.selectRateSettingList();
        for (MallMemberCollectSchoolVo mallMemberCollectSchoolVo : result) {
            //tags
            if (StringUtils.isNotBlank(mallMemberCollectSchoolVo.getTags())) {
                List<String> list = mallSchoolTagMapper.selectTagByTagId(mallMemberCollectSchoolVo.getTags());
                mallMemberCollectSchoolVo.setTagList(list);
            }

            // 学校录取专业最低分
            Map<String, Object> r = new HashMap<>();
            MallSchoolAccept mallSchoolAccept = mallSchoolAcceptMapper.selectMinReferenceBySchoolId(mallMemberCollectSchoolVo.getSchoolId(), mallMemberCollectSchool.getBatchId(), mallMemberCollectSchool.getSubjectType());
            if (ObjectUtils.isNotEmpty(mallSchoolAccept)){
                r = mallCountMethodService.getAcceptRateForReport(mallSchoolAccept.getReference(),  mallSchoolAccept.getAcceptRuleId(), mallMemberCollectSchool.getCultureScore(), mallMemberCollectSchool.getMajorScore());
            }else{
                r.put("acceptRate", 0);
                r.put("rateType", -2);
            }
             if (!r.isEmpty() && new BigDecimal(r.get("acceptRate").toString()).compareTo(BigDecimal.ZERO) > 0 ){
                mallMemberCollectSchoolVo.setAcceptRate(new BigDecimal(r.get("acceptRate").toString()));
                mallMemberCollectSchoolVo.setShowAcceptRate(mallMemberCollectSchoolVo.getAcceptRate().multiply(new BigDecimal("100")).setScale(0, RoundingMode.HALF_UP) + "%");
            }
        }
        return result;
    }
    /**
     * 新增用户学校收藏
     *
     * @param mallMemberCollectSchool 用户学校收藏
     * @return 结果
     */
    @Override
    public int insertMallMemberCollectSchool(MallMemberCollectSchool mallMemberCollectSchool)
    {
        return mallMemberCollectSchoolMapper.insertMallMemberCollectSchool(mallMemberCollectSchool);
    }

    /**
     * 修改用户学校收藏
     *
     * @param mallMemberCollectSchool 用户学校收藏
     * @return 结果
     */
    @Override
    public int updateMallMemberCollectSchool(MallMemberCollectSchool mallMemberCollectSchool)
    {
        return mallMemberCollectSchoolMapper.updateMallMemberCollectSchool(mallMemberCollectSchool);
    }

    /**
     * 批量删除用户学校收藏
     *
     * @param collectSchoolIds 需要删除的用户学校收藏主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberCollectSchoolByCollectSchoolIds(Long[] collectSchoolIds)
    {
        return mallMemberCollectSchoolMapper.deleteMallMemberCollectSchoolByCollectSchoolIds(collectSchoolIds);
    }

    /**
     * 删除用户学校收藏信息
     *
     * @param collectSchoolId 用户学校收藏主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberCollectSchoolByCollectSchoolId(Long collectSchoolId)
    {
        return mallMemberCollectSchoolMapper.deleteMallMemberCollectSchoolByCollectSchoolId(collectSchoolId);
    }

    /**
     * 收藏/取消收藏学校
     * @param collectSchoolBO 参数类
     * @return 结果
     */
    @Override
    public AjaxResult collectSchool(CollectSchoolBO collectSchoolBO) {
        Long id = collectSchoolBO.getId();
        Long schoolId = collectSchoolBO.getSchoolId();
        Long batchId = collectSchoolBO.getBatchId();

        String now = DateUtils.getTime();

        int status = 0;

        // 根据学校id查询查询用户收藏表
        MallMemberCollectSchool mallMemberCollectSchool = mallMemberCollectSchoolMapper.selectCollectByIdAndSchoolId(schoolId, id, batchId);

        if (ObjectUtils.isNotEmpty(mallMemberCollectSchool)){
            String delFlag = mallMemberCollectSchool.getDelFlag();
            if (delFlag.equals("1")){
                // 收藏
                mallMemberCollectSchool.setDelFlag("0");
                mallMemberCollectSchool.setUpdateAt(now);
                status = 1;
            }else{
                // 取消收藏
                mallMemberCollectSchool.setDelFlag("1");
                mallMemberCollectSchool.setUpdateAt(now);
                status = 0;
            }
            mallMemberCollectSchoolMapper.updateMallMemberCollectSchool(mallMemberCollectSchool);

        }else{
            // 新增收藏

            MallMemberCollectSchool newCollect = new MallMemberCollectSchool();
            newCollect.setSchoolId(schoolId);
            newCollect.setBatchId(batchId);
            newCollect.setId(id);
            newCollect.setCreateAt(now);
            newCollect.setDelFlag("0");
            mallMemberCollectSchoolMapper.insertMallMemberCollectSchool(newCollect);

            status = 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("status", status);
        return AjaxResult.success(map);
    }

    /**
     * 收藏状态
     * @param id 用户id
     * @param schoolId 学校id
     * @param batchId 批次id
     * @return
     */
    @Override
    public AjaxResult getCollectStatus(Long id, Long schoolId, Long batchId) {
        int status = 0;
        // 根据学校id查询查询用户收藏表
        MallMemberCollectSchool mallMemberCollectSchool = mallMemberCollectSchoolMapper.selectCollectByIdAndSchoolId(schoolId, id, batchId);
        if (ObjectUtils.isNotEmpty(mallMemberCollectSchool) && mallMemberCollectSchool.getDelFlag().equals("0")){
            status =  1;
        }
        Map<String, Object> result = new HashMap<>();
        result.put("status", status);
        return AjaxResult.success(result);
    }

    /**
     *  学校收藏列表
     * @param id 用户id
     * @param pageNum 页码
     * @param pageSize 每页个数
     * @return 结果
     */
    @Override
    public PageInfo<SchoolVO> selectCollectSchoolList(Long id, Integer pageNum, Integer pageSize) {
        return null;
    }
}
