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

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.ParentAgentActivityQueryReq;
import com.seer.teach.mp.admin.controller.resp.AdminParentAgentActivityResp;
import com.seer.teach.mp.admin.convert.AdminParentAgentActivityConvert;
import com.seer.teach.mp.entity.MpParentAgentActivityRelationEntity;
import com.seer.teach.mp.service.IMpParentAgentActivityRelationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@RequiredArgsConstructor
@Service
@Slf4j
public class AdminParentAgentActivityService {

    private final IMpParentAgentActivityRelationService parentAgentActivityRelationService;

    public PageListBean<AdminParentAgentActivityResp> pageList(ParentAgentActivityQueryReq query) {

        IPage<MpParentAgentActivityRelationEntity> pageParam = new Page<>(query.getPageNo(),query.getPageSize());

        LambdaQueryWrapper<MpParentAgentActivityRelationEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(query.getActivityId()), MpParentAgentActivityRelationEntity::getActivityId, query.getActivityId())
                .eq(Objects.nonNull(query.getAgentId()), MpParentAgentActivityRelationEntity::getAgentId, query.getAgentId())
                .eq(Objects.nonNull(query.getParentId()), MpParentAgentActivityRelationEntity::getParentId, query.getParentId())
                .like(Objects.nonNull(query.getActivityName()) && !query.getActivityName().isEmpty(), MpParentAgentActivityRelationEntity::getActivityName, query.getActivityName())
                .like(Objects.nonNull(query.getAgentName()) && !query.getAgentName().isEmpty(), MpParentAgentActivityRelationEntity::getAgentName, query.getAgentName())
                .eq(Objects.nonNull(query.getStatus()), MpParentAgentActivityRelationEntity::getStatus, query.getStatus())
                .orderByDesc(MpParentAgentActivityRelationEntity::getSignUpTime);

        IPage<MpParentAgentActivityRelationEntity> pageResult = parentAgentActivityRelationService.page(pageParam, wrapper);

        return PageConverterUtils.convertPageListBean(pageResult, AdminParentAgentActivityConvert.INSTANCE::convertToRespList);
    }

    public AdminParentAgentActivityResp getDetail(Integer id) {
        MpParentAgentActivityRelationEntity entity = parentAgentActivityRelationService.getById(id);
        return AdminParentAgentActivityConvert.INSTANCE.convertToResp(entity);
    }

    public boolean delete(List<Integer> ids) {
        return parentAgentActivityRelationService.removeByIds(ids);
    }
}
