package com.zhongzheng.controller.activity;

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

import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.core.page.TableDataInfo;
import com.zhongzheng.modules.activity.bo.ActivityAdvertisingAddBo;
import com.zhongzheng.modules.activity.bo.ActivityAdvertisingEditBo;
import com.zhongzheng.modules.activity.bo.ActivityAdvertisingQueryBo;
import com.zhongzheng.modules.activity.domain.ActivityAdvertising;
import com.zhongzheng.modules.activity.service.IActivityAdvertisingService;
import com.zhongzheng.modules.activity.vo.ActivityAdvertisingVo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhongzheng.common.annotation.Log;
import com.zhongzheng.common.core.controller.BaseController;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 广告发布储存Controller
 * 
 * @author change
 * @date 2021-05-17
 */
@Api(value = "广告发布控制器", tags = {"广告发布管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/advertising")
public class ActivityAdvertisingController extends BaseController {

    private final IActivityAdvertisingService iActivityAdvertisingService;

    /**
     * 查询广告发布储存列表
     */
    @ApiOperation("查询广告发布储存列表")
    @PreAuthorize("@ss.hasPermi('system:advertising:list')")
    @GetMapping("/list")
    public TableDataInfo<ActivityAdvertisingVo> list(ActivityAdvertisingQueryBo bo) {
        startPage();
        List<ActivityAdvertisingVo> list = iActivityAdvertisingService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 导出广告发布储存列表
     */
  /*  @ApiOperation("导出广告发布储存列表")
    @PreAuthorize("@ss.hasPermi('system:advertising:export')")
    @Log(title = "广告发布储存", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<ActivityAdvertisingVo> export(ActivityAdvertisingQueryBo bo) {
        List<ActivityAdvertisingVo> list = iActivityAdvertisingService.queryList(bo);
        ExcelUtil<ActivityAdvertisingVo> util = new ExcelUtil<ActivityAdvertisingVo>(ActivityAdvertisingVo.class);
        return util.exportExcel(list, "广告发布储存");
    }*/

    /**
     * 获取广告发布储存详细信息
     */
    @ApiOperation("获取广告发布储存详细信息")
    @PreAuthorize("@ss.hasPermi('system:advertising:query')")
    @GetMapping("/{id}")
    public AjaxResult<ActivityAdvertisingVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iActivityAdvertisingService.queryById(id));
    }

    /**
     * 新增广告发布储存
     */
    @ApiOperation("新增广告发布储存")
    @PreAuthorize("@ss.hasPermi('system:advertising:add')")
    @Log(title = "广告发布储存", businessType = BusinessType.INSERT)
    @PostMapping("addActivity")
    public AjaxResult add(@RequestBody ActivityAdvertisingAddBo bo) {
        long l = iActivityAdvertisingService.insertByAddBo(bo);
        String content = "新增广告：";
        content.concat((StrUtil.isBlank(bo.getAdvName())?"":"广告名称："+bo.getAdvName()));
        content.concat((StrUtil.isBlank(bo.getAdverUrl())?"":"广告图片："+bo.getAdverUrl()));
        content.concat((StrUtil.isBlank(bo.getJumpUrl())?"":"跳转url："+bo.getJumpUrl()));
        content.concat((bo.getStatus()==null)?"":"有效时间："+bo.getStartTime().toString());
        content.concat((bo.getStatus()==null)?"":"有效时间："+bo.getEndTime().toString());
        content.concat((StrUtil.isBlank(bo.getRemarks())?"":"备注："+bo.getRemarks()));
        content.concat((bo.getStatus()==null)?"":(bo.getReStatus()==1)?"发布方式：定时发布":"发布方式：手动发布");
        content.concat((bo.getStatus()==null)?"":(bo.getStatus()==1)?"状态：待发布":(bo.getStatus()==2)?"状态：撤回":"状态：发布成功");
        return AjaxResult.success_log(l,content);
    }

    /**
     * 修改广告发布储存
     */
    @ApiOperation("修改广告发布储存")
    @PreAuthorize("@ss.hasPermi('system:advertising:edit')")
    @Log(title = "广告发布储存", businessType = BusinessType.UPDATE)
    @PostMapping("updateActivity")
    public AjaxResult<Void> edit(@RequestBody ActivityAdvertisingEditBo bo) {
        ActivityAdvertisingVo activityAdvertising = iActivityAdvertisingService.updateByEditBo(bo);
        String content = "修改广告：";
        content.concat((StrUtil.isBlank(bo.getAdvName())?"":"修改广告名称："+bo.getAdvName()+"原名称："+activityAdvertising.getAdvName()));
        content.concat((StrUtil.isBlank(bo.getAdverUrl())?"":"广告图片："+bo.getAdverUrl()));
        content.concat((StrUtil.isBlank(bo.getJumpUrl())?"":"跳转url："+bo.getJumpUrl()));
        content.concat((bo.getStartTime()==null)?"":"有效时间："+bo.getStartTime().toString());
        content.concat((bo.getEndTime()==null)?"":"有效时间："+bo.getEndTime().toString());
        content.concat((StrUtil.isBlank(bo.getRemarks())?"":"备注："+bo.getRemarks()));
        content.concat((bo.getReStatus()==null)?"":(bo.getReStatus()==1)?"发布方式：定时发布":"发布方式：手动发布");
        content.concat((bo.getStatus()==null)?"":(bo.getStatus()==1)?"状态：待发布":(bo.getStatus()==2)?"状态：撤回":"状态：发布成功");
        return AjaxResult.success_log(bo.getId(),content);
    }

    /**
     * 删除广告发布储存
     */
    @ApiOperation("删除广告发布储存")
    @PreAuthorize("@ss.hasPermi('system:advertising:remove')")
    @Log(title = "广告发布储存" , businessType = BusinessType.DELETE)
    @PostMapping("deleteActivity")
    public AjaxResult<Void> remove(@RequestBody Long[] ids) {
        return toAjax(iActivityAdvertisingService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
