package com.yiqi.admin.controller.activity;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.activity.domain.vo.ActivityRechargeItemVO;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.DeleteStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yiqi.common.annotation.Log;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.activity.domain.ActivityRechargeItem;
import com.yiqi.activity.service.IActivityRechargeItemService;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.common.core.page.TableDataInfo;

/**
 * 充值多送活动明细Controller
 *
 * @author xiaohei
 * @date 2023-09-10
 */
@Api(tags = "管理后台 - 充值多送活动明细")
@RestController
@RequestMapping(UrlConstants.managerApi + "/activity/rechargeItem")
public class ActivityRechargeItemController extends BaseController {
    @Autowired
    private IActivityRechargeItemService activityRechargeItemService;

    /**
     * 查询充值多送活动明细列表
     */
    @PreAuthorize("@ss.hasPermi('activity:rechargeInfo:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询充值多送活动明细列表")
    public TableDataInfo list(ActivityRechargeItem activityRechargeItem) {
        startPage();
        List<ActivityRechargeItemVO> list = activityRechargeItemService.selectActivityRechargeItemList(activityRechargeItem);
        return getDataTable(list);
    }

    /**
     * 导出充值多送活动明细列表
     */
    @PreAuthorize("@ss.hasPermi('activity:rechargeInfo:export')")
    @Log(title = "充值多送活动明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出充值多送活动明细列表")
    public void export(HttpServletResponse response, ActivityRechargeItem activityRechargeItem) {
        List<ActivityRechargeItemVO> list = activityRechargeItemService.selectActivityRechargeItemList(activityRechargeItem);
        ExcelUtil<ActivityRechargeItemVO> util = new ExcelUtil<ActivityRechargeItemVO>(ActivityRechargeItemVO. class);
        util.exportExcel(response, list, "充值多送活动明细数据");
    }

    /**
     * 获取充值多送活动明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('activity:rechargeInfo:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取充值多送活动明细详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(activityRechargeItemService.selectActivityRechargeItemById(id));
    }

    /**
     * 新增充值多送活动明细
     */
    @PreAuthorize("@ss.hasPermi('activity:rechargeInfo:add')")
    @Log(title = "充值多送活动明细", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增充值多送活动明细")
    public AjaxResult add(@RequestBody ActivityRechargeItem activityRechargeItem) {
        if(activityRechargeItemService.count(new QueryWrapper<ActivityRechargeItem>().lambda()
                .eq(ActivityRechargeItem::getActivityId,activityRechargeItem.getActivityId())
                .eq(ActivityRechargeItem::getRechargeTypeId,activityRechargeItem.getRechargeTypeId())
                .eq(ActivityRechargeItem::getDelFlag, DeleteStatus.OK.getCode())) > 0){
            throw new ServiceException("已经配置过这个充值类型");
        }
        activityRechargeItem.setCreateById(getUserId());
        activityRechargeItem.setCreateBy(getUsername());
        return toAjax(activityRechargeItemService.insertActivityRechargeItem(activityRechargeItem));
    }

    /**
     * 修改充值多送活动明细
     */
    @PreAuthorize("@ss.hasPermi('activity:rechargeInfo:edit')")
    @Log(title = "充值多送活动明细", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改充值多送活动明细")
    public AjaxResult edit(@RequestBody ActivityRechargeItem activityRechargeItem) {
        if(activityRechargeItemService.count(new QueryWrapper<ActivityRechargeItem>().lambda()
                .eq(ActivityRechargeItem::getActivityId,activityRechargeItem.getActivityId())
                .eq(ActivityRechargeItem::getRechargeTypeId,activityRechargeItem.getRechargeTypeId())
                .eq(ActivityRechargeItem::getDelFlag, DeleteStatus.OK.getCode())
                .ne(ActivityRechargeItem::getId,activityRechargeItem.getId())) > 0){
            throw new ServiceException("已经配置过这个充值类型");
        }
        activityRechargeItem.setUpdateById(getUserId());
        activityRechargeItem.setUpdateBy(getUsername());
        return toAjax(activityRechargeItemService.updateActivityRechargeItem(activityRechargeItem));
    }

    /**
     * 删除充值多送活动明细
     */
    @PreAuthorize("@ss.hasPermi('activity:rechargeInfo:remove')")
    @Log(title = "充值多送活动明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除充值多送活动明细")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(activityRechargeItemService.deleteActivityRechargeItemByIds(ids));
    }


    @GetMapping(value = "/getRechargeTypeRelationActivityInfo")
    @ApiOperation(value = "获取套餐相关的活动信息")
    public R getRechargeTypeRelationActivityInfo(@RequestParam("rechargeTypeId") Long rechargeTypeId,
                                                 @RequestParam("orgId") Long orgId){
        ActivityRechargeItem giveAmountByActivity = activityRechargeItemService.getGiveAmountByActivity(rechargeTypeId, orgId);
        if (giveAmountByActivity == null){
            return R.ok();
        }
        return R.ok(giveAmountByActivity.getActivityGiveAmount());
    }
}
