package com.ruoyi.web.modules.entry.controller;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.web.modules.entry.domain.BizMerchantEntryApply;
import com.ruoyi.web.modules.entry.dto.EntryApplyApproveDTO;
import com.ruoyi.web.modules.entry.dto.EntryApplyRejectDTO;
import com.ruoyi.web.modules.entry.service.IMerchantEntryApplyService;

/**
 * 平台端商户入驻审核
 */
@RestController
@RequestMapping("/agri/merchantEntryApply")
public class MerchantEntryApplyController extends BaseController
{
    @Autowired
    private IMerchantEntryApplyService applyService;

    @PreAuthorize("@ss.hasPermi('agri:merchantEntryApply:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizMerchantEntryApply query)
    {
        startPage();
        return getDataTable(applyService.selectList(query));
    }

    @PreAuthorize("@ss.hasPermi('agri:merchantEntryApply:list')")
    @GetMapping("/pendingCount")
    public AjaxResult pendingCount()
    {
        Map<String, Object> data = new HashMap<>();
        data.put("pendingCount", applyService.countPlatformPending());
        return success(data);
    }

    @PreAuthorize("@ss.hasPermi('agri:merchantEntryApply:query')")
    @GetMapping("/{applyId}")
    public AjaxResult getInfo(@PathVariable("applyId") Long applyId)
    {
        return success(applyService.selectDetail(applyId));
    }

    @PreAuthorize("@ss.hasPermi('agri:merchantEntryApply:audit')")
    @Log(title = "商户入驻审核通过", businessType = BusinessType.UPDATE)
    @PutMapping("/approve/{applyId}")
    public AjaxResult approve(@PathVariable("applyId") Long applyId,
            @RequestBody(required = false) EntryApplyApproveDTO dto)
    {
        return success(applyService.approve(applyId, dto, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('agri:merchantEntryApply:audit')")
    @Log(title = "商户入驻完成公示", businessType = BusinessType.UPDATE)
    @PutMapping("/completePublicity/{applyId}")
    public AjaxResult completePublicity(@PathVariable("applyId") Long applyId)
    {
        return success(applyService.completePublicity(applyId, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('agri:merchantEntryApply:audit')")
    @Log(title = "商户入驻审核驳回", businessType = BusinessType.UPDATE)
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody EntryApplyRejectDTO dto)
    {
        applyService.reject(dto, getUsername());
        return success();
    }
}
