package com.ruoyi.web.modules.merchant.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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
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.RequestParam;
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.merchant.domain.BizMerchant;
import com.ruoyi.web.modules.merchant.dto.MerchantCertStatusDTO;
import com.ruoyi.web.modules.merchant.dto.MerchantCreateDTO;
import com.ruoyi.web.modules.merchant.exception.MerchantBatchDeleteException;
import com.ruoyi.web.modules.merchant.service.IMerchantService;

/**
 * 平台端商户管理
 */
@RestController
@RequestMapping("/agri/merchant")
public class MerchantController extends BaseController
{
    @Autowired
    private IMerchantService merchantService;

    @PreAuthorize("@ss.hasPermi('agri:merchant:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizMerchant query)
    {
        startPage();
        return getDataTable(merchantService.selectMerchantList(query));
    }

    @PreAuthorize("@ss.hasPermi('agri:merchant:query')")
    @GetMapping("/{merchantId}")
    public AjaxResult getInfo(@PathVariable("merchantId") Long merchantId)
    {
        return success(merchantService.selectMerchantDetail(merchantId));
    }

    @PreAuthorize("@ss.hasPermi('agri:merchant:add')")
    @Log(title = "商户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MerchantCreateDTO merchant)
    {
        Map<String, Object> data = merchantService.insertMerchant(merchant, getUsername());
        AjaxResult result = AjaxResult.success("请尽快在编辑中完善商户经营信息后再开设店铺");
        result.put(AjaxResult.DATA_TAG, data);
        return result;
    }

    @PreAuthorize("@ss.hasPermi('agri:merchant:edit')")
    @Log(title = "商户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizMerchant merchant)
    {
        Map<String, Object> data = merchantService.updateMerchant(merchant, getUsername());
        String msg = Boolean.TRUE.equals(data.get("bizCompleteChanged")) && "1".equals(data.get("bizComplete"))
                ? "保存成功，已可开设店铺" : "操作成功";
        AjaxResult result = AjaxResult.success(msg);
        result.put(AjaxResult.DATA_TAG, data);
        return result;
    }

    @PreAuthorize("@ss.hasPermi('agri:merchant:cert')")
    @Log(title = "商户认证状态", businessType = BusinessType.UPDATE)
    @PutMapping("/{merchantId}/certStatus")
    public AjaxResult updateCertStatus(@PathVariable("merchantId") Long merchantId,
            @RequestBody MerchantCertStatusDTO dto)
    {
        Map<String, Object> data = merchantService.updateCertStatus(merchantId, dto, getUsername());
        if (Boolean.TRUE.equals(data.get("needConfirm")))
        {
            AjaxResult result = AjaxResult.success();
            result.put(AjaxResult.DATA_TAG, data);
            return result;
        }
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:merchant:remove')")
    @GetMapping("/{merchantId}/deleteCheck")
    public AjaxResult deleteCheck(@PathVariable("merchantId") Long merchantId)
    {
        return success(merchantService.deleteCheck(merchantId));
    }

    @PreAuthorize("@ss.hasPermi('agri:merchant:remove')")
    @Log(title = "商户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{merchantIds}")
    public AjaxResult remove(@PathVariable("merchantIds") Long[] merchantIds)
    {
        try
        {
            merchantService.deleteMerchantByIds(merchantIds, getUsername());
            return success();
        }
        catch (MerchantBatchDeleteException e)
        {
            Map<String, Object> data = new HashMap<>();
            data.put("reasons", e.getReasons());
            AjaxResult result = AjaxResult.error(e.getMessage());
            result.put(AjaxResult.DATA_TAG, data);
            return result;
        }
    }

    @PreAuthorize("@ss.hasPermi('agri:merchant:list') or @ss.hasPermi('agri:shop:add')")
    @GetMapping("/selectList")
    public AjaxResult selectList()
    {
        return success(merchantService.selectSelectableList());
    }

    @PreAuthorize("@ss.hasPermi('agri:merchant:add')")
    @GetMapping("/adminUserOptions")
    public AjaxResult adminUserOptions(@RequestParam(value = "keyword", required = false) String keyword)
    {
        return success(merchantService.searchAdminUserOptions(keyword));
    }

    @PreAuthorize("@ss.hasPermi('agri:merchant:add')")
    @GetMapping("/memberOptions")
    public AjaxResult memberOptions(@RequestParam(value = "keyword", required = false) String keyword)
    {
        return success(merchantService.searchMemberOptions(keyword));
    }

    @PreAuthorize("@ss.hasPermi('agri:merchant:query') or @ss.hasPermi('agri:shop:add')")
    @GetMapping("/{merchantId}/openShopCheck")
    public AjaxResult openShopCheck(@PathVariable("merchantId") Long merchantId)
    {
        return success(merchantService.openShopCheck(merchantId));
    }

    @PreAuthorize("@ss.hasPermi('agri:merchant:query')")
    @GetMapping("/{merchantId}/shops")
    public AjaxResult shops(@PathVariable("merchantId") Long merchantId)
    {
        return success(merchantService.listShops(merchantId));
    }
}
