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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.ActivityFormExecutionQueryReq;
import com.seer.teach.mp.admin.controller.resp.AdminActivityFormExecutionResp;
import com.seer.teach.mp.admin.convert.AdminActivityFormExecutionConvert;
import com.seer.teach.mp.admin.service.IAdminActivityFormExecutionService;
import com.seer.teach.mp.entity.MpActivityFormExecutionEntity;
import com.seer.teach.mp.service.IMpActivityFormExecutionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 管理端活动表单执行实例服务实现类
 * </p>
 *
 * @author Lingma
 * @since 2026-01-10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminActivityFormExecutionServiceImpl implements IAdminActivityFormExecutionService {

    private final IMpActivityFormExecutionService activityFormExecutionService;

    @Override
    public PageListBean<AdminActivityFormExecutionResp> pageList(ActivityFormExecutionQueryReq query) {
        Page<MpActivityFormExecutionEntity> page = new Page<>(query.getPageNo(), query.getPageSize());
        
        LambdaQueryWrapper<MpActivityFormExecutionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(query.getActivityId() != null, MpActivityFormExecutionEntity::getActivityId, query.getActivityId())
               .eq(query.getAgentId() != null, MpActivityFormExecutionEntity::getAgentId, query.getAgentId())
               .eq(query.getFormId() != null, MpActivityFormExecutionEntity::getFormId, query.getFormId())
               .eq(StringUtils.isNotBlank(query.getStatus()), 
                   MpActivityFormExecutionEntity::getStatus, query.getStatus())
               .like(StringUtils.isNotBlank(query.getSubmitterName()), 
                     MpActivityFormExecutionEntity::getSubmitterName, query.getSubmitterName())
               .orderByDesc(MpActivityFormExecutionEntity::getId);

        Page<MpActivityFormExecutionEntity> pageResult = activityFormExecutionService.page(page, wrapper);


        return PageConverterUtils.convertPageList(pageResult, AdminActivityFormExecutionConvert.INSTANCE::convertToResp);
    }

    @Override
    public Boolean saveOrUpdate(ActivityFormExecutionQueryReq request) {
        MpActivityFormExecutionEntity entity = new MpActivityFormExecutionEntity();
        BeanUtil.copyProperties(request, entity);
        
        if (request.getId() == null) {
            // 新增时设置默认值
            entity.setStatus(entity.getStatus() == null ? "draft" : entity.getStatus());
        }
        
        return activityFormExecutionService.saveOrUpdate(entity);
    }

    @Override
    public Boolean deleteById(Integer id) {
        return activityFormExecutionService.removeById(id);
    }

    @Override
    public AdminActivityFormExecutionResp getById(Integer id) {
        if (id == null) {
            return null;
        }
        MpActivityFormExecutionEntity entity = activityFormExecutionService.getById(id);
        return AdminActivityFormExecutionConvert.INSTANCE.convertToResp(entity);
    }

    @Override
    public Boolean submitById(Integer id) {
        MpActivityFormExecutionEntity entity = activityFormExecutionService.getById(id);
        if (entity == null) {
            return false;
        }
        entity.setStatus("submitted");
        return activityFormExecutionService.updateById(entity);
    }
}