package com.ruoyi.brewery.controller;

import com.ruoyi.brewery.domain.ActivityExtendTimeVO;
import com.ruoyi.brewery.domain.WyActivity;
import com.ruoyi.brewery.domain.WyActivityBeer;
import com.ruoyi.brewery.domain.WyGoods;
import com.ruoyi.brewery.service.IWyActivityBeerService;
import com.ruoyi.brewery.service.IWyActivityService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 营销活动Controller
 *
 * @author kxl
 * @date 2024-12-21
 */
@Api(tags = "营销活动")
@RestController
@RequestMapping("/brewery/activity")
public class BreweryWyActivityController extends BaseController {
    @Autowired
    private IWyActivityService wyActivityService;

    /**
     * 品牌方活动数据统计
     */
    @ApiOperation("品牌方活动数据统计")
    @PreAuthorize("@bss.hasPermi('brewery:activity:statistics')")
    @GetMapping("/statistics")
    public AjaxResult statistics() {

        final HashMap<String, Integer> hashMap = wyActivityService.selectActivityDataByBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        return AjaxResult.success(hashMap);
    }

    /**
     * 查询营销活动列表
     */
    @ApiOperation("查询营销活动列表")
    @PreAuthorize("@bss.hasPermi('brewery:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyActivity wyActivity) {
        startPage();
        wyActivity.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        List<WyActivity> list = wyActivityService.selectWyActivityListWithData(wyActivity);
        return getDataTable(list);
    }

    /**
     * 导出营销活动列表
     */
    @ApiOperation("导出营销活动列表")
    @PreAuthorize("@bss.hasPermi('brewery:activity:export')")
    @Log(title = "营销活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyActivity wyActivity) {
        wyActivity.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        List<WyActivity> list = wyActivityService.selectWyActivityList(wyActivity);
        ExcelUtil<WyActivity> util = new ExcelUtil<WyActivity>(WyActivity.class);
        util.exportExcel(response, list, "营销活动数据");
    }

    /**
     * 获取营销活动详细信息
     */
    @ApiOperation("获取营销活动详细信息")
    @PreAuthorize("@bss.hasPermi('brewery:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        validate(id);
        return success(wyActivityService.selectWyActivityById(id));
    }

    /**
     * 新增营销活动
     */
    @ApiOperation("新增营销活动")
    @PreAuthorize("@bss.hasPermi('brewery:activity:add')")
    @Log(title = "营销活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyActivity wyActivity) {
        wyActivity.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        return toAjax(wyActivityService.insertWyActivity(wyActivity));
    }

    /**
     * 修改营销活动
     */
    @ApiOperation("修改营销活动")
    @PreAuthorize("@bss.hasPermi('brewery:activity:edit')")
    @Log(title = "营销活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyActivity wyActivity) {
        validate(wyActivity.getId());
        return toAjax(wyActivityService.updateWyActivity(wyActivity));
    }

    /**
     * 延长活动时间
     */
    @ApiOperation("延长活动时间")
    @PreAuthorize("@bss.hasPermi('brewery:activity:extendTime')")
    @Log(title = "延长活动时间", businessType = BusinessType.UPDATE)
    @PostMapping("/extendTime")
    public AjaxResult extendTime(@Validated @RequestBody ActivityExtendTimeVO vo) {
        validate(vo.getActivityId());
        final WyActivity activity = wyActivityService.selectWyActivityById(vo.getActivityId());
        if (activity.getStartDate().after(vo.getExtendTime())) {
            return error("延长时间不能早于活动开始时间");
        }
        if (vo.getExtendTime().before(DateUtils.getNowDate())) {
            return error("延长时间不能早于当前时间");
        }
        activity.setEndDate(vo.getExtendTime());
        return toAjax(wyActivityService.updateWyActivity(activity));
    }

    /**
     * 中止活动
     */
    @ApiOperation("中止活动")
    @PreAuthorize("@bss.hasPermi('brewery:activity:suspend')")
    @Log(title = "中止活动", businessType = BusinessType.UPDATE)
    @PostMapping("/suspend")
    public AjaxResult suspend(@RequestParam("activityId") Long activityId) {
        validate(activityId);
        final WyActivity activity = wyActivityService.selectWyActivityById(activityId);
        activity.setActivityStatus(0);
        return toAjax(wyActivityService.updateWyActivity(activity));
    }

    /**
     * 删除营销活动
     */
    @ApiOperation("删除营销活动")
    @PreAuthorize("@bss.hasPermi('brewery:activity:remove')")
    @Log(title = "营销活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        validate(id);
        return toAjax(wyActivityService.deleteWyActivityById(id));
    }


    private void validate(Long id) {
        final WyActivity wyActivity = wyActivityService.selectWyActivityById(id);
        if (wyActivity == null) {
            throw new IllegalArgumentException("活动不存在");
        }
        if (!SecurityUtils.getBreweryLoginUser().getBreweryId().equals(wyActivity.getBreweryId())) {
            throw new IllegalArgumentException("只能操作自己的品牌方的活动");
        }
    }
}
