package com.yh.saas.plugin.servicer.service.impl;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.winsea.svc.base.base.entity.CommonRole;
import com.winsea.svc.base.base.entity.CommonStaff;
import com.winsea.svc.base.base.entity.CommonStaffRole;
import com.winsea.svc.base.base.service.ICommonRoleService;
import com.winsea.svc.base.base.service.ICommonStaffRoleService;
import com.winsea.svc.base.base.service.ICommonStaffService;
import com.winsea.svc.base.security.entity.User;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.winsea.svc.base.workflow.entity.WorkflowHistoricTask;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.plugin.servicer.entity.ServicerBillOperateHis;
import com.yh.saas.plugin.servicer.mapper.ServicerBillOperateHisMapper;
import com.yh.saas.plugin.servicer.service.IServicerBillOperateHisService;
import com.yh.saas.toolkit.workflow.service.IWorkflowHistoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 记录服务商单据操作历史 服务实现类
 * </p>
 *
 * @author yul
 * @since 2019-12-23
 */
@Service
public class ServicerBillOperateHisServiceImpl extends ServiceImpl<ServicerBillOperateHisMapper, ServicerBillOperateHis>
		implements IServicerBillOperateHisService {

	@SofaReference
	private ICommonStaffRoleService staffRoleService;

	@SofaReference
	private ICommonStaffService staffService;

	@SofaReference
	private ICommonRoleService roleService;

	@Autowired
	private IWorkflowHistoryService workflowHistoryService;

	@Override
	public List<ServicerBillOperateHis> getBillOperateHis(String id, String workflowId) {
		List<ServicerBillOperateHis> list = this.selectList(
				new EntityWrapper<ServicerBillOperateHis>().eq(ServicerBillOperateHis.QueryFields.BILL_ID, id)
						.orderBy(ServicerBillOperateHis.QueryFields.OPERATE_DATE, false));
		if (StringUtils.isNotEmpty(workflowId)) {
			List<WorkflowHistoricTask> historicTasks = this.workflowHistoryService.getInstanceHistories(workflowId, id);
			historicTasks
					.forEach(
							histortyTask -> list.add(ServicerBillOperateHis.getInstance(histortyTask.getOperatorName(),
									histortyTask.getEndTime() == null ? histortyTask.getClaimTime()
											: histortyTask.getEndTime(),
									histortyTask.getApproved() ? "1" : "0", histortyTask.getTaskName(),
									histortyTask.getTaskNameEn(), histortyTask.getOperatorMajorRoleName(),
									histortyTask.getAuditMind())));
		}
		return list.stream().sorted(Comparator.comparing(ServicerBillOperateHis::getOperateDate).reversed())
				.collect(Collectors.toList());
	}

	@Override
	public void saveBillOperateHis(String billType, String billId, String billName, String makeUser, Date nowDate,
			String statusName, String statusNameEn, String reason) {
		String id = IdGenerator.generateUUID();
		// 插入操作历史
		ServicerBillOperateHis billOperateHis = new ServicerBillOperateHis();
		billOperateHis.setBillType(billType);
		billOperateHis.setId(id);
		billOperateHis.setBillId(billId);
		billOperateHis.setBillName(billName);
		billOperateHis.setOperateUser(makeUser);
		if (nowDate == null) {
			nowDate = new Date();
		}
		billOperateHis.setOperateDate(nowDate);
		billOperateHis.setDealMsg(statusName);
		billOperateHis.setDealMsgEn(statusNameEn);
		billOperateHis.setVerifyRemark(reason);
		this.insert(billOperateHis);
	}

	/**
	 * 取得当前登录人职务姓名
	 *
	 * @return
	 */
	@Override
	public String getStaffAndName() {
		User currentUser = AuthSecurityUtils.getCurrentUserInfo();
		// 当前登录人主要角色
		CommonStaffRole staffRole = staffRoleService.getMajorRole(currentUser.getUserId());
		CommonStaff staff = staffService.getInfo(staffRole.getStaffId());
		CommonRole role = roleService.selectById(staffRole.getRoleId());
		return role.getRoleName() + " " + staff.getStaffName();
	}

}
