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

import com.alibaba.fastjson.JSONObject;
import com.ym.mec.biz.dal.dao.ChargeTypeOrganizationFeeDao;
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.SubFeeSettingDto;
import com.ym.mec.biz.dal.dto.SubjectApplyDetailDto;
import com.ym.mec.biz.dal.entity.ChargeTypeOrganizationFee;
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.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
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 ChargeTypeOrganizationFeeDao chargeTypeOrganizationFeeDao;

    @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(),queryInfo.getTenantId());
        }
        return pageInfo;
    }

    @Override
    public List<Subject> findDefaultSubByChargeTypeId(Integer chargeTypeId,Integer organId,Integer number) {
        List<Subject> subByChargeTypeId = subjectDao.findDefaultSubByChargeTypeId(chargeTypeId);
        ChargeTypeOrganizationFee byOrganId = chargeTypeOrganizationFeeDao.findByOrganId(chargeTypeId, organId);
        if(byOrganId != null){
            BigDecimal bigDecimal = new BigDecimal(number);
            subByChargeTypeId.forEach(e->{
                e.setFee(byOrganId.getCourseFee().multiply(bigDecimal));
            });
        }
        return subByChargeTypeId;
    }

    @Override
    public List<SubjectApplyDetailDto> findSubApplyDetail(String musicGroupId) {
        List<SubjectApplyDetailDto> subApplyDetail = subjectDao.findSubApplyDetail(musicGroupId);
//        //统计当前乐团不同声部的报名人数
        Map<Integer,Integer> applyNum = JSONObject.parseObject(JSONObject.toJSONString(MapUtil.convertIntegerMap(studentRegistrationDao.countApplyNum(musicGroupId))),HashMap.class);
        Map<Integer, Integer> payNumMap = JSONObject.parseObject(JSONObject.toJSONString(MapUtil.convertIntegerMap(studentRegistrationDao.countPayNum(musicGroupId))),HashMap.class);
        subApplyDetail.forEach(detail ->{
            Integer num = payNumMap.get(detail.getSubjectId());
            detail.setPayNum(num == null?0:num);
            num = applyNum.get(detail.getSubjectId());
            detail.setApplyStudentNum(num == null?0:num);
        });
        return subApplyDetail;
    }

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

    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();
    }

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

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

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

    @Override
    public Map<Long, String> findSubjectMapBySubjectIdsList(List<String> subjectIdsList) {
        if(CollectionUtils.isEmpty(subjectIdsList)){
            return new HashMap<>();
        }
        String subjectIdsString = StringUtils.join(subjectIdsList, ",");
        String[] split = subjectIdsString.split(",");
        List<String> subjectIds=new ArrayList<>(Arrays.asList(split));
        subjectIds = subjectIds.stream().distinct().collect(Collectors.toList());
        List<Map<Long, String>> bySubjecIds = subjectDao.findBySubjecIds(StringUtils.join(subjectIds,","));
        Map<Long,String> subjectIdAndName = MapUtil.convertMybatisMap(bySubjecIds);
        return subjectIdAndName;
    }

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

    @Override
    public List<Subject> findBySubjectByIdList(List<Integer> subjectIdList) {
        return subjectDao.findBySubjectByIdList(StringUtils.join(subjectIdList,","));
    }

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

    @Override
    public List<String> findBySubIds(String subIds) {
        return subjectDao.findBySubIds(subIds);
    }

    @Override
    public SubFeeSettingDto setSubjectInfo(Integer chargeTypeId) {
        return null;
    }
}
