package top.continew.admin.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.continew.admin.business.mapper.EffectMapper;
import top.continew.admin.business.model.entity.Effect;
import top.continew.admin.business.model.query.EffectPageQuery;
import top.continew.admin.business.model.query.EffectQuery;
import top.continew.admin.business.model.req.EffectReq;
import top.continew.admin.business.model.resp.EffectDetailResp;
import top.continew.admin.business.model.resp.EffectResp;
import top.continew.admin.business.service.EffectService;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;

/**
 * 数字人-特效服务实现类
 *
 * @author 王志维
 * @since 2025/12/29
 */
@Service
@RequiredArgsConstructor
public class EffectServiceImpl extends BaseServiceImpl<EffectMapper, Effect, EffectResp, EffectDetailResp, EffectQuery, EffectReq> implements EffectService {

    private final EffectMapper effectMapper;

    @Override
    public PageResp<EffectResp> pageList(EffectQuery query, EffectPageQuery pageQuery) {
        LambdaQueryWrapper<Effect> wrapper = new LambdaQueryWrapper<>();
        if (query.getName() != null) {
            wrapper.like(Effect::getName, query.getName());
        }
        if (query.getType() != null) {
            wrapper.eq(Effect::getType, query.getType());
        }
        if (query.getTemplateId() != null) {
            wrapper.eq(Effect::getTemplateId, query.getTemplateId());
        }
        if (query.getDigitalHumanId() != null) {
            wrapper.eq(Effect::getDigitalHumanId, query.getDigitalHumanId());
        }

        wrapper.orderByDesc(Effect::getCreateTime);

        Page<Effect> page = new Page<>(pageQuery.getPage(), pageQuery.getSize());
        Page<Effect> result = effectMapper.selectPage(page, wrapper);

        PageResp<EffectResp> pageResp = new PageResp();
        pageResp.setTotal(result.getTotal());
        pageResp.setList(result.getRecords().stream().map(this::convertToResp).toList());
        return pageResp;
    }

    @Override
    public Long add(EffectReq req) {
        Effect effect = new Effect();
        BeanUtils.copyProperties(req, effect);
        effect.setCreateTime(java.time.LocalDateTime.now());
        effect.setUpdateTime(java.time.LocalDateTime.now());
        effectMapper.insert(effect);
        return effect.getId();
    }

    private EffectResp convertToResp(Effect effect) {
        EffectResp resp = new EffectResp();
        BeanUtils.copyProperties(effect, resp);
        return resp;
    }

    @Override
    public void update(EffectReq req, Long id) {
        Effect effect = new Effect();
        BeanUtils.copyProperties(req, effect);
        effect.setId(id);
        effect.setUpdateTime(java.time.LocalDateTime.now());
        effectMapper.updateById(effect);
    }
}