package com.ym.mec.biz.service.impl;

import com.alibaba.fastjson.JSON;
import com.ym.mec.biz.dal.dto.MusicGroupRegRespDto;
import com.ym.mec.biz.dal.dto.MusicGroupSubjectGoodsAndInfoDto;
import com.ym.mec.biz.dal.entity.*;
import com.ym.mec.biz.dal.enums.GoodsType;
import com.ym.mec.biz.service.*;
import com.ym.mec.util.collection.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.mec.biz.dal.dao.MusicGroupSubjectPlanDao;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;

import java.util.List;
import java.util.Map;

@Service
public class MusicGroupSubjectPlanServiceImpl extends BaseServiceImpl<Integer, MusicGroupSubjectPlan> implements MusicGroupSubjectPlanService {

    @Autowired
    private MusicGroupSubjectPlanDao musicGroupSubjectPlanDao;

    @Autowired
    private StudentRegistrationService studentRegistrationService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private MusicGroupSubjectGoodsGroupService musicGroupSubjectGoodsGroupService;

    @Autowired
    private MusicGroupService musicGroupService;

    @Autowired
    private SubjectService subjectService;

    @Override
    public BaseDAO<Integer, MusicGroupSubjectPlan> getDAO() {
        return musicGroupSubjectPlanDao;
    }

    @Override
    public List<MusicGroupSubjectPlan> getMusicSubjectClassPlan(String musicGroupId) {
        List<MusicGroupSubjectPlan> musicSubjectClassPlans = musicGroupSubjectPlanDao.getMusicSubjectClassPlan(musicGroupId);
        List<Map<Integer, Long>> noClassStuCountBySubjectIds = studentRegistrationService.getNoClassStuCountBySubjectId(musicGroupId);
        Map<Integer, Long> map = MapUtil.convertMybatisMap(noClassStuCountBySubjectIds);
        for (MusicGroupSubjectPlan musicSubjectClassPlan : musicSubjectClassPlans) {
            Long aLong = map.get(musicSubjectClassPlan.getSubjectId());
            musicSubjectClassPlan.setNoClassStudentNum(aLong==null?0:aLong.intValue());
        }
        return musicSubjectClassPlans;
    }

    @Override
    public MusicGroupSubjectPlan getMusicOneSubjectClassPlan(String musicGroupId, int subjectId) {
        return musicGroupSubjectPlanDao.getMusicOneSubjectClassPlan(musicGroupId, subjectId);
    }

    @Override
    public MusicGroupSubjectGoodsAndInfoDto getSubjectGoodsAndInfo(String musicGroupId, Integer subjectId) {
        MusicGroup musicGroup = musicGroupService.get(musicGroupId);

        //课程形态
        Map<String, Object> courseForm = JSON.parseObject(musicGroup.getCourseForm(), Map.class);

        //乐团计划及收费信息
        MusicGroupSubjectPlan musicOneSubjectClassPlan = this.getMusicOneSubjectClassPlan(musicGroupId, subjectId);

        //乐团乐器及辅件信息
        List<MusicGroupSubjectGoodsGroup> goodsGroups = musicGroupSubjectGoodsGroupService.findGoodsGroup(musicGroupId, subjectId);
        goodsGroups.forEach(goodsGroup -> {
            if (goodsGroup.getType().equals(GoodsType.INSTRUMENT)) {
                goodsGroup.getGoodsList().forEach(goods -> {
                    goods.setGoodsList(goodsService.findGoodsByIds(goods.getComplementGoodsIdList()));
                });
            }
        });
        //获取声部（科目）下其他商品
        List<Goods> otherGoods = goodsService.findGoodsBySubId(subjectId, "OTHER");

        MusicGroupSubjectGoodsAndInfoDto musicGroupSubjectGoodsAndInfo = new MusicGroupSubjectGoodsAndInfoDto();
        musicGroupSubjectGoodsAndInfo.setMusicGroupSubjectPlan(musicOneSubjectClassPlan);
        musicGroupSubjectGoodsAndInfo.setCourseScheduleInfo(courseForm);
        musicGroupSubjectGoodsAndInfo.setMusicGroupSubjectGoodsGroupList(goodsGroups);
        musicGroupSubjectGoodsAndInfo.setOtherGoods(otherGoods);
        return musicGroupSubjectGoodsAndInfo;
    }

    @Override
    public MusicGroupRegRespDto getMusicGroupRegInfo(String musicGroupId) {
        MusicGroupRegRespDto musicGroupRegResp = new MusicGroupRegRespDto();

        //乐团信息
        MusicGroup musicGroup = musicGroupService.get(musicGroupId);
        musicGroupRegResp.setMusicGroupName(musicGroup.getName());
        //声部信息
        List<Subject> subjectList = subjectService.findSubByMusicGroupId(musicGroupId);
        musicGroupRegResp.setSubjectList(subjectList);

        return musicGroupRegResp;
    }
}