package com.infoepoch.pms.agent.domain.baisc.user;

import com.infoepoch.pms.agent.common.utils.SnowFlake;
import com.infoepoch.pms.agent.platform.shared.exception.ValidationException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiangyz
 * @date 2026/4/10 10:09
 * 合作单位用户Service
 */
@Service
public class SupplierEmployeeService {

    @Autowired
    private ISupplierEmployeeRepository supplierEmployeeRepository;

    /**
     * 根据登录用户名查询合作单位用户
     *
     * @param username 登录用户名
     * @return 合作单位用户，不存在返回null
     */
    public SupplierEmployee getByUsername(String username) {
        String userName = StringUtils.trimToEmpty(username);
        if (StringUtils.isBlank(userName)) {
            throw new ValidationException("登录账号不可为空");
        }
        SupplierEmployeeCriteria criteria = new SupplierEmployeeCriteria();
        criteria.setUserName(userName);
        return supplierEmployeeRepository.selectOneByCriteria(criteria);
    }

    /**
     * 分页查询合作单位用户列表
     *
     * @param criteria 查询条件
     * @return 包含 totalCount 和 entityList 的结果集
     */
    public Map<String, Object> queryList(SupplierEmployeeCriteria criteria) {
        criteria.removeMapNullOrEmpty();
        if (criteria.byPage()){
            List<SupplierEmployee> entityList = supplierEmployeeRepository.selectCriteriaByPage(
                    criteria, criteria.getPageIndex(), criteria.getPageSize());
            int totalCount = supplierEmployeeRepository.selectCountByCriteria(criteria);
            Map<String, Object> map = new HashMap<>();
            map.put("totalCount", totalCount);
            map.put("entityList", entityList);
            return map;
        }
        return null;
    }

    /**
     * 根据ID查询合作单位用户
     *
     * @param id 唯一标识
     * @return 合作单位用户
     */
    public SupplierEmployee getById(String id) {
        if (StringUtils.isBlank(id)) {
            throw new ValidationException("唯一标识不能为空");
        }
        return supplierEmployeeRepository.selectById(id);
    }
}
