import com.red.circle.OtherServiceApplication;
import com.red.circle.other.app.scheduler.AdminSalarySettlementTask;
import com.red.circle.other.app.scheduler.BdLeaderSalarySettlementTask;
import com.red.circle.other.app.scheduler.BdSalarySettlementTask;
import com.red.circle.other.app.scheduler.TeamBillTask;
import com.red.circle.other.infra.database.mongo.entity.bd.BdLeaderSalarySettlementRecord;
import com.red.circle.other.infra.database.mongo.entity.bd.BdSalaryPolicy;
import com.red.circle.other.infra.database.mongo.entity.bd.BdSalarySettlementRecord;
import com.red.circle.other.infra.database.mongo.service.bd.BdSalaryPolicyService;
import com.red.circle.other.infra.database.mongo.service.team.TeamBillCycleUtils;
import com.red.circle.other.infra.enums.BdPolicyType;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import com.red.circle.wallet.inner.endpoint.salary.UserSalaryAccountClient;
import com.red.circle.wallet.inner.model.cmd.SalaryIssueInnerCmd;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.util.List;

@SpringBootTest(classes = OtherServiceApplication.class)
@RunWith(SpringRunner.class)
public class BdSettlementTest {

  @Autowired
  private BdSalarySettlementTask bdSalarySettlementTask;
  @Autowired
  private BdLeaderSalarySettlementTask  bdLeaderSalarySettlementTask;
  @Autowired
  private AdminSalarySettlementTask adminSalarySettlementTask;
  @Autowired
  private BdSalaryPolicyService bdSalaryPolicyService;
  @Autowired
  private MongoTemplate mongoTemplate;
  @Autowired
  private UserSalaryAccountClient userSalaryAccountClient;


  /**
   * BD 工资结算
   */
  @Test
  public void testBdSettlement() {
    Integer billBelong = TeamBillCycleUtils.getCalcBillBelong();

    bdSalarySettlementTask.processBdSalarySettlementTest(billBelong);

  }

  /**
   * BDLEADER 工资结算
   */
  @Test
  public void testBdLeaderSettlement() {
    Integer billBelong = TeamBillCycleUtils.getCalcBillBelong();

    bdLeaderSalarySettlementTask.processBdLeaderSalarySettlementTest(billBelong);

  }

  @Test
  public void testAdminSettlement() {
      adminSalarySettlementTask.processAdminSalarySettlementTest(TeamBillCycleUtils.getCalcBillBelong());
  }

  @Test
  public void testIssueSalary() {
//      Long bdUserId = 1963790037740466178L;
//      BdSalarySettlementRecord bdSalarySettlementRecord = new BdSalarySettlementRecord();
//      bdSalarySettlementRecord.setBillBelong(20251116);
//      bdSalarySettlementRecord.setSysOrigin("LIKEI");
//      bdSalarySettlementRecord.setSettlementSalary(new BigDecimal(111));
//      bdSalarySettlementRecord.setId(IdWorkerUtils.getIdStr());
//      issueSalary(bdUserId, bdSalarySettlementRecord);


      Long bdLeaderUserId = 1963790037740466178L;
      BdLeaderSalarySettlementRecord bdLeaderSalarySettlementRecord = new BdLeaderSalarySettlementRecord();
      bdLeaderSalarySettlementRecord.setBillBelong(20251116);
      bdLeaderSalarySettlementRecord.setSysOrigin("LIKEI");
      bdLeaderSalarySettlementRecord.setSettlementSalary(new BigDecimal(2));
      bdLeaderSalarySettlementRecord.setId(IdWorkerUtils.getIdStr());
      issueSalary(bdLeaderUserId, bdLeaderSalarySettlementRecord);
  }

    /**
     * 发放BD工资
     */
    private void issueSalary(Long bdUserId, BdSalarySettlementRecord record) {
        SalaryIssueInnerCmd innerCmd = new SalaryIssueInnerCmd();
        innerCmd.setBillBelong(record.getBillBelong());
        innerCmd.setUserId(bdUserId);
        innerCmd.setSysOrigin(record.getSysOrigin());
        innerCmd.setSalaryType("BD_SALARY");
        innerCmd.setEventDesc("SALARY_BD");
        innerCmd.setAmount(record.getSettlementSalary());
        innerCmd.setTrackId(record.getId());
        innerCmd.setBdId(bdUserId);
        userSalaryAccountClient.issueSalary(innerCmd);
    }

    /**
     * 发放BDLEADER工资
     */
    private void issueSalary(Long bdLeaderUserId, BdLeaderSalarySettlementRecord record) {
        SalaryIssueInnerCmd innerCmd = new SalaryIssueInnerCmd();
        innerCmd.setBillBelong(record.getBillBelong());
        innerCmd.setUserId(bdLeaderUserId);
        innerCmd.setSysOrigin(record.getSysOrigin());
        innerCmd.setSalaryType("BD_LEADER_SALARY");
        innerCmd.setEventDesc("SALARY_BD_LEADER");
        innerCmd.setAmount(record.getSettlementSalary());
        innerCmd.setTrackId(record.getId());
        innerCmd.setBdId(bdLeaderUserId);
        userSalaryAccountClient.issueSalary(innerCmd);
    }


}
