package cn.superdesk.uniorder.dubbo.provider.scheduler;

import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.scheduler.SysQuartzScheduleTaskStatementOfAccount;
import cn.superdesk.uniorder.dao.entity.sys.SysUser;
import cn.superdesk.uniorder.dto.param.accounting.AutoCreateScheduleTaskStatementOfAccountParam;
import cn.superdesk.uniorder.dto.param.accounting.AutoCreateScheduleTaskStatementOfAccountReportVo;
import cn.superdesk.uniorder.dto.param.scheduler.SysQuartzScheduleTaskStatementOfAccountParam;
import cn.superdesk.uniorder.dubbo.consumer.websocket.WebSocketDubboCustomerService;
import cn.superdesk.uniorder.dubbo.facade.scheduler.DubboSysQuartzScheduleTaskStatementOfAccountService;
import cn.superdesk.uniorder.service.scheduler.SysQuartzScheduleTaskStatementOfAccountService;
import cn.superdesk.uniorder.service.sys.SysUserService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @Description: 定时任务-时刻任务-对账单
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2020-12-17 16:35:26
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020-12-17 16:35:26
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboSysQuartzScheduleTaskStatementOfAccountServiceImpl implements DubboSysQuartzScheduleTaskStatementOfAccountService {


	/****************************************** 外部调用 start ******************************************/
	@Resource
	private WebSocketDubboCustomerService                  webSocketDubboCustomerService;
	/****************************************** 外部调用   end ******************************************/

	@Autowired
	private SysQuartzScheduleTaskStatementOfAccountService sysQuartzScheduleTaskStatementOfAccountService;

	@Autowired
	private SysUserService sysUserService;


	/****************************************** extends BaseService 部分 start ******************************************/

	@Override
	public void addObj(SysQuartzScheduleTaskStatementOfAccountParam param) {

		SysQuartzScheduleTaskStatementOfAccount sysQuartzScheduleTaskStatementOfAccount = AnBeanUtils.copy(param, SysQuartzScheduleTaskStatementOfAccount.class);
		sysQuartzScheduleTaskStatementOfAccountService.insertSelective(sysQuartzScheduleTaskStatementOfAccount);

	}

	@Override
	public void updateObj(SysQuartzScheduleTaskStatementOfAccountParam param) {

		SysQuartzScheduleTaskStatementOfAccount sysQuartzScheduleTaskStatementOfAccount = AnBeanUtils.copy(param, SysQuartzScheduleTaskStatementOfAccount.class);
		sysQuartzScheduleTaskStatementOfAccountService.updateByPrimaryKeySelective(sysQuartzScheduleTaskStatementOfAccount);
	}

	@Override
	public void deleteObj(Long id) {
		sysQuartzScheduleTaskStatementOfAccountService.deleteObj(id);
	}

	@Override
	public SysQuartzScheduleTaskStatementOfAccount findById(Long id) {
		return sysQuartzScheduleTaskStatementOfAccountService.findById(id);
	}

	@Override
	public PageInfo<SysQuartzScheduleTaskStatementOfAccount> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {
		return sysQuartzScheduleTaskStatementOfAccountService.findByPaging(conditions, pageNun, pageSize);
	}

	@Override
	public List<SysQuartzScheduleTaskStatementOfAccount> findAll(Map<String, Object> queryMap) {

		return sysQuartzScheduleTaskStatementOfAccountService.findAll(queryMap);
	}

	/****************************************** extends BaseService 部分   end ******************************************/


	@Override
	public void addSysQuartzScheduleTaskStatementOfAccount(SysQuartzScheduleTaskStatementOfAccountParam param) {

		SysQuartzScheduleTaskStatementOfAccount sysQuartzScheduleTaskStatementOfAccount = AnBeanUtils.copy(param, SysQuartzScheduleTaskStatementOfAccount.class);
		sysQuartzScheduleTaskStatementOfAccountService.addSysQuartzScheduleTaskStatementOfAccount(sysQuartzScheduleTaskStatementOfAccount);
	}

	@Override
	public void updateSysQuartzScheduleTaskStatementOfAccount(SysQuartzScheduleTaskStatementOfAccountParam param) {

		SysQuartzScheduleTaskStatementOfAccount sysQuartzScheduleTaskStatementOfAccount = AnBeanUtils.copy(param, SysQuartzScheduleTaskStatementOfAccount.class);
		sysQuartzScheduleTaskStatementOfAccountService.updateSysQuartzScheduleTaskStatementOfAccount(sysQuartzScheduleTaskStatementOfAccount);
	}

	@Override
	public void deleteSysQuartzScheduleTaskStatementOfAccount(Long id) {
		sysQuartzScheduleTaskStatementOfAccountService.deleteSysQuartzScheduleTaskStatementOfAccount(id);
	}

	@Override
	public SysQuartzScheduleTaskStatementOfAccount findSysQuartzScheduleTaskStatementOfAccountById(Long id) {
		return sysQuartzScheduleTaskStatementOfAccountService.findSysQuartzScheduleTaskStatementOfAccountById(id);
	}

	@Override
	public R<List<SysQuartzScheduleTaskStatementOfAccount>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {
		return sysQuartzScheduleTaskStatementOfAccountService.pageQuery(conditions, pageNun, pageSize);

	}

	public AutoCreateScheduleTaskStatementOfAccountReportVo autoCreateScheduleTaskStatementOfAccountDimOrder(String token, AutoCreateScheduleTaskStatementOfAccountParam params) {

		return sysQuartzScheduleTaskStatementOfAccountService.autoCreateScheduleTaskStatementOfAccountDimOrder(token, params);
	}

	public AutoCreateScheduleTaskStatementOfAccountReportVo autoCreateScheduleTaskStatementOfAccountDimGoods(String token, AutoCreateScheduleTaskStatementOfAccountParam params) {

		return sysQuartzScheduleTaskStatementOfAccountService.autoCreateScheduleTaskStatementOfAccountDimGoods(token, params);
	}

	public List<SysQuartzScheduleTaskStatementOfAccount> getToDoSysQuartzScheduleTaskStatementOfAccountList(int pageNun, int pageSize) {
		return sysQuartzScheduleTaskStatementOfAccountService.getToDoSysQuartzScheduleTaskStatementOfAccountList(pageNun, pageSize);
	}

	/**
	 * 前端调用的 -> 单个任务执行 异步
	 *
	 * @param entity
	 * @param loginSysUser
	 */
	public void runTaskSysQuartzScheduleTaskStatementOfAccount(SysQuartzScheduleTaskStatementOfAccount entity, LoginSysUser loginSysUser) {


		try {
			sysQuartzScheduleTaskStatementOfAccountService.runTaskSysQuartzScheduleTaskStatementOfAccount(entity);

			SysUser sysUser = sysUserService.findById(entity.getCreateUserId());

			// 通知 创建人 sysUser.getUserName()
			webSocketDubboCustomerService.processWrapperBodySysQuartzScheduleTaskStatementOfAccountRunTask(loginSysUser.getSysUserName(), entity);

			// 通知
//				dubboSysNotifyService.notifyExportExcelAccountingCustomerNotYetDimGoods(sysNotify, loginSysUser);

		} catch (Exception e2) {
			log.error("\n客户结算 -> 单个任务{} -> 执行 异常:{}", AnJsonUtils.toJson(entity), e2);
		}

	}

	public void runAsyncTaskSysQuartzScheduleTaskStatementOfAccount(SysQuartzScheduleTaskStatementOfAccount entity, LoginSysUser loginSysUser) {

		// 异步操作 包裹 mark by linjinyu at 20210220
		log.info("\n异步操作 包裹 mark by linjinyu at 20210220 \n{}", AnJsonUtils.toJson(entity));
		CompletableFuture.runAsync(() -> {
			runTaskSysQuartzScheduleTaskStatementOfAccount(entity, loginSysUser);
		});
	}

	/**
	 * 同步
	 *
	 * @param message
	 * @param loginSysUser
	 */
	public void kafkaStatementOfAccountDimOrderTaskReceive(String message, LoginSysUser loginSysUser) {

		log.info("##################### kafkaStatementOfAccountDimOrderTaskReceive start #####################\n原始:\n{}", message);

		try {

			SysQuartzScheduleTaskStatementOfAccount entity = AnJsonUtils.toObject(message, SysQuartzScheduleTaskStatementOfAccount.class);
			log.info("\n转换:\n{}", AnJsonUtils.toJson(entity));

			sysQuartzScheduleTaskStatementOfAccountService.runTaskSysQuartzScheduleTaskStatementOfAccount(entity);

		} catch (Exception e) {

			log.error("kafkaStatementOfAccountDimOrderTaskReceive 任务处理 错误 \n " + ExceptionUtils.getMessage(e));

			e.printStackTrace();
		}
	}
}
