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

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.StudentRegistrationDao;
import com.ym.mec.biz.dal.dao.SubjectDao;
import com.ym.mec.biz.dal.dao.SubjectGoodsMapperDao;
import com.ym.mec.biz.dal.dto.ConditionDto;
import com.ym.mec.biz.dal.dto.QueryConditionDto;
import com.ym.mec.biz.dal.dto.SubjectApplyDetailDto;
import com.ym.mec.biz.dal.entity.Subject;
import com.ym.mec.biz.dal.entity.SubjectGoodsMapper;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.page.SubjectQueryInfo;
import com.ym.mec.biz.service.SubjectService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;

import com.ym.mec.util.collection.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SubjectServiceImpl extends BaseServiceImpl<Integer, Subject>  implements SubjectService {

    @Autowired
    private SubjectDao subjectDao;
    @Autowired
    private StudentRegistrationDao studentRegistrationDao;
    @Autowired
    private SubjectGoodsMapperDao subjectGoodsMapperDao;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Override
    public BaseDAO<Integer, Subject> getDAO() {
        return subjectDao;
    }

    @Override
    public List<Subject> findSubByMusicGroupId(String musicGroupId) {
        return subjectDao.findSubByMusicGroupId(musicGroupId);
    }

    @Override
    public PageInfo<Subject> queryPageTree(SubjectQueryInfo queryInfo) {
        PageInfo<Subject> pageInfo = queryPage(queryInfo);
        for (Subject subject:pageInfo.getRows()) {
            subject = getTree(subject,queryInfo.getDelFlag());
        }
        return pageInfo;
    }

    @Override
    public List<Subject> findDefaultSubByChargeTypeId(Integer chargeTypeId) {
        return subjectDao.findDefaultSubByChargeTypeId(chargeTypeId);
    }

    @Override
    public List<SubjectApplyDetailDto> findSubApplyDetail(String musicGroupId) {
        List<SubjectApplyDetailDto> subApplyDetail = subjectDao.findSubApplyDetail(musicGroupId);
        List<Map<Integer, Integer>> payNums = studentRegistrationDao.countPayNum(musicGroupId);
        Map<Integer, Integer> payNumMap = MapUtil.convertMybatisMap(payNums);
        subApplyDetail.forEach(detail ->{
            detail.setPayNum(payNumMap.get(detail.getSubjectId()));
        });
        return subApplyDetail;
    }

    @Override
    public void upSetSubject(Subject subject) {
        Integer parentId = upset(subject, null);
        List<Subject> subjects = subject.getSubjects();
        if(subjects != null && subjects.size() > 0){
            subjects.forEach(e->{
                upset(e, parentId);
            });
        }
    }

    @Override
    public void markGoods(List<SubjectGoodsMapper> subjectGoodsMappers) {
        subjectGoodsMappers.forEach(e->{
            upsetGoods(e);
        });
    }

    private void upsetGoods(SubjectGoodsMapper subjectGoodsMapper){
        if(subjectGoodsMapper.getId() != null){
            if(subjectGoodsMapper.getDelFlag()){
                subjectGoodsMapperDao.delete(subjectGoodsMapper.getId());
                return;
            }
            subjectGoodsMapper.setUpdateTime(new Date());
            subjectGoodsMapperDao.update(subjectGoodsMapper);
        }else {
            subjectGoodsMapperDao.insert(subjectGoodsMapper);
        }
    }


    private Integer upset(Subject subject,Integer parentId){
        if(parentId != null){
            subject.setParentSubjectId(parentId);
        }
        if(subject.getId() != null){
            subject.setUpdateTime(new Date());
            subjectDao.update(subject);
        }else {
            subjectDao.insert(subject);
        }
        return subject.getId();
    }

    private Subject getTree(Subject sub,YesOrNoEnum yesOrNoEnum){
        //得到根节点对象
        //获取子节点list
        List<Subject> subjects = subjectDao.findByParentId(sub.getId().toString(),yesOrNoEnum);
        //如果存在子节点
        if(subjects != null && subjects.size() > 0) {
            //将子节点list放入父节点对象
            sub.setSubjects(subjects);
            //遍历子节点....
            for (Subject subject : subjects) {
                getTree(subject,yesOrNoEnum);
            }
        }
        return sub;
    }

    @Override
    public List<Subject> findTeacherSubjects() {
        return subjectDao.findTeacherSubjects(sysUserFeignService.queryUserInfo().getId().longValue());
    }

    @Override
    public List<ConditionDto> findSubjectConditions() {
        return subjectDao.findSubjectConditions();
    }
}