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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

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.util.AuthSecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.saas.plugin.news.entity.OperationHis;
import com.yh.saas.plugin.news.mapper.NewsOperationHisMapper;
import com.yh.saas.plugin.news.service.INewsOperationHisService;
import com.yh.saas.plugin.news.vo.OperationHisVo;

/**
 * <p>
 * 记录单据操作历史 服务实现类
 * </p>
 *
 * @author Chenly
 * @since 2019-09-30
 */
@Service
public class NewsOperationHisServiceImpl extends ServiceImpl<NewsOperationHisMapper, OperationHis>
		implements INewsOperationHisService {

	@SofaReference
	private ICommonStaffRoleService staffRoleService;

	@SofaReference
	private ICommonStaffService staffService;

	@SofaReference
	private ICommonRoleService commonRoleService;

	@Override
	public List<OperationHisVo> getOperationHis(String billTypeId) {
		List<OperationHisVo> list = new ArrayList<OperationHisVo>();
		Wrapper<OperationHis> wrapper = new EntityWrapper<>();
		wrapper.eq(OperationHis.QueryFields.COMPID, AuthSecurityUtils.getCurrentUserInfo().getCompId());
		wrapper.eq(OperationHis.QueryFields.BILLID, billTypeId);
		wrapper.orderBy(OperationHis.QueryFields.OPERATEDATE, false);
		List<OperationHis> operationHisList = this.selectList(wrapper);
		operationHisList.stream().forEach(operationHis -> {
			OperationHisVo operationHisVo = new OperationHisVo();
			org.springframework.beans.BeanUtils.copyProperties(operationHis, operationHisVo);
			operationHisVo
					.setOperateUserName(operationHis.getOperateUserDuty() + " " + operationHis.getOperateUserName());
			operationHisVo
					.setOperateHis(operationHis.getDealMsg());
			operationHisVo.setDealMsg(operationHis.getVerifyRemark());
			if (StringUtils.isNotEmpty(operationHis.getAppendixId())){
				operationHisVo.setAppendixSize(operationHis.getAppendixId().split(";").length);
			}
			list.add(operationHisVo);
		});
		return list;
	}

	@Override
	@Transactional
	public void saveOperationHis(String billId, String dealMsg,
			String verifyRemark,String appendixId) {
		CommonStaffRole staffRole = staffRoleService.getMajorRole(AuthSecurityUtils.getCurrentUserInfo().getUserId());
		CommonRole commonRole = commonRoleService.selectById(staffRole.getRoleId());
		CommonStaff commonStaff = staffService.getInfo(staffRole.getStaffId());
		OperationHis operationHis = new OperationHis();
		operationHis.setId(UUID.randomUUID().toString().replace("-",""));
		operationHis.setCompId(AuthSecurityUtils.getCurrentUserInfo().getCompId());
		operationHis.setBillId(billId);
		operationHis.setDealMsg(dealMsg);
		operationHis.setVerifyRemark(verifyRemark);
		operationHis.setOperateDate(new Date());
		operationHis.setOperateUserDuty(commonRole.getRoleName());
		operationHis.setOperateUserId(AuthSecurityUtils.getCurrentUserInfo().getUserId());
		operationHis.setOperateUserName(commonStaff.getStaffName());
		operationHis.setAppendixId(appendixId);
		this.insert(operationHis);
	}

}
