package com.yifu.cloud.plus.v1.yifu.salary.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.entity.TStatisticsTaxSalary;
import com.yifu.cloud.plus.v1.yifu.salary.mapper.TStatisticsTaxSalaryMapper;
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.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 统计-工资薪金
 *
 * @author hgw
 * @date 2020-05-27 10:46:05
 */
@AllArgsConstructor
@Slf4j
@Service("tStatisticsTaxSalaryTwoService")
public class TStatisticsTaxSalaryTwoServiceImpl extends ServiceImpl<TStatisticsTaxSalaryMapper, TStatisticsTaxSalary> implements TStatisticsTaxSalaryTwoService {

	private final TSalaryAccount2022Service tSalaryAccount2020Service;

	private final TStatisticsCurrentReportService tStatisticsCurrentReportService;
     /**
      * @param tableName
      * @param settleMonth
      * @Description: 刷新工资薪金
      * @Author: hgw
      * @Date: 2022/2/23 15:12
      * @return: com.yifu.cloud.v1.common.core.util.R<java.lang.String>
      **/
     @Override
     public R<String> doStatisticsTaxSalary(String tableName, String settleMonth) {
          int deletes = baseMapper.deleteByYearMonth(settleMonth);
          if (deletes != -1) {
               String settleYear = settleMonth.substring(0, 4);
               baseMapper.insertStatisticsTaxSalaryByTableName(tableName, settleMonth, settleYear);
               return R.ok("刷新工资薪金成功！");
          }
          return R.failed("刷新工资薪金失败！");
     }

	@Override
	public void doRefreshStatisticsTaxAuto(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 = this.doStatisticsTaxSalary(tableName, settleMonth);
			if (taxR.getCode() == CommonConstants.SUCCESS) {
				// 刷新对应结算月的 本期申报
				tStatisticsCurrentReportService.doRefreshStatisticsCurrentReport(settleMonth);
			} else {
				log.error(taxR.getMsg());
			}
		} else {
			log.error(accountR == null ? "刷新对应结算月的 年度报账-失败！" : accountR.getMsg());
		}
	}

}
