package com.fs.company.controller.newAdv;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
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.fs.common.result.Result;
import com.fs.newAdv.domain.AdvChannelEntity;
import com.fs.newAdv.dto.req.ChannelSaveBatchReq;
import com.fs.newAdv.service.IAdvChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 广告渠道表 前端控制器
 * </p>
 *
 * @author zhangqin
 * @since 2025-11-27
 */
@RestController
@RequestMapping("/adv/channel")
public class AdvChannelController {

    @Autowired
    private IAdvChannelService advChannelService;

    /**
     * 分页查询渠道
     */
    @GetMapping("/page")
    public Result<IPage<AdvChannelEntity>> page(
            @RequestParam(defaultValue = "1") Long pageNum,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) String channelName,
            @RequestParam(required = false) Long parentId) {

        Page<AdvChannelEntity> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<AdvChannelEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(channelName), AdvChannelEntity::getChannelName, channelName);
        wrapper.eq(ObjectUtil.isNotEmpty(parentId), AdvChannelEntity::getParentId, parentId);
        wrapper.ne(ObjectUtil.isEmpty(parentId), AdvChannelEntity::getParentId, 0);
        wrapper.orderByDesc(AdvChannelEntity::getCreateTime);
        IPage<AdvChannelEntity> result = advChannelService.page(page, wrapper);
        return Result.success(result);
    }

    /**
     * 创建渠道/修改渠道
     */
    @PostMapping("/addOrUpdate")
    public Result<Void> create(@RequestBody AdvChannelEntity advertiser) {
        boolean success = false;
        if (advertiser.getId() != null) {
            success = advChannelService.updateById(advertiser);
        } else {
            success = advChannelService.save(advertiser);
        }
        return success ? Result.success() : Result.error("创建失败");
    }

    /**
     * 批量新增
     */
    @PostMapping("/saveBatch")
    public Result<Void> saveBatch(@RequestBody ChannelSaveBatchReq saveBatch) {
        // 参数验证
        int num = saveBatch.getNum();
        if (num <= 0 || num > 10000) {
            return Result.error("批量数量必须在1-10000之间");
        }

        String channelName = saveBatch.getChannelName();
        if (StrUtil.isBlank(channelName)) {
            return Result.error("渠道名称不能为空");
        }

        List<AdvChannelEntity> list = new ArrayList<>(num);
        Integer start = saveBatch.getStart();
        Long parentId = saveBatch.getParentId();

        for (int i = 0; i < num; i++) {
            AdvChannelEntity entity = new AdvChannelEntity();
            entity.setChannelName(new StringBuilder()
                    .append(channelName)
                    .append("-")
                    .append(start++)
                    .toString());
            entity.setParentId(parentId);
            list.add(entity);
        }
        try {
            return advChannelService.saveBatch(list) ? Result.success() : Result.error("创建失败");
        } catch (Exception e) {
            return Result.error("批量创建失败：" + e.getMessage());
        }
    }
}
