package com.iformall.controller.device;

import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.WxScreenAd;
import com.iformall.enums.EnumScreenAdStatus;
import com.iformall.enums.EnumScreenAdType;
import com.iformall.service.WxCampaignService;
import com.iformall.service.WxCouponChannelService;
import com.iformall.service.WxScreenAdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("wxScreenAd")
@Api(description="广告屏")
public class WxScreenAdController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
    private WxScreenAdService wxScreenAdService;

	@Autowired
	private WxCampaignService wxCampaignService;

	@Autowired
	private WxCouponChannelService wxCouponChannelService;


	@ApiOperation("广告列表")
	@GetMapping("list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "广告屏-广告列表")
	public Result list(@ModelAttribute WxScreenAd wxScreenAd, Integer pageNum, Integer pageSize) {
		if (wxScreenAd == null) wxScreenAd = new WxScreenAd();
		wxScreenAd.setTenantId(getTenantId());
		wxScreenAd.setStatus(EnumScreenAdStatus.VALID.getCode());
		return new ResultData(wxScreenAdService.listAsPage(wxScreenAd,pageNum,pageSize));
	}

    @ApiOperation("添加广告")
    @PostMapping("add")
    @SystemControllerLog(description = "广告屏-添加广告")
    public Result add(@RequestBody WxScreenAd wxScreenAd) {
		if (wxScreenAd == null) wxScreenAd = new WxScreenAd();
		wxScreenAd.setStatus(EnumScreenAdStatus.VALID.getCode());
		wxScreenAd.setTenantId(getTenantId());
		return wxScreenAdService.saveOrUpdate(wxScreenAd);
    }

	@ApiOperation("更新广告")
	@PostMapping("update")
    @SystemControllerLog(description = "广告屏-更新广告")
	public Result update(@RequestBody WxScreenAd wxScreenAd) {
    	if (wxScreenAd == null)
    		return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		if (wxScreenAd.getId() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		wxScreenAd.setTenantId(getTenantId());
		wxScreenAdService.saveOrUpdate(wxScreenAd);
		return new ResultData();
	}

	@ApiOperation("广告详情")
	@GetMapping("detail")
    @SystemControllerLog(description = "广告屏-广告详情")
	public Result detail(@RequestParam Long id) {
		if (id == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		WxScreenAd wxScreenAd = wxScreenAdService.getById(id);
		if (wxScreenAd == null)
			return new ResultData(ErrorCode.SCREENAD_NOT_FOUND);

		Object target = null;
		if (wxScreenAd.getType().equals(EnumScreenAdType.COUPON.getCode()))
			target = wxCouponChannelService.findDetailVo(wxScreenAd.getTargetId());
		if (wxScreenAd.getType().equals(EnumScreenAdType.COUPON.getCode()))
			target = wxCampaignService.getById(wxScreenAd.getTargetId());

		wxScreenAd.setTarget(target);
		return new ResultData(wxScreenAd);
	}

	@ApiOperation("删除广告")
	@PostMapping("delete")
    @SystemControllerLog(description = "广告屏-删除广告")
	public Result delete(@RequestParam Long id) {
		if (id == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		WxScreenAd wxScreenAd = new WxScreenAd();
		wxScreenAd.setId(id);
		wxScreenAd.setTenantId(getTenantId());
		wxScreenAd.setStatus(EnumScreenAdStatus.INVALID.getCode());
		wxScreenAdService.saveOrUpdate(wxScreenAd);
		return new ResultData();
	}

}
