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

import com.alibaba.fastjson.JSON;
import com.ym.mec.biz.dal.dao.ActivityPlanDao;
import com.ym.mec.biz.dal.dto.ActivityPlanAddDto;
import com.ym.mec.biz.dal.dto.SalarySettlementDto;
import com.ym.mec.biz.dal.dto.SalarySettlementTypeDto;
import com.ym.mec.biz.dal.entity.ActivityPlan;
import com.ym.mec.biz.service.ActivityPlanService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
public class ActivityPlanServiceImpl extends BaseServiceImpl<Integer, ActivityPlan>  implements ActivityPlanService {
	
	@Autowired
	private ActivityPlanDao activityPlanDao;

	@Override
	public BaseDAO<Integer, ActivityPlan> getDAO() {
		return activityPlanDao;
	}

    @Override
	@Transactional(rollbackFor = Exception.class)
    public void addActivityPlan(ActivityPlanAddDto activityPlanAddDto) {
		if(Objects.isNull(activityPlanAddDto.getOrganId())){
			throw new BizException("请指定分部!");
		}
		SalarySettlementDto salarySettlement = activityPlanAddDto.getSalarySettlement();
		SalarySettlementTypeDto onlineSalarySettlement = salarySettlement.getOnlineSalarySettlement();
		if(Objects.nonNull(onlineSalarySettlement)){
			switch (onlineSalarySettlement.getSalarySettlementType()){
				case TEACHER_DEFAULT:
					break;
				case FIXED_SALARY:
					if(Objects.isNull(onlineSalarySettlement.getSettlementValue())){
						throw new BizException("请指定线下课固定课酬");
					}
					break;
				default:
					throw new BizException("错误的课酬结算标准!");
			}
		}
		SalarySettlementTypeDto offlineSalarySettlement = salarySettlement.getOfflineSalarySettlement();
		if(Objects.nonNull(offlineSalarySettlement)){
			switch (offlineSalarySettlement.getSalarySettlementType()){
				case TEACHER_DEFAULT:
					break;
				case FIXED_SALARY:
					if(Objects.isNull(offlineSalarySettlement.getSettlementValue())){
						throw new BizException("请指定线上课固定课酬");
					}
					break;
				default:
					throw new BizException("错误的课酬结算标准!");
			}
		}
		SalarySettlementTypeDto giveOnlineSalarySettlement = salarySettlement.getGiveOnlineSalarySettlement();
		if(Objects.nonNull(giveOnlineSalarySettlement)){
			switch (giveOnlineSalarySettlement.getSalarySettlementType()){
				case TEACHER_DEFAULT:
					break;
				case FIXED_SALARY:
					if(Objects.isNull(giveOnlineSalarySettlement.getSettlementValue())){
						throw new BizException("请指定赠送线下课固定课酬");
					}
					break;
				default:
					throw new BizException("错误的课酬结算标准!");
			}
		}
		SalarySettlementTypeDto giveOfflineSalarySettlement = salarySettlement.getGiveOfflineSalarySettlement();
		if(Objects.nonNull(giveOfflineSalarySettlement)){
			switch (giveOfflineSalarySettlement.getSalarySettlementType()){
				case TEACHER_DEFAULT:
					break;
				case FIXED_SALARY:
					if(Objects.isNull(giveOfflineSalarySettlement.getSettlementValue())){
						throw new BizException("请指定赠送线上课固定课酬");
					}
					break;
				default:
					throw new BizException("错误的课酬结算标准!");
			}
		}

		activityPlanAddDto.setSalarySettlementJson(JSON.toJSONString(salarySettlement));
		super.insert(activityPlanAddDto);
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateActivityPlan(ActivityPlanAddDto activityPlanAddDto) {
//		SysUser sysUser = sysUserFeignService.queryUserInfo();
//		if(sysUser == null){
//			throw new BizException("用户信息获取失败");
//		}
		ActivityPlan oldActivityPlan = activityPlanDao.get(activityPlanAddDto.getId());
		if(Objects.isNull(oldActivityPlan)){
			throw new BizException("活动不存在!");
		}
		if(Objects.isNull(activityPlanAddDto.getOrganId())){
			throw new BizException("请指定分部!");
		}
		SalarySettlementDto salarySettlement = activityPlanAddDto.getSalarySettlement();
		SalarySettlementTypeDto onlineSalarySettlement = salarySettlement.getOnlineSalarySettlement();
		if(Objects.nonNull(onlineSalarySettlement)){
			switch (onlineSalarySettlement.getSalarySettlementType()){
				case TEACHER_DEFAULT:
					onlineSalarySettlement.setSettlementValue(null);
					break;
				case FIXED_SALARY:
					if(Objects.isNull(onlineSalarySettlement.getSettlementValue())){
						throw new BizException("请指定线下课固定课酬");
					}
					break;
				default:
					throw new BizException("错误的课酬结算标准!");
			}
		}

		SalarySettlementTypeDto offlineSalarySettlement = salarySettlement.getOfflineSalarySettlement();
		if(Objects.nonNull(offlineSalarySettlement)){
			switch (offlineSalarySettlement.getSalarySettlementType()){
				case TEACHER_DEFAULT:
					break;
				case FIXED_SALARY:
					if(Objects.isNull(offlineSalarySettlement.getSettlementValue())){
						throw new BizException("请指定线上课固定课酬");
					}
					break;
				default:
					throw new BizException("错误的课酬结算标准!");
			}
		}
		SalarySettlementTypeDto giveOnlineSalarySettlement = salarySettlement.getGiveOnlineSalarySettlement();
		if(Objects.nonNull(giveOnlineSalarySettlement)){
			switch (giveOnlineSalarySettlement.getSalarySettlementType()){
				case TEACHER_DEFAULT:
					break;
				case FIXED_SALARY:
					if(Objects.isNull(giveOnlineSalarySettlement.getSettlementValue())){
						throw new BizException("请指定赠送线下课固定课酬");
					}
					break;
				default:
					throw new BizException("错误的课酬结算标准!");
			}
		}
		SalarySettlementTypeDto giveOfflineSalarySettlement = salarySettlement.getGiveOfflineSalarySettlement();
		if(Objects.nonNull(giveOfflineSalarySettlement)){
			switch (giveOfflineSalarySettlement.getSalarySettlementType()){
				case TEACHER_DEFAULT:
					break;
				case FIXED_SALARY:
					if(Objects.isNull(giveOfflineSalarySettlement.getSettlementValue())){
						throw new BizException("请指定赠送线上课固定课酬");
					}
					break;
				default:
					throw new BizException("错误的课酬结算标准!");
			}
		}

		activityPlanAddDto.setSalarySettlementJson(JSON.toJSONString(salarySettlement));

		super.update(activityPlanAddDto);
//		entityChangeEventSource.entityChange(oldActivityPlan, activityPlanAddDto, sysUser.getId());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteActivityPlan(Integer id) {
		//被使用过的活动无法修改

	}
}