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.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.Bo.TargetSchoolBO;
import com.ruoyi.mall.domain.MallAcceptRateSetting;
import com.ruoyi.mall.domain.MallMemberCollectSchool;
import com.ruoyi.mall.domain.Vo.MallMemberCollectSchoolVo;
import com.ruoyi.mall.domain.Vo.MallMemberTargetSchoolVo;
import com.ruoyi.mall.mapper.MallAcceptRateSettingMapper;
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.MallMemberTargetSchoolMapper;
import com.ruoyi.mall.domain.MallMemberTargetSchool;
import com.ruoyi.mall.service.IMallMemberTargetSchoolService;

import javax.annotation.Resource;

/**
 * 目标学校Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-26
 */
@Service
public class MallMemberTargetSchoolServiceImpl implements IMallMemberTargetSchoolService
{
    @Resource
    private MallMemberTargetSchoolMapper mallMemberTargetSchoolMapper;
    @Resource
    private MallAcceptRateSettingMapper mallAcceptRateSettingMapper;
    @Resource
    private MallSchoolTagMapper mallSchoolTagMapper;

    /**
     * 查询目标学校
     *
     * @param targetId 目标学校主键
     * @return 目标学校
     */
    @Override
    public MallMemberTargetSchool selectMallMemberTargetSchoolByTargetId(Long targetId)
    {
        return mallMemberTargetSchoolMapper.selectMallMemberTargetSchoolByTargetId(targetId);
    }

    /**
     * 查询目标学校列表
     *
     * @param mallMemberTargetSchool 目标学校
     * @return 目标学校
     */
    @Override
    public List<MallMemberTargetSchool> selectMallMemberTargetSchoolList(MallMemberTargetSchool mallMemberTargetSchool)
    {
        return mallMemberTargetSchoolMapper.selectMallMemberTargetSchoolList(mallMemberTargetSchool);
    }
    /**
     * 查询 我的-目标学校列表
     * @param mallMemberTargetSchool 目标学校
     * @return 用户学校目标集合 包含学校详情
     */
    @Override
    public List<MallMemberTargetSchoolVo> selectVoForMineList(MallMemberTargetSchool mallMemberTargetSchool) {
        List<MallMemberTargetSchoolVo> result = mallMemberTargetSchoolMapper.selectVoForMineList(mallMemberTargetSchool);


       for (MallMemberTargetSchoolVo mallMemberTargetSchoolVo : result) {
            //tags
            if (StringUtils.isNotBlank(mallMemberTargetSchoolVo.getTags())) {
                List<String> list = mallSchoolTagMapper.selectTagByTagId(mallMemberTargetSchoolVo.getTags());
                mallMemberTargetSchoolVo.setTagList(list);
            }
        }
        return result;
    }

    /**
     * 新增目标学校
     *
     * @param mallMemberTargetSchool 目标学校
     * @return 结果
     */
    @Override
    public int insertMallMemberTargetSchool(MallMemberTargetSchool mallMemberTargetSchool)
    {
        return mallMemberTargetSchoolMapper.insertMallMemberTargetSchool(mallMemberTargetSchool);
    }

    /**
     * 修改目标学校
     *
     * @param mallMemberTargetSchool 目标学校
     * @return 结果
     */
    @Override
    public int updateMallMemberTargetSchool(MallMemberTargetSchool mallMemberTargetSchool)
    {
        return mallMemberTargetSchoolMapper.updateMallMemberTargetSchool(mallMemberTargetSchool);
    }

    /**
     * 批量删除目标学校
     *
     * @param targetIds 需要删除的目标学校主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberTargetSchoolByTargetIds(Long[] targetIds)
    {
        return mallMemberTargetSchoolMapper.deleteMallMemberTargetSchoolByTargetIds(targetIds);
    }

    /**
     * 删除目标学校信息
     *
     * @param targetId 目标学校主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberTargetSchoolByTargetId(Long targetId)
    {
        return mallMemberTargetSchoolMapper.deleteMallMemberTargetSchoolByTargetId(targetId);
    }

    /**
     * 设置/取消目标学校
     * @param targetSchoolBO 学校信息
     * @return 结果
     */
    @Override
    public AjaxResult setTargetSchool(TargetSchoolBO targetSchoolBO) {
        Long id = targetSchoolBO.getId();
        Long schoolId = targetSchoolBO.getSchoolId();
        Long batchId = targetSchoolBO.getBatchId();

        String now = DateUtils.getTime();

        int status = 0;

        // 根据学校id查询查询用户收藏表
        MallMemberTargetSchool mallMemberTargetSchool = mallMemberTargetSchoolMapper.selectTargetSchoolById(id, schoolId, batchId);

        if (ObjectUtils.isNotEmpty(mallMemberTargetSchool)){
            String delFlag = mallMemberTargetSchool.getDelFlag();
            if (delFlag.equals("1")){
                // 设为目标
                mallMemberTargetSchool.setDelFlag("0");
                mallMemberTargetSchool.setUpdateAt(now);
                status = 1;
            }else{
                // 取消设定目标
                mallMemberTargetSchool.setDelFlag("1");
                mallMemberTargetSchool.setUpdateAt(now);
                status = 0;
            }
            mallMemberTargetSchoolMapper.updateMallMemberTargetSchool(mallMemberTargetSchool);
        }else{
            // 新增收藏
            MallMemberTargetSchool newTargetSchool = new MallMemberTargetSchool();
            newTargetSchool.setSchoolId(schoolId);
            newTargetSchool.setBatchId(batchId);
            newTargetSchool.setId(id);
            newTargetSchool.setCreateAt(now);
            newTargetSchool.setDelFlag("0");
            mallMemberTargetSchoolMapper.insertMallMemberTargetSchool(newTargetSchool);
            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 getTargetStatus(Long id, Long schoolId, Long batchId) {
        MallMemberTargetSchool mallMemberTargetSchool = mallMemberTargetSchoolMapper.selectTargetSchoolStatus(id, schoolId, batchId);
        int status = 0;
        if(ObjectUtils.isNotEmpty(mallMemberTargetSchool)){
            status = 1;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("status", status);

        return AjaxResult.success(map);
    }
}
