package com.yifu.cloud.plus.v1.yifu.salary.util;

import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.salary.service.TSalaryAccount2022Service;
import com.yifu.cloud.plus.v1.yifu.salary.service.TStatisticsCurrentReportService;
import com.yifu.cloud.plus.v1.yifu.salary.service.TStatisticsTaxSalaryTwoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 主要执行统计时长较长的方法
 *
 * @author hgw
 * @Date 2022-2-25 10:32:37
 * @Description 多线程的执行demo
 */
@Slf4j
@Component
public class DoStatisticsTask {

	@Autowired
	private TStatisticsTaxSalaryTwoService tStatisticsTaxSalaryTwoService;

	@Autowired
	private TSalaryAccount2022Service tSalaryAccount2020Service;

	@Autowired
	private TStatisticsCurrentReportService tStatisticsCurrentReportService;

	/**
	 * @param settleMonth
	 * @Description: 核心刷新-年度报账、工资薪金、本期申报
	 * @Author: hgw
	 * @Date: 2022-2-25 10:29:28
	 * @return: void
	 **/
	@Async
	public void doRefreshCore(String settleMonth) {
		String tableName = "t_salary_account_" + settleMonth.substring(0, 4);
		// 刷新对应结算月的 年度报账
		R<String> accountR = tSalaryAccount2020Service.doRefreshSalaryAccountByMonth(tableName, settleMonth);
		if (accountR != null && accountR.getCode() == CommonConstants.SUCCESS) {
			// 刷新对应结算月的 工资薪金
			R<String> taxR = tStatisticsTaxSalaryTwoService.doStatisticsTaxSalary(tableName, settleMonth);
			if (taxR.getCode() == CommonConstants.SUCCESS) {
				// 刷新对应结算月的 本期申报
				tStatisticsCurrentReportService.doRefreshStatisticsCurrentReport(settleMonth);
			} else {
				log.error(taxR.getMsg());
			}
		} else {
			log.error(accountR == null ? "刷新对应结算月的 年度报账-失败！" : accountR.getMsg());
		}
	}

}
