package com.slodon.b2b2c.controller.system.admin;

import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.aop.OperationLogger;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.model.system.PcFirstAdvModel;
import com.slodon.b2b2c.system.example.PcFirstAdvExample;
import com.slodon.b2b2c.system.pojo.PcFirstAdv;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "admin-首页首页广告设置设置（开屏图、顶部广告）")
@RestController
@RequestMapping("v3/system/admin/pcFirstAdv")
public class AdminPcFirstAdvController {

    @Resource
    private PcFirstAdvModel pcFirstAdvModel;

    @ApiOperation("获取pc首页广告")
    @GetMapping("get")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "站点",defaultValue = "1"),
            @ApiImplicitParam(name = "type", value = "广告类型,1-弹层广告，2-顶部广告", required = false,defaultValue = "1")
    })
    public JsonResult<PcFirstAdv> get(@RequestParam(value = "webSite",required = false,defaultValue = "1") String webSite,@RequestParam(value = "type",required = false,defaultValue = "1") Integer type) {
        AssertUtil.isTrue(type != 1 && type != 2 ,"参数不对");
        PcFirstAdvExample example = new PcFirstAdvExample();
        example.setAdvId(type);
        example.setWebSite(webSite);
        List<PcFirstAdv> pcFirstAdvListList = pcFirstAdvModel.getPcFirstAdvList(example, null);
        if (CollectionUtils.isEmpty(pcFirstAdvListList)){
            return SldResponse.success();
        }
        return SldResponse.success(pcFirstAdvListList.get(0));
    }

    @ApiOperation("更新pc首页弹层广告")
    @OperationLogger(option = "更新pc首页广告")
    @PostMapping("update")
    public JsonResult update(HttpServletRequest request, PcFirstAdv pcFirstAdv) {

        PcFirstAdv record = new PcFirstAdv();
        record.setData(pcFirstAdv.getData());

        PcFirstAdvExample example=new PcFirstAdvExample();
        example.setAdvId(pcFirstAdv.getAdvId());
        example.setWebSite(pcFirstAdv.getWebSite());
        pcFirstAdvModel.updateByExampleSelective(record,example);
        return SldResponse.success("更新成功");
    }
}
