package com.ruoyi.brewery.controller;

import com.ruoyi.bar.domain.WyActivityJoinBatch;
import com.ruoyi.bar.domain.WyBeerQrcodeScanLog;
import com.ruoyi.bar.service.IWyActivityJoinBatchService;
import com.ruoyi.bar.service.IWyBarService;
import com.ruoyi.bar.service.IWyBeerQrcodeScanLogService;
import com.ruoyi.brewery.domain.WyActivityReward;
import com.ruoyi.brewery.domain.WyBeerQrcode;
import com.ruoyi.brewery.domain.WyBeerQrcodeBatch;
import com.ruoyi.brewery.service.IWyActivityRewardService;
import com.ruoyi.brewery.service.IWyBeerQrcodeService;
import com.ruoyi.brewery.service.IWyBeerService;
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.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Api(tags = "酒品二维码扫码核销记录")
@RestController
@RequestMapping("/brewery/qrcodeScanLog")
public class BreweryWyBeerQrcodeScanLogController extends BaseController {
    @Resource
    private IWyBeerQrcodeService wyBeerQrcodeService;
    @Resource
    private IWyBeerQrcodeScanLogService wyBeerQrcodeScanLogService;
    @Resource
    private IWyBarService wyBarService;
    @Resource
    private IWyBeerService wyBeerService;
    @Resource
    private IWyActivityJoinBatchService activityJoinBatchService;
    @Resource
    private IWyActivityRewardService rewardService;

    /**
     * 查询所有二维码扫码记录(无效和有效的)
     */
    @ApiOperation("查询所有二维码扫码记录(无效和有效的)")
    @PreAuthorize("@bss.hasPermi('brewery:qrcodeScanLog:allList')")
    @GetMapping("/allList")
    public TableDataInfo allList(WyBeerQrcodeScanLog scanLog) {
        scanLog.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        startPage();
        final List<WyBeerQrcodeScanLog> wyBeerQrcodeScanLogs = wyBeerQrcodeScanLogService.selectWyBeerQrcodeScanLogList(scanLog);
        return getDataTable(wyBeerQrcodeScanLogs);
    }

    /**
     * 查询所有二维码扫码核销记录
     */
    @ApiOperation("查询所有二维码扫码核销记录")
    @PreAuthorize("@bss.hasPermi('brewery:qrcodeScanLog:consumeList')")
    @GetMapping("/consumeList")
    public TableDataInfo consumeList(WyBeerQrcodeScanLog scanLog) {
        scanLog.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        scanLog.setScanStatus(1);
        startPage();
        final List<WyBeerQrcodeScanLog> wyBeerQrcodeScanLogs = wyBeerQrcodeScanLogService.selectWyBeerQrcodeScanLogList(scanLog);
        return getDataTable(wyBeerQrcodeScanLogs);
    }

    /**
     * 查询所有二维码无效的扫码记录
     */
    @ApiOperation("查询所有二维码无效的扫码记录")
    @PreAuthorize("@bss.hasPermi('brewery:qrcodeScanLog:checkoutList')")
    @GetMapping("/checkoutList")
    public TableDataInfo checkoutList(WyBeerQrcodeScanLog scanLog) {
        scanLog.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        scanLog.setScanStatus(0);
        startPage();
        final List<WyBeerQrcodeScanLog> wyBeerQrcodeScanLogs = wyBeerQrcodeScanLogService.selectWyBeerQrcodeScanLogList(scanLog);
        return getDataTable(wyBeerQrcodeScanLogs);
    }

    /**
     * 查询某个活动扫码记录
     */
    @ApiOperation("查询某个活动扫码记录")
    @PreAuthorize("@bss.hasPermi('brewery:qrcodeScanLog:activityConsumeList')")
    @GetMapping("/activityConsumeList")
    public TableDataInfo activityConsumeList(WyBeerQrcodeScanLog scanLog) {
        if (scanLog.getActivityId() == null) {
            throw new IllegalArgumentException("活动id不能为空");
        }
        scanLog.setActivitySupplier(1);

        scanLog.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        scanLog.setScanStatus(1);
        startPage();
        final List<WyBeerQrcodeScanLog> wyBeerQrcodeScanLogs = wyBeerQrcodeScanLogService.selectWyBeerQrcodeScanLogList(scanLog);
        return getDataTable(wyBeerQrcodeScanLogs);
    }

    /**
     * 查询某个活动参与情况(分批次)
     */
    @ApiOperation("查询某个活动参与情况")
    @PreAuthorize("@bss.hasPermi('brewery:qrcodeScanLog:activityBatchList')")
    @GetMapping("/activityBatchList")
    public TableDataInfo activityBatchList(WyActivityJoinBatch batch) {
        if (batch.getActivityId() == null) {
            throw new IllegalArgumentException("活动id不能为空");
        }
        batch.setActivitySupplier(1);
        batch.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        startPage();
        final List<WyActivityJoinBatch> wyActivityJoinBatches = activityJoinBatchService.selectWyActivityJoinBatchList(batch);
        return getDataTable(wyActivityJoinBatches);
    }

    /**
     * 查询某个活动参与情况详情(按参与批次id)
     */
    @ApiOperation("查询某个活动参与情况详情(按参与批次id)")
    @PreAuthorize("@bss.hasPermi('brewery:qrcodeScanLog:activityBatchDetail')")
    @GetMapping("/activityBatchDetail")
    public AjaxResult activityBatchDetail(@RequestParam("batchId") Long batchId) {
        final WyActivityJoinBatch wyActivityJoinBatch = activityJoinBatchService.selectWyActivityJoinBatchByBatchIdWithData(batchId);
        if (!Objects.equals(wyActivityJoinBatch.getBreweryId(), SecurityUtils.getBreweryLoginUser().getBreweryId())) {
            return AjaxResult.error("批次id不属于当前品牌方");
        }
        return AjaxResult.success(wyActivityJoinBatch);
    }

    /**
     * 某个活动数据统计
     */
    @ApiOperation("某个活动数据统计")
    @PreAuthorize("@bss.hasPermi('brewery:qrcodeScanLog:statistics')")
    @GetMapping("/statistics")
    public AjaxResult statistics(@RequestParam("activityId") Long activityId) {
        HashMap<String, Object> map = new HashMap<>();
        final WyBeerQrcodeScanLog wyBeerQrcodeScanLog = new WyBeerQrcodeScanLog();
        wyBeerQrcodeScanLog.setActivityId(activityId);
        wyBeerQrcodeScanLog.setActivitySupplier(1);
        wyBeerQrcodeScanLog.setScanStatus(1);
        final List<WyBeerQrcodeScanLog> wyBeerQrcodeScanLogs = wyBeerQrcodeScanLogService.selectWyBeerQrcodeScanLogList(wyBeerQrcodeScanLog);
        map.put("consumeCount", wyBeerQrcodeScanLogs.size());
        //参加门店数
        final long count = wyBeerQrcodeScanLogs.stream().map(WyBeerQrcodeScanLog::getBarId).distinct().count();
        map.put("consumeBarCount", count);
        final WyActivityReward wyActivityReward = new WyActivityReward();
        wyActivityReward.setActivityId(activityId);
        wyActivityReward.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        wyActivityReward.setRewardStatus(0);
        final Integer waitRewardCount = rewardService.selectWyActivityRewardCount(wyActivityReward);
        map.put("waitRewardCount", waitRewardCount);
        wyActivityReward.setRewardStatus(2);
        final Integer completeCount = rewardService.selectWyActivityRewardCount(wyActivityReward);
        map.put("completeCount", completeCount);
        return AjaxResult.success(map);
    }


    /**
     * 撤销核销记录
     */
    @ApiOperation("撤销核销记录")
    @PreAuthorize("@bss.hasPermi('brewery:qrcodeScanLog:cancelConsume')")
    @Log(title = "撤销核销记录", businessType = BusinessType.UPDATE)
    @PostMapping("/cancelConsume")
    public AjaxResult cancelConsume(@RequestParam("logId") Long logId) {
        final WyBeerQrcodeScanLog wyBeerQrcodeScanLog = wyBeerQrcodeScanLogService.selectWyBeerQrcodeScanLogById(logId);
        if (wyBeerQrcodeScanLog == null) {
            return AjaxResult.error("记录不存在");
        }
        if (!Objects.equals(wyBeerQrcodeScanLog.getBreweryId(), SecurityUtils.getBreweryLoginUser().getBreweryId())) {
            return AjaxResult.error("qr码不属于当前酒厂");
        }
        wyBeerQrcodeScanLog.setScanStatus(0);
        wyBeerQrcodeScanLog.setRemark("撤销核销");
        return toAjax(wyBeerQrcodeScanLogService.updateWyBeerQrcodeScanLog(wyBeerQrcodeScanLog));
    }

    /**
     * 查询某个二维码扫码记录(无效和有效的)
     */
    @ApiOperation("查询某个二维码扫码记录(无效和有效的)")
    @PreAuthorize("@bss.hasPermi('brewery:qrcodeScanLog:qrcodeScanLogList')")
    @GetMapping("/qrcodeScanLogList")
    public AjaxResult qrcodeScanLogList(@RequestParam("qrcodeId") Long qrcodeId) {
        WyBeerQrcodeScanLog scanLog = new WyBeerQrcodeScanLog();
        scanLog.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        scanLog.setCodeId(qrcodeId);
        final List<WyBeerQrcodeScanLog> wyBeerQrcodeScanLogs = wyBeerQrcodeScanLogService.selectWyBeerQrcodeScanLogList(scanLog);
        return AjaxResult.success(wyBeerQrcodeScanLogs);
    }
}
