package com.gxwebsoft.cms.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.cms.entity.Ad;
import com.gxwebsoft.cms.param.AdParam;
import com.gxwebsoft.cms.service.AdService;
import com.gxwebsoft.cms.vo.SideVo;
import com.gxwebsoft.common.core.utils.JSONUtil;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 广告位管理表控制器
 *
 * @author 科技小王子
 * @since 2024-03-04 21:14:15
 */
@Slf4j
@Api(tags = "广告")
@RestController
@RequestMapping("/api/cms/ad")
public class AdController extends BaseController {
    @Resource
    private AdService adService;

    @ApiOperation("分页查询广告位管理表")
    @GetMapping("/page")
    public ApiResult<PageResult<Ad>> page(AdParam param) {
        // 使用关联查询
        return success(adService.pageRel(param));
    }

    @ApiOperation("查询全部广告位管理表")
    @GetMapping()
    public ApiResult<List<Ad>> list(AdParam param) {
        PageParam<Ad, AdParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(adService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(adService.listRel(param));
    }

    @ApiOperation("根据id查询广告位管理表")
    @GetMapping("/{id}")
    public ApiResult<Ad> get(@PathVariable("id") Integer id) {
        return success(adService.getById(id));
        // 使用关联查询
        //return success(adService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('cms:ad:save')")
    @ApiOperation("添加广告位管理表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Ad ad) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          ad.setUserId(loginUser.getUserId());
        }
        if (adService.save(ad)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:ad:update')")
    @ApiOperation("修改广告位管理表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Ad ad) {
        if (adService.updateById(ad)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:ad:remove')")
    @ApiOperation("删除广告位管理表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (adService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:ad:save')")
    @ApiOperation("批量添加广告位管理表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Ad> list) {
        if (adService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:ad:update')")
    @ApiOperation("批量修改广告位管理表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Ad> batchParam) {
        if (batchParam.update(adService, "ad_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:ad:remove')")
    @ApiOperation("批量删除广告位管理表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (adService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("首页幻灯片")
    @GetMapping("/side")
    public ApiResult<?> getSide() {
      final Ad ad = adService.getOne(new LambdaQueryWrapper<Ad>().eq(Ad::getPageName, "首页").eq(Ad::getAdType, "幻灯片").last("limit 1"));
      System.out.println("adService = " + ad);
      if (ObjectUtil.isNotEmpty(ad)) {
        ad.setData(JSONUtil.parseObject(ad.getImages(), Object.class));
        return success(ad);
      }
      return fail("幻灯片未设置");
    }

}
