package com.red.circle.other.infra.database.mongo.service.bd.impl;

import com.red.circle.other.infra.database.mongo.entity.bd.AdminSalaryPolicy;
import com.red.circle.other.infra.database.mongo.service.bd.AdminSalaryPolicyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * 管理员工资政策服务实现.
 *
 * @author AI Assistant
 * @since 2025-12-13
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminSalaryPolicyServiceImpl implements AdminSalaryPolicyService {

    private final MongoTemplate mongoTemplate;

    @Override
    public List<AdminSalaryPolicy> listEnabledByType(String policyType) {
        Query query = new Query(Criteria.where("policyType").is(policyType)
                .and("enabled").is(true));
        return mongoTemplate.find(query, AdminSalaryPolicy.class);
    }

    @Override
    public void create(AdminSalaryPolicy policy) {
        policy.setCreateTime(new Timestamp(System.currentTimeMillis()));
        policy.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        mongoTemplate.insert(policy);
    }

    @Override
    public void batchCreate(List<AdminSalaryPolicy> policies) {
        Timestamp now = new Timestamp(System.currentTimeMillis());
        policies.forEach(policy -> {
            policy.setCreateTime(now);
            policy.setUpdateTime(now);
        });
        mongoTemplate.insertAll(policies);
    }
}
