package com.liquidnet.client.admin.web.controller.zhengzai.merchant;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.liquidnet.client.admin.common.annotation.Log;
import com.liquidnet.client.admin.common.core.controller.BaseController;
import com.liquidnet.client.admin.common.core.domain.AjaxResult;
import com.liquidnet.client.admin.common.core.page.TableDataInfo;
import com.liquidnet.client.admin.common.enums.BusinessType;
import com.liquidnet.client.admin.common.utils.ShiroUtils;
import com.liquidnet.client.admin.zhengzai.merchant.service.IMerchantSponsorAppliesAdminService;
import com.liquidnet.service.merchant.dto.param.admin.MerchantSponsorApplyListParam;
import com.liquidnet.service.merchant.dto.vo.MerchantSponsorAppliesVo;
import com.liquidnet.service.merchant.entity.MerchantSponsorApplies;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@Api(tags = "主办申请管理")
@Controller
@RequestMapping("merchant/sponsor/apply")
public class MerchantSponsorAppliesController extends BaseController {

    private final String prefix = "zhengzai/kylin/sponsor";

    @Autowired
    IMerchantSponsorAppliesAdminService merchantSponsorAppliesAdminService;

    @RequiresPermissions("merchant:sponsor:apply:view")
    @GetMapping()
    public String applyList() {
        return prefix + "/applyList";
    }

    @Log(title = "主办申请管理", businessType = BusinessType.LIST)
    @RequiresPermissions("merchant:sponsor:apply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MerchantSponsorApplyListParam param) {
        startPage();
        return getDataTable(merchantSponsorAppliesAdminService.list(
                Wrappers.lambdaQuery(MerchantSponsorApplies.class)
                        .eq(param.getSponsorApplyId().length() > 0, MerchantSponsorApplies::getSponsorApplyId, param.getSponsorApplyId())
                        .eq(param.getApplyStatus() >= 0, MerchantSponsorApplies::getApplyStatus, param.getApplyStatus())
                        .select(
                                MerchantSponsorApplies::getSponsorApplyId, MerchantSponsorApplies::getApplyStatus, MerchantSponsorApplies::getApplyType, MerchantSponsorApplies::getReject, MerchantSponsorApplies::getUid,
                                MerchantSponsorApplies::getName, MerchantSponsorApplies::getLogo,
                                MerchantSponsorApplies::getCreatedAt
                        )
        ));
    }

    @Log(title = "主办申请管理", businessType = BusinessType.DETAIL)
    @RequiresPermissions("merchant:sponsor:apply:detail")
    @GetMapping(value = "/details/{sponsorApplyId}")
    public String detail(@PathVariable("sponsorApplyId") String sponsorApplyId, ModelMap mmap) {

        LambdaQueryWrapper<MerchantSponsorApplies> sponsorAppliesLambdaQueryWrapper = Wrappers.lambdaQuery(MerchantSponsorApplies.class);
        sponsorAppliesLambdaQueryWrapper.eq(MerchantSponsorApplies::getSponsorApplyId, sponsorApplyId);
        MerchantSponsorApplies sponsorApplies = merchantSponsorAppliesAdminService.getOne(sponsorAppliesLambdaQueryWrapper);

        MerchantSponsorAppliesVo sponsorAppliesVo = MerchantSponsorAppliesVo.getNew();
        sponsorAppliesVo.copy(sponsorApplies);

        mmap.put("sponsorApplyInfo", sponsorAppliesVo);

        return prefix + "/applyDetail";
    }

    @ApiOperation(value = "审核通过")
    @Log(title = "主办申请审核通过", businessType = BusinessType.INSERT)
    @RequiresPermissions("merchant:sponsor:apply:approve")
    @PostMapping("/approve")
    @ResponseBody
    public AjaxResult approve(@RequestParam(required = true) String sponsorApplyId) {
        String authUid = ShiroUtils.getUserId().toString();
        String authName = ShiroUtils.getLoginName();

        return toAjax(merchantSponsorAppliesAdminService.applyApprove(sponsorApplyId, authUid, authName));
    }

    @ApiOperation(value = "审核驳回")
    @Log(title = "主办申请审核驳回", businessType = BusinessType.INSERT)
    @RequiresPermissions("merchant:sponsor:apply:reject")
    @PostMapping("/reject")
    @ResponseBody
    public AjaxResult reject(@RequestParam(required = true) String sponsorApplyId,
                             @RequestParam(required = true) String reject) {
        String authUid = ShiroUtils.getUserId().toString();
        String authName = ShiroUtils.getLoginName();

        return toAjax(merchantSponsorAppliesAdminService.applyReject(sponsorApplyId, authUid, authName, reject));
    }
}
