package com.ruoyi.web.modules.store.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.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.store.domain.BizShop;
import com.ruoyi.web.modules.store.dto.MerchantAccountUpdateDTO;
import com.ruoyi.web.modules.store.dto.ShopCreateDTO;
import com.ruoyi.web.modules.store.dto.ShopUpdateDTO;
import com.ruoyi.web.modules.store.exception.ShopBatchDeleteException;
import com.ruoyi.web.modules.store.service.IShopService;

/**
 * 平台端店铺管理
 */
@RestController
@RequestMapping("/agri/shop")
public class ShopController extends BaseController
{
    @Autowired
    private IShopService shopService;

    @PreAuthorize("@ss.hasPermi('agri:shop:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizShop query)
    {
        startPage();
        return getDataTable(shopService.selectShopList(query));
    }

    @PreAuthorize("@ss.hasPermi('agri:shop:query')")
    @GetMapping("/{shopId}")
    public AjaxResult getInfo(@PathVariable("shopId") Long shopId)
    {
        return success(shopService.selectShopDetail(shopId));
    }

    @PreAuthorize("@ss.hasPermi('agri:shop:add')")
    @Log(title = "店铺管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopCreateDTO dto)
    {
        Long shopId = shopService.createShop(dto, getUsername());
        Map<String, Object> data = new HashMap<>();
        data.put("shopId", shopId);
        AjaxResult result = AjaxResult.success();
        result.put(AjaxResult.DATA_TAG, data);
        return result;
    }

    @PreAuthorize("@ss.hasPermi('agri:shop:edit')")
    @Log(title = "店铺管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopUpdateDTO dto)
    {
        Map<String, Object> data = shopService.updateShop(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:shop:remove')")
    @GetMapping("/{shopId}/deleteCheck")
    public AjaxResult deleteCheck(@PathVariable("shopId") Long shopId)
    {
        return success(shopService.deleteCheck(shopId));
    }

    @PreAuthorize("@ss.hasPermi('agri:shop:remove')")
    @Log(title = "店铺管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{shopIds}")
    public AjaxResult remove(@PathVariable("shopIds") Long[] shopIds)
    {
        try
        {
            shopService.deleteShopByIds(shopIds, getUsername());
            return success();
        }
        catch (ShopBatchDeleteException 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:shop:account') or @ss.hasPermi('agri:shop:add')")
    @GetMapping("/merchant/{merchantId}/account")
    public AjaxResult getAccount(@PathVariable("merchantId") Long merchantId)
    {
        return success(shopService.getMerchantAccount(merchantId));
    }

    @PreAuthorize("@ss.hasPermi('agri:shop:account')")
    @Log(title = "店铺经营账号", businessType = BusinessType.UPDATE)
    @PutMapping("/merchant/{merchantId}/account")
    public AjaxResult updateAccount(@PathVariable("merchantId") Long merchantId,
            @RequestBody MerchantAccountUpdateDTO dto)
    {
        Map<String, Object> data = shopService.updateMerchantAccount(merchantId, dto, getUsername());
        if (Boolean.TRUE.equals(data.get("needConfirm")))
        {
            AjaxResult result = AjaxResult.success();
            result.put(AjaxResult.DATA_TAG, data);
            return result;
        }
        return success();
    }

}
