package com.younike.merchant;

import com.alibaba.fastjson.JSONObject;
import com.younike.common.annotation.Log;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.enums.BusinessType;
import com.younike.common.network.HttpClientUtils;
import com.younike.system.domain.vo.*;
import com.younike.system.service.ICategoryService;
import com.younike.system.service.IMerchantCategoryService;
import com.younike.system.service.IMerchantService;
import com.younike.system.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 店铺信息Controller
 * 
 * @author elev
 * @date 2022-01-04
 */
@RestController
@RequestMapping("/system/merchant")
public class MerchantController extends BaseController
{
    @Autowired
    private IMerchantService merchantService;
    @Autowired
    private IMerchantCategoryService merchantCategoryService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IOrderService orderService;

    @Log(title = "入驻申请", businessType = BusinessType.INSERT)
    @PermitAll
    @PostMapping("settled")
    public AjaxResult settled(@RequestBody MerchantSettledVO merchantSettledVO)
    {
        return AjaxResult.success(merchantService.settled(merchantSettledVO));
    }

    @GetMapping("/merchantInfo")
    @PermitAll
    public AjaxResult merchantInfo(@RequestParam("merchantId") Long merchantId) {
        merchantService.checkMerchant(merchantId);
        return AjaxResult.success(merchantService.merchantInfo(merchantId));
    }

    @Log(title = "入驻信息修改", businessType = BusinessType.UPDATE)
    @PermitAll
    @PostMapping("settledModify")
    public AjaxResult settledModify(@RequestBody MerchantSettledVO merchantSettledVO)
    {
        merchantService.checkMerchant(merchantSettledVO.getMerchantId());
        return AjaxResult.success(merchantService.settledModify(merchantSettledVO));
    }

    @Log(title = "商家基本信息修改", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasRole('merchant')")
    @PostMapping("merchantModify")
    public AjaxResult merchantModify(@RequestBody @Validated MerchantModifyVO merchantModifyVO)
    {
        merchantService.checkMerchant(merchantModifyVO.getMerchantId());
        return AjaxResult.success(merchantService.merchantModify(merchantModifyVO));
    }

    @Log(title = "商家服务类型修改", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasRole('merchant')")
    @PostMapping("merchantCategoryModify")
    public AjaxResult merchantCategoryModify(@RequestBody MerchantCategoryModifyVO merchantCategoryModifyVO)
    {
        merchantService.checkMerchant(merchantCategoryModifyVO.getMerchantId());
        return AjaxResult.success(merchantCategoryService.merchantCategoryModify(merchantCategoryModifyVO));
    }

    @GetMapping("categoryList")
    @PreAuthorize("@ss.hasAnyRoles({'merchant', 'operation'})")
    public AjaxResult categoryList(){
        Map<String, List<CategoryVo>> result = new HashMap<>();
        result.put("serviceList", categoryService.enabledCategoryList(1));
        result.put("repairList", categoryService.enabledCategoryList(2));
        return AjaxResult.success(result);
    }

    /**
     * 查询家政分类列表
     *
     * @return
     */
    @GetMapping("/homeCategoryList")
    @PreAuthorize("@ss.hasAnyRoles({'merchant', 'operation'})")
    public AjaxResult homeCategoryList(){
        Map<String, List<CategoryVo>> result = new HashMap<>();
        result.put("serviceList", categoryService.enabledCategoryList(1));
        return AjaxResult.success(result);
    }

    @GetMapping("merchantCategoryList")
    @PreAuthorize("@ss.hasRole('merchant')")
    public AjaxResult merchantCategoryList(@RequestParam("merchantId") Long merchantId)
    {
        merchantService.checkMerchant(merchantId);
        return AjaxResult.success(merchantCategoryService.merchantCategoryList(merchantId));
    }

    @GetMapping("dashboard")
    @PreAuthorize("@ss.hasRole('merchant')")
    public AjaxResult dashboard(@RequestParam("merchantId") Long merchantId)
    {
        merchantService.checkMerchant(merchantId);
        return AjaxResult.success(orderService.dashboard(merchantId));
    }

    @Log(title = "商户退款", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasRole('merchant')")
    @PostMapping("merchantRefund")
    public AjaxResult merchantRefund(@RequestBody @Validated MerchantRefundVO merchantRefund)
    {
//        return AjaxResult.success(merchantService.merchantRefund(merchantRefund));
        return AjaxResult.success();
    }

    @GetMapping("querybankaps")
    @PermitAll
    public AjaxResult querybankaps(@RequestParam String card, @RequestParam String key, @RequestParam String page){

        String host = "https://cnaps.market.alicloudapi.com";
        String path = "/lianzhuo/querybankaps";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + "54bd705496704563b15b1f0077cf4425");
        JSONObject data = new JSONObject();
        try {
            String s = HttpClientUtils.executeGet(host + path + "?card="+card+"&"+"key="+key+"&page="+page, headers);
            data = JSONObject.parseObject(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success(data);
    }

}
