package com.seer.teach.mp.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mp.admin.controller.req.AgentActivityLogQueryReq;
import com.seer.teach.mp.admin.controller.resp.AdminAgentActivityLogResp;
import com.seer.teach.mp.admin.service.IAdminAgentActivityLogService;
import com.seer.teach.mp.entity.MpAgentActivityLogEntity;
import com.seer.teach.mp.service.IMpAgentActivityLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 代理商活动操作日志表 管理端服务实现类
 * </p>
 *
 * @author
 * @since 2025-12-30
 */
@Service
@RequiredArgsConstructor
public class AdminAgentActivityLogServiceImpl implements IAdminAgentActivityLogService {

    private final IMpAgentActivityLogService mpAgentActivityLogService;

    @Override
    public PageListBean<AdminAgentActivityLogResp> pageList(AgentActivityLogQueryReq query) {
        Page<MpAgentActivityLogEntity> page = new Page<>(query.getPageNum(), query.getPageSize());
        LambdaQueryWrapper<MpAgentActivityLogEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(query.getActivityId() != null, MpAgentActivityLogEntity::getActivityId, query.getActivityId())
                .eq(query.getOperatorId() != null, MpAgentActivityLogEntity::getOperatorId, query.getOperatorId())
                .eq(query.getOperationType() != null, MpAgentActivityLogEntity::getOperationType, query.getOperationType())
                .ge(query.getStartTime() != null, MpAgentActivityLogEntity::getCreateTime, query.getStartTime())
                .le(query.getEndTime() != null, MpAgentActivityLogEntity::getCreateTime, query.getEndTime())
                .orderByDesc(MpAgentActivityLogEntity::getUpdateTime);

        IPage<MpAgentActivityLogEntity> result = mpAgentActivityLogService.page(page, wrapper);

        return PageConverterUtils.convertPageList(result, this::convertToResp);
    }

    @Override
    public AdminAgentActivityLogResp getById(Integer id) {
        MpAgentActivityLogEntity entity = mpAgentActivityLogService.getById(id);
        return convertToResp(entity);
    }

    @Override
    public boolean deleteLog(Integer id) {
        return mpAgentActivityLogService.removeById(id);
    }

    /**
     * 将实体转换为响应对象
     */
    private AdminAgentActivityLogResp convertToResp(MpAgentActivityLogEntity entity) {
        AdminAgentActivityLogResp resp = new AdminAgentActivityLogResp();
        resp.setId(entity.getId());
        resp.setActivityId(entity.getActivityId());
        resp.setOperatorId(entity.getOperatorId());
        resp.setOperationType(entity.getOperationType());
        resp.setDescription(entity.getDescription());
        resp.setBeforeData(entity.getBeforeData());
        resp.setAfterData(entity.getAfterData());
        resp.setCreateTime(entity.getCreateTime());
        resp.setUpdateTime(entity.getUpdateTime());
        return resp;
    }
}