package com.miaxis.app.controller.ad;

import com.miaxis.ad.domain.AdInfo;
import com.miaxis.ad.service.IAdInfoService;
import com.miaxis.common.annotation.Log;
import com.miaxis.common.constant.Constants;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.page.ResponsePageInfo;
import com.miaxis.common.enums.BusinessTypeEnum;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 【ad】Controller
 *
 * @author miaxis
 * @date 2021-08-19
 */
@RestController
@RequestMapping(Constants.OPEN_PREFIX+"/ad/info")
@Api(tags={"【APP-广告滚动条】"})
public class AdInfoController extends BaseController{
    @Autowired
    private IAdInfoService adInfoService;

    /**
     * 查询ad列表
     */
    @GetMapping("/list")
    @ApiOperation("查询ad列表")
        @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<AdInfo> list(@ModelAttribute AdInfo adInfo){
        startPage();
        List<AdInfo> list = adInfoService.selectAdInfoList(adInfo);
        return toResponsePageInfo(list);
    }


    /**
     * 获取ad详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取ad详细信息")
    public Response<AdInfo> getInfo(
            @ApiParam(name = "id", value = "ad参数", required = true)
            @PathVariable("id") Long id
    ){
        return Response.success(adInfoService.getById(id));
    }

    /**
     * 新增ad
     */
    @Log(title = "ad", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation("新增ad")
    public Response<Integer> add(@RequestBody AdInfo adInfo){
        return toResponse(adInfoService.save(adInfo) ? 1 : 0);
    }

    /**
     * 修改ad
     */
    @Log(title = "ad", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    @ApiOperation("修改ad")
    public Response<Integer> edit(@RequestBody AdInfo adInfo){
        return toResponse(adInfoService.updateById(adInfo) ? 1 : 0);
    }

    /**
     * 删除ad
     */
    @Log(title = "ad", businessType = BusinessTypeEnum.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除ad")
    public  Response<Integer> remove(
            @ApiParam(name = "ids", value = "adids参数", required = true)
            @PathVariable Long[] ids
    ){
        return toResponse(adInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
