package com.ruoyi.mall.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.domain.Vo.*;
import com.ruoyi.mall.mapper.MallSchoolMapper;
import com.ruoyi.mall.mapper.MallSchoolTagMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallAcceptPlanMapper;
import com.ruoyi.mall.domain.MallAcceptPlan;
import com.ruoyi.mall.service.IMallAcceptPlanService;

import javax.annotation.Resource;

/**
 * 招生计划Service业务层处理
 *
 * @author Lsm
 * @date 2023-10-19
 */
@Service
public class MallAcceptPlanServiceImpl implements IMallAcceptPlanService
{
    @Resource
    private MallAcceptPlanMapper mallAcceptPlanMapper;

    @Resource
    private MallSchoolMapper mallSchoolMapper;

    @Resource
    private MallSchoolTagMapper mallSchoolTagMapper;

    /**
     * 查询招生计划
     *
     * @param acceptPlanId 招生计划主键
     * @return 招生计划
     */
    @Override
    public MallAcceptPlan selectMallAcceptPlanByAcceptPlanId(Long acceptPlanId)
    {
        return mallAcceptPlanMapper.selectMallAcceptPlanByAcceptPlanId(acceptPlanId);
    }

    /**
     * 查询招生计划列表
     *
     * @param mallAcceptPlan 招生计划
     * @return 招生计划
     */
    @Override
    public List<MallAcceptPlan> selectMallAcceptPlanList(MallAcceptPlan mallAcceptPlan)
    {
        return mallAcceptPlanMapper.selectMallAcceptPlanList(mallAcceptPlan);
    }

    /**
     * 新增招生计划
     *
     * @param mallAcceptPlan 招生计划
     * @return 结果
     */
    @Override
    public int insertMallAcceptPlan(MallAcceptPlan mallAcceptPlan)
    {
        mallAcceptPlan.setCreateAt(DateUtils.getTime());
        mallAcceptPlan.setDelFlag("0");
        return mallAcceptPlanMapper.insertMallAcceptPlan(mallAcceptPlan);
    }

    /**
     * 修改招生计划
     *
     * @param mallAcceptPlan 招生计划
     * @return 结果
     */
    @Override
    public int updateMallAcceptPlan(MallAcceptPlan mallAcceptPlan)
    {
        mallAcceptPlan.setUpdateAt(DateUtils.getTime());
        return mallAcceptPlanMapper.updateMallAcceptPlan(mallAcceptPlan);
    }

    /**
     * 批量删除招生计划
     *
     * @param acceptPlanIds 需要删除的招生计划主键
     * @return 结果
     */
    @Override
    public int deleteMallAcceptPlanByAcceptPlanIds(Long[] acceptPlanIds)
    {
        return mallAcceptPlanMapper.deleteMallAcceptPlanByAcceptPlanIds(acceptPlanIds);
    }

    /**
     * 删除招生计划信息
     *
     * @param acceptPlanId 招生计划主键
     * @return 结果
     */
    @Override
    public int deleteMallAcceptPlanByAcceptPlanId(Long acceptPlanId)
    {
        return mallAcceptPlanMapper.deleteMallAcceptPlanByAcceptPlanId(acceptPlanId);
    }

    /** 查询新招专业或所有新招  用于志愿填报自主选择*/
    @Override
    public List<MajorItemVo> newAcceptList(String subjectType,String newAcceptType, Long batchId) {
        return mallAcceptPlanMapper.newAcceptList(subjectType,newAcceptType,batchId);
    }

    /**志愿添加 查询专业 人数 学费*/
    @Override
    public List<MajorItemVo> majorList(MajorItemVo vo) {
        return mallAcceptPlanMapper.majorList(vo);
    }

    /** 招生变化*/
    @Override
    public AcceptChangeVo getChangeBatch(Integer pageNum,Integer pageSize,Long batchId, MallMemberUser user) {
        AcceptChangeVo result = new AcceptChangeVo();
        result.setBatchId(batchId);
        //获取今年a和去年b的年
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        Calendar cc = Calendar.getInstance();
        cc.setTime(new Date());
        cc.add(Calendar.YEAR, -1);
        Date y = cc.getTime();
        String by =  format.format(y);
        String ay = format.format(new Date());
        AcceptChangeItemVo a = mallAcceptPlanMapper.getSumNumByBatchId(ay,batchId,user.getSubjectType());
        AcceptChangeItemVo b = mallAcceptPlanMapper.getSumNumByBatchId(by,batchId,user.getSubjectType());
        AcceptChangeItemVo c = new AcceptChangeItemVo();
        //allNum outProvinceNum  provinceNum localNum
        if(null != a && null !=b){
            c.setAllNum(a.getAllNum() - b.getAllNum());
            c.setOutProvinceNum(a.getOutProvinceNum()-b.getOutProvinceNum());
            c.setProvinceNum(a.getProvinceNum()-b.getProvinceNum());
            c.setLocalNum(a.getLocalNum()-b.getLocalNum());
        }
        result.setA(a);
        result.setB(b);
        result.setC(c);
        //// 加入新招院校列表
        //List<Long> schoolIds = mallAcceptPlanMapper.getNewAcceptSchools(batchId,user.getSubjectType());
        //List<SchoolVO> schoolVOS = new ArrayList<>();
        ////有新招院校 / 无新招院校
        //if(null != schoolIds && schoolIds.size()>0){
        //    PageHelper.startPage(pageNum, pageSize);
        //    schoolVOS = mallSchoolMapper.selectSchoolVoByIds(schoolIds);
        //    for (SchoolVO schoolVO : schoolVOS) {
        //        //填充学校标签
        //        if (StringUtils.isNotBlank(schoolVO.getTags())) {
        //            List<String> list = mallSchoolTagMapper.selectTagByTagId(schoolVO.getTags());
        //            schoolVO.setTagList(list);
        //        }
        //    }
        //
        //}
        PageHelper.startPage(pageNum, pageSize);
        List<SchoolVO> schoolVOList = mallSchoolMapper.selectNewSchoolList(batchId, user.getSubjectType());
        PageInfo<SchoolVO> pageInfo = new PageInfo<>(schoolVOList);
        result.setSchoolList(pageInfo);
        return result;
    }
}
