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.CopyrightMapper;
import top.continew.admin.business.model.entity.Copyright;
import top.continew.admin.business.model.query.CopyrightPageQuery;
import top.continew.admin.business.model.query.CopyrightQuery;
import top.continew.admin.business.model.req.CopyrightReq;
import top.continew.admin.business.model.resp.CopyrightDetailResp;
import top.continew.admin.business.model.resp.CopyrightResp;
import top.continew.admin.business.service.CopyrightService;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;

import java.util.List;

/**
 * 数字人-文案服务实现类
 *
 * @author 王志维
 * @since 2025/12/29
 */
@Service
@RequiredArgsConstructor
public class CopyrightServiceImpl extends BaseServiceImpl<CopyrightMapper, Copyright, CopyrightResp, CopyrightDetailResp, CopyrightQuery, CopyrightReq> implements CopyrightService {

    private final CopyrightMapper copyrightMapper;

    @Override
    public PageResp<CopyrightResp> pageList(CopyrightQuery query, CopyrightPageQuery pageQuery) {
        LambdaQueryWrapper<Copyright> wrapper = new LambdaQueryWrapper<>();
        if (query.getUserId() != null) {
            wrapper.eq(Copyright::getUserId, query.getUserId());
        }
        if (query.getText() != null) {
            wrapper.like(Copyright::getText, query.getText());
        }

        // 默认格局createTime 倒叙排列
        wrapper.orderByDesc(Copyright::getCreateTime);

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

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

    @Override
    public Long add(CopyrightReq req) {
        Copyright copyright = new Copyright();
        BeanUtils.copyProperties(req, copyright);
        copyright.setCreateTime(java.time.LocalDateTime.now());
        copyright.setUpdateTime(java.time.LocalDateTime.now());
        copyrightMapper.insert(copyright);
        return copyright.getId();
    }

    @Override
    public List<Long> batchAdd(List<CopyrightReq> reqList) {
        // 获取当前时间，统一设置创建时间和更新时间
        java.time.LocalDateTime now = java.time.LocalDateTime.now();

        // 转换为实体列表并设置时间
        List<Copyright> copyrightList = reqList.stream().map(req -> {
            Copyright copyright = new Copyright();
            BeanUtils.copyProperties(req, copyright);
            copyright.setCreateTime(now);
            copyright.setUpdateTime(now);
            return copyright;
        }).toList();

        // 批量插入
        copyrightList.forEach(copyrightMapper::insert);

        // 返回新增的ID列表
        return copyrightList.stream().map(Copyright::getId).toList();
    }

    private CopyrightResp convertToResp(Copyright copyright) {
        CopyrightResp resp = new CopyrightResp();
        BeanUtils.copyProperties(copyright, resp);
        return resp;
    }
}