<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载:https://gitee.com/likeshop_gitee
// | github下载:https://github.com/likeshop-github
// | 访问官网:https://www.likeshop.cn
// | 访问社区:https://home.likeshop.cn
// | 访问手册:http://doc.likeshop.cn
// | 微信公众号:likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
// |  likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------

namespace app\admin\logic;

use app\common\basics\Logic;
use app\common\model\Pay;
use app\common\server\UrlServer;

/**
 * 支付配置 - 逻辑
 * Class PayConfigLogic
 * @package app\admin\logic
 */
class PayConfigLogic extends Logic
{

    /**
     * Notes: 配置列表
     * @author 段誉(2021/5/7 18:15)
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function lists()
    {
        $count = Pay::count();
        $lists = Pay::withAttr('status', function($value, $data) {
            return $value == 1 ? '启用' : '关闭';
        })->order('sort')->select();
        return ['lists' => $lists, 'count' => $count];
    }


    /**
     * Notes: 详情
     * @param $pay_code
     * @author 段誉(2021/5/7 18:15)
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function info($pay_code)
    {
        return Pay::where(['code' => $pay_code])->find();
    }


    /**
     * Notes: 余额
     * @param $post
     * @author 段誉(2021/5/7 18:15)
     * @return Pay
     */
    public static function editBalance($post)
    {
        return Pay::where('code', 'balance')->update([
            'short_name' => $post['short_name'],
            'image'      => UrlServer::setFileUrl($post['image'])  ?? '',
            'status'     => $post['status'],
            'sort'       => $post['sort'] ?? 0,
        ]);
    }


    /**
     * Notes: 微信
     * @param $post
     * @author 段誉(2021/5/7 18:16)
     * @return Pay
     */
    public static function editWechat($post)
    {

        $data = [
            'short_name' => $post['short_name'],
            'image'      => UrlServer::setFileUrl($post['image']) ?? '',
            'status'     => $post['status'],
            'sort'       => $post['sort'] ?? 0,
            'config'     => [
                'pay_sign_key' => $post['pay_sign_key'],
                'mch_id' => $post['mch_id'],
                'apiclient_cert' => $post['apiclient_cert'],
                'apiclient_key' => $post['apiclient_key']
            ]
        ];
        return Pay::where('code', 'wechat')->update($data);
    }


    /**
     * Notes: 支付宝
     * @param $post
     * @author 段誉(2021/5/7 18:16)
     * @return Pay
     */
    public static function editAlipay($post)
    {
        $data = [
            'short_name' => $post['short_name'],
            'image'      => UrlServer::setFileUrl($post['image']) ?? '',
            'status'     => $post['status'],
            'sort'       => $post['sort'] ?? 0,
            'config'     => [
                'app_id' => $post['app_id'],
                'private_key' => $post['private_key'],//应用私钥
                'ali_public_key' => $post['ali_public_key']//支付宝公钥
            ]
        ];
        return Pay::where('code', 'alipay')->update($data);
    }

}