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.SysConfigDao;
import com.ym.mec.biz.dal.dao.TeacherSalaryComplaintsDao;
import com.ym.mec.biz.dal.entity.TeacherSalaryComplaints;
import com.ym.mec.biz.dal.enums.MessageTypeEnum;
import com.ym.mec.biz.dal.enums.TeacherSalaryComplaintsStatusEnum;
import com.ym.mec.biz.service.SysConfigService;
import com.ym.mec.biz.service.SysMessageService;
import com.ym.mec.biz.service.TeacherSalaryComplaintsService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
import com.ym.mec.util.date.DateUtil;
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.*;
import java.util.stream.Collectors;

import static com.ym.mec.biz.dal.enums.TeacherSalaryComplaintsStatusEnum.PENDING;

@Service
public class TeacherSalaryComplaintsServiceImpl extends BaseServiceImpl<Long, TeacherSalaryComplaints> implements TeacherSalaryComplaintsService {

	@Autowired
	private TeacherSalaryComplaintsDao teacherSalaryComplaintsDao;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	@Autowired
	private SysConfigDao sysConfigDao;
	@Autowired
	private SysMessageService sysMessageService;

	@Override
	public BaseDAO<Long, TeacherSalaryComplaints> getDAO() {
		return teacherSalaryComplaintsDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void complaintsDispose(TeacherSalaryComplaints complaints) {
		TeacherSalaryComplaints salaryComplaints = teacherSalaryComplaintsDao.get(complaints.getId());
		if(salaryComplaints == null){
			throw new BizException("投诉信息不存在");
		}
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			throw new BizException("用户信息获取失败");
		}
		if(salaryComplaints.getStatusEnum() == TeacherSalaryComplaintsStatusEnum.WITHDRAWN){
			throw new BizException("投诉信息已撤销");
		}
		if(salaryComplaints.getStatusEnum() != PENDING){
			throw new BizException("投诉信息已处理");
		}
		salaryComplaints.setUpdateTime(new Date());
		salaryComplaints.setStatusEnum(complaints.getStatusEnum());
		salaryComplaints.setHandingSuggestion(complaints.getHandingSuggestion());
		salaryComplaints.setOperatorId(sysUser.getId());
		teacherSalaryComplaintsDao.update(salaryComplaints);
		Map<Integer, String> map = new HashMap<>(1);
		map.put(salaryComplaints.getTeacherId(), salaryComplaints.getTeacherId().toString());
		if (map != null && map.size() > 0) {
			String url = new StringBuffer("8?").append(sysConfigDao.findConfigValue(SysConfigService.TEACHER_BASE_URL)).append("/#/appealResult?id=").append(salaryComplaints.getId()).toString();
			sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, MessageTypeEnum.COURSE_SALARY_COMPLAINTS_RESULT, map, null, 0, url,
					"TEACHER", DateUtil.format(salaryComplaints.getCreateTime(),DateUtil.DEFAULT_PATTERN),complaints.getStatusEnum().getMsg());
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public synchronized Object add(TeacherSalaryComplaints complaints) {
		String settlementMonth = complaints.getSalarySettlementMonth();
		String reason = complaints.getReason();
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			throw new BizException("用户信息获取失败");
		}
		Integer teacherId = sysUser.getId();
		if(StringUtils.isEmpty(settlementMonth) || StringUtils.isEmpty(reason)){
			throw new BizException("数据提交失败");
		}
		TeacherSalaryComplaints salaryComplaints = teacherSalaryComplaintsDao.findByMonthAndTeacherId(settlementMonth,teacherId,PENDING);
		if(salaryComplaints != null){
			throw new BizException("申述已提交");
		}
		complaints.setTeacherId(teacherId);
		complaints.setStatusEnum(PENDING);
		return teacherSalaryComplaintsDao.insert(complaints);
	}

	@Override
	public Object findByMonth(String salarySettlementMonth) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			throw new BizException("用户信息获取失败");
		}
		return teacherSalaryComplaintsDao.findByMonthAndTeacherId(salarySettlementMonth,sysUser.getId(),PENDING);
	}

	@Override
	public void repealComplaints(String salarySettlementMonth) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			throw new BizException("用户信息获取失败");
		}
		teacherSalaryComplaintsDao.repealComplaints(salarySettlementMonth,sysUser.getId());
	}
}
