package com.ruoyi.mall.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
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.mall.domain.Bo.ExpectBO;
import com.ruoyi.mall.domain.MallMemberCollectSchool;
import com.ruoyi.mall.domain.Vo.SchoolVO;
import com.ruoyi.mall.mapper.MallSchoolTagMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallMemberExpectMapper;
import com.ruoyi.mall.domain.MallMemberExpect;
import com.ruoyi.mall.service.IMallMemberExpectService;

import javax.annotation.Resource;

/**
 * 我的意愿学校记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-03
 */
@Service
public class MallMemberExpectServiceImpl implements IMallMemberExpectService
{
    @Resource
    private MallMemberExpectMapper mallMemberExpectMapper;

    @Resource
    private MallSchoolTagMapper mallSchoolTagMapper;

    /**
     * 查询我的意愿学校记录
     *
     * @param expectId 我的意愿学校记录主键
     * @return 我的意愿学校记录
     */
    @Override
    public MallMemberExpect selectMallMemberExpectByExpectId(Long expectId)
    {
        return mallMemberExpectMapper.selectMallMemberExpectByExpectId(expectId);
    }

    /**
     * 查询我的意愿学校记录列表
     *
     * @param mallMemberExpect 我的意愿学校记录
     * @return 我的意愿学校记录
     */
    @Override
    public List<MallMemberExpect> selectMallMemberExpectList(MallMemberExpect mallMemberExpect)
    {
        return mallMemberExpectMapper.selectMallMemberExpectList(mallMemberExpect);
    }

    /**
     * 新增我的意愿学校记录
     *
     * @param mallMemberExpect 我的意愿学校记录
     * @return 结果
     */
    @Override
    public int insertMallMemberExpect(MallMemberExpect mallMemberExpect)
    {
        mallMemberExpect.setCreateTime(DateUtils.getNowDate());
        return mallMemberExpectMapper.insertMallMemberExpect(mallMemberExpect);
    }

    /**
     * 修改我的意愿学校记录
     *
     * @param mallMemberExpect 我的意愿学校记录
     * @return 结果
     */
    @Override
    public int updateMallMemberExpect(MallMemberExpect mallMemberExpect)
    {
        mallMemberExpect.setUpdateTime(DateUtils.getNowDate());
        return mallMemberExpectMapper.updateMallMemberExpect(mallMemberExpect);
    }

    /**
     * 批量删除我的意愿学校记录
     *
     * @param expectIds 需要删除的我的意愿学校记录主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberExpectByExpectIds(Long[] expectIds)
    {
        return mallMemberExpectMapper.deleteMallMemberExpectByExpectIds(expectIds);
    }

    /**
     * 删除我的意愿学校记录信息
     *
     * @param expectId 我的意愿学校记录主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberExpectByExpectId(Long expectId)
    {
        return mallMemberExpectMapper.deleteMallMemberExpectByExpectId(expectId);
    }

    /**
     * 更改意向状态
     * @param expectBO 意向信息
     * @return 结果
     */
    @Override
    public AjaxResult changeExpectStatus(ExpectBO expectBO) {

        Long id = expectBO.getId();
        Long schoolId = expectBO.getSchoolId();
        Long batchId = expectBO.getBatchId();

        Date nowDate = DateUtils.getNowDate();

        int status = 0;

        // 根据学校id查询查询用户收藏表
        MallMemberExpect mallMemberExpect = mallMemberExpectMapper.selectMallMemberExpectByUserId(id, schoolId, batchId);

        if (ObjectUtils.isNotEmpty(mallMemberExpect)){
            Integer delFlag = mallMemberExpect.getDelFlag();
            if (delFlag == 1){
                // 收藏
                mallMemberExpect.setDelFlag(0);
                mallMemberExpect.setUpdateTime(nowDate);
                status = 1;
            }else{
                // 取消收藏
                mallMemberExpect.setDelFlag(1);
                mallMemberExpect.setUpdateTime(nowDate);
                status = 0;
            }
            mallMemberExpectMapper.updateMallMemberExpect(mallMemberExpect);

        }else{
            // 新增收藏
            MallMemberExpect newMemberExpect = new MallMemberExpect();
            newMemberExpect.setSchoolId(schoolId);
            newMemberExpect.setBatchId(batchId);
            newMemberExpect.setId(id);
            newMemberExpect.setCreateTime(nowDate);
            newMemberExpect.setDelFlag(0);
            mallMemberExpectMapper.insertMallMemberExpect(newMemberExpect);

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

    /**
     * 意向学校列表
     * @param id 用户id
     * @param batchId 批次id
     * @param pageNum
     * @param pageSize
     * @return 结果
     */
    @Override
    public PageInfo<SchoolVO> selectExpectSchoolList(Long id, Long batchId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SchoolVO> schoolVOList = mallMemberExpectMapper.selectExpectSchoolList(id, batchId);
       if (!schoolVOList.isEmpty()){
           for (SchoolVO schoolVO : schoolVOList) {
               if (StringUtils.isNotBlank(schoolVO.getTags())) {
                   // 查询标签列表
                   List<String> tagList = mallSchoolTagMapper.selectTagByTagId(schoolVO.getTags());
                   schoolVO.setTagList(tagList);
               }
           }
       }
       return new PageInfo<>(schoolVOList);

    }
}
