package com.jeequan.jeepay.pay.ctrl.webcashier;

import com.jeequan.jeepay.db.entity.ThirdAuthDataEntity;
import com.jeequan.jeepay.service.impl.ThirdAuthDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class CashierController {

    @Autowired
    private ThirdAuthDataService wxThirdAuthDataService;

    /**
     * 小程序的公用路径，这里可以根据服务商编号做特定区分
     *
     * @param agentNo  服务商编号
     * @param fileName 文件名称
     */
    @GetMapping("/pages/hub/{agentNo}/{fileName}")
    public String getWxAuthData(@PathVariable("agentNo") String agentNo, @PathVariable("fileName") String fileName) {
        ThirdAuthDataEntity one = wxThirdAuthDataService.lambdaQuery()
                .eq(ThirdAuthDataEntity::getAgentNo, agentNo)
                .eq(ThirdAuthDataEntity::getSubType, ThirdAuthDataEntity.SUB_TYPE_LITE)
                .eq(ThirdAuthDataEntity::getType, ThirdAuthDataEntity.TYPE_WX)
                .eq(ThirdAuthDataEntity::getAuthFileName, fileName)
                .last("limit 1")
                .one();

        if (one != null) {
            return one.getValue();
        }

        return "";
    }

    /**
     * 公众号根目录授权
     *
     * @param fileName 文件名称
     */
    @GetMapping("/{fileName}")
    public String getWxAuthData(@PathVariable("fileName") String fileName) {
        ThirdAuthDataEntity one = wxThirdAuthDataService.lambdaQuery()
                .eq(ThirdAuthDataEntity::getSubType, ThirdAuthDataEntity.SUB_TYPE_H5)
                .eq(ThirdAuthDataEntity::getType, ThirdAuthDataEntity.TYPE_WX)
                .eq(ThirdAuthDataEntity::getAuthFileName, fileName)
                .last("limit 1")
                .one();

        if (one != null) {
            return one.getValue();
        }

        return "";
    }
}
