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

import com.alibaba.fastjson.JSON;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.dao.VipGroupActivityDao;
import com.ym.mec.biz.dal.dto.VipGroupActivityAddDto;
import com.ym.mec.biz.dal.dto.VipGroupSalarySettlementDto;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.dal.entity.VipGroupActivity;
import com.ym.mec.biz.event.source.EntityChangeEventSource;
import com.ym.mec.biz.service.VipGroupActivityService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class VipGroupActivityServiceImpl extends BaseServiceImpl<Integer, VipGroupActivity>  implements VipGroupActivityService {
	
	@Autowired
	private VipGroupActivityDao vipGroupActivityDao;
	@Autowired
	private TeacherDao teacherDao;
	@Autowired
	private EntityChangeEventSource entityChangeEventSource;

	@Override
	public BaseDAO<Integer, VipGroupActivity> getDAO() {
		return vipGroupActivityDao;
	}

	@Override
	public void addVipGroupActivity(VipGroupActivityAddDto vipGroupActivityAddDto) {

		if(Objects.isNull(vipGroupActivityAddDto.getOrganId())){
			throw new BizException("请指定部门!");
		}

		VipGroupSalarySettlementDto vipGroupSalarySettlement = vipGroupActivityAddDto.getVipGroupSalarySettlement();
		if(Objects.nonNull(vipGroupSalarySettlement.getOnlineSalarySettlement())){
			switch (vipGroupSalarySettlement.getOnlineSalarySettlement().getSalarySettlementType()){
				case TEACHER_DEFAULT:
					break;
				case RATIO_DISCOUNT:
					if(Objects.isNull(vipGroupSalarySettlement.getOnlineSalarySettlement().getSettlementValue())){
						throw new BizException("请指定线下课单价折算比例");
					}
					break;
				case FIXED_SALARY:
					if(Objects.isNull(vipGroupSalarySettlement.getOnlineSalarySettlement().getSettlementValue())){
						throw new BizException("请指定线下课固定课酬");
					}
					break;
				default:
					throw new BizException("未指定课酬结算标准!");
			}
		}

		if(Objects.nonNull(vipGroupSalarySettlement.getOfflineSalarySettlement())){
			switch (vipGroupSalarySettlement.getOfflineSalarySettlement().getSalarySettlementType()){
				case TEACHER_DEFAULT:
					break;
				case RATIO_DISCOUNT:
					if(Objects.isNull(vipGroupSalarySettlement.getOfflineSalarySettlement().getSettlementValue())){
						throw new BizException("请指定线线上课单价折算比例");
					}
					break;
				case FIXED_SALARY:
					if(Objects.isNull(vipGroupSalarySettlement.getOfflineSalarySettlement().getSettlementValue())){
						throw new BizException("请指定线上课固定课酬");
					}
					break;
				default:
					throw new BizException("未指定课酬结算标准!");
			}
		}

		vipGroupActivityAddDto.setSalarySettlementJson(JSON.toJSONString(vipGroupActivityAddDto.getVipGroupSalarySettlement()));
		super.insert(vipGroupActivityAddDto);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateVipGroupActivity(VipGroupActivityAddDto vipGroupActivityAddDto, Integer operatorId) {
		VipGroupActivity oldVipGroupActivity = vipGroupActivityDao.get(vipGroupActivityAddDto.getId());
		if(Objects.isNull(oldVipGroupActivity)){
			throw new BizException("活动不存在!");
		}
		if(Objects.isNull(vipGroupActivityAddDto.getOrganId())){
			throw new BizException("请指定部门!");
		}

		if(Objects.nonNull(vipGroupActivityAddDto.getVipGroupSalarySettlement().getOnlineSalarySettlement())){
			switch (vipGroupActivityAddDto.getVipGroupSalarySettlement().getOnlineSalarySettlement().getSalarySettlementType()){
				case TEACHER_DEFAULT:
					vipGroupActivityAddDto.getVipGroupSalarySettlement().getOnlineSalarySettlement().setSettlementValue(null);
					break;
				case RATIO_DISCOUNT:
					if(Objects.isNull(vipGroupActivityAddDto.getVipGroupSalarySettlement().getOnlineSalarySettlement().getSettlementValue())){
						throw new BizException("请指定线下课单价折算比例");
					}
					break;
				case FIXED_SALARY:
					if(Objects.isNull(vipGroupActivityAddDto.getVipGroupSalarySettlement().getOnlineSalarySettlement().getSettlementValue())){
						throw new BizException("请指定线下课固定课酬");
					}
					break;
				default:
					throw new BizException("未指定课酬结算标准!");
			}
		}

		if(Objects.nonNull(vipGroupActivityAddDto.getVipGroupSalarySettlement().getOfflineSalarySettlement())){
			switch (vipGroupActivityAddDto.getVipGroupSalarySettlement().getOfflineSalarySettlement().getSalarySettlementType()){
				case TEACHER_DEFAULT:
					vipGroupActivityAddDto.getVipGroupSalarySettlement().getOfflineSalarySettlement().setSettlementValue(null);
					break;
				case RATIO_DISCOUNT:
					if(Objects.isNull(vipGroupActivityAddDto.getVipGroupSalarySettlement().getOfflineSalarySettlement().getSettlementValue())){
						throw new BizException("请指定线线上课单价折算比例");
					}
					break;
				case FIXED_SALARY:
					if(Objects.isNull(vipGroupActivityAddDto.getVipGroupSalarySettlement().getOfflineSalarySettlement().getSettlementValue())){
						throw new BizException("请指定线上课固定课酬");
					}
					break;
				default:
					throw new BizException("未指定课酬结算标准!");
			}
		}

		vipGroupActivityAddDto.setSalarySettlementJson(JSON.toJSONString(vipGroupActivityAddDto.getVipGroupSalarySettlement()));

		super.update(vipGroupActivityAddDto);
		entityChangeEventSource.entityChange(oldVipGroupActivity, vipGroupActivityAddDto, operatorId);
	}

	@Override
	public List<VipGroupActivity> findByVipGroupCategory(Long categoryId, Integer teacherId) {
		if(Objects.isNull(categoryId)){
			throw new BizException("请选择课程形态!");
		}
		if(Objects.isNull(teacherId)){
			throw new BizException("请指定教师");
		}
		Teacher teacher = teacherDao.get(teacherId);
		if(Objects.isNull(teacher)){
			throw new BizException("未找到指定教师");
		}
		String organIds = teacher.getTeacherOrganId().toString();
		if (StringUtils.isNotEmpty(teacher.getFlowOrganRange())) {
			organIds += "," + teacher.getFlowOrganRange();
		}
		return vipGroupActivityDao.findByCategory(categoryId,organIds);
	}
}
