# -*- coding: utf-8 -*-
"""
@Author: HuangJianYi
@Date: 2021-01-07 10:30:16
:LastEditTime: 2021-02-23 17:28:23
:LastEditors: HuangJianYi
@Description: 
"""
from handlers.base.client_base import *
from handlers.base.seven import *

from models.db_models.act.act_info_model import *


class IndexHandler(BaseHandler):
    """
    :description: 默认页
    """
    def get_async(self):
        """
        :description: 默认页
        :param 
        :return 字符串
        :last_editors: HuangJingCan
        """
        self.write(config.get_value("run_port") + "_api")

    def head_async(self):
        """
        docstring: 默认页
        """
        self.write(config.get_value("run_port") + "_api")


class CoreBaseHandler(ClientBaseHandler):
    """
    :description: 业务基类
    """
    @client_filter_check_head()
    def get_async(self):
        """
        :param act_id：活动id
        :return: dict
        :last_editors: WangQiang
        """
        #活动信息
        act_info_model = ActInfoModel()
        act_info = act_info_model.get_entity()
        base_info = {}
        base_info["is_open"] = 1
        base_info["close_word"] = "对不起，小程序已关闭"
        base_info["coin_name"] = config.get_value("coin_name")
        if act_info:
            base_info["act_id"] = act_info.id
            base_info["is_open"] = act_info.is_open  #小程序是否开启
            base_info["close_word"] = act_info.close_word  #小程序关闭文案
            base_info["share_desc"] = act_info.share_desc  #分享内容
            base_info["back_pic"] = ""  #首页背景图
            if act_info.skin_desc:
                skin_desc = [j for j in self.json_loads(act_info.skin_desc) if j["is_select"] == 1]
                base_info["back_pic"] = skin_desc[0]["image"] if len(skin_desc) > 0 else ""
            base_info["menu_desc"] = act_info.menu_desc  #底部导航菜单
            base_info["freight_price"] = act_info.freight_price  #运费价格
            base_info["deliver_explain"] = act_info.deliver_explain  #发货说明
            base_info["is_num_free_shipping"] = act_info.is_num_free_shipping  #是否开启满件包邮
            base_info["free_shipping_count"] = act_info.free_shipping_count  #包邮件数
            base_info["is_amount_free_shipping"] = act_info.is_amount_free_shipping  #是否开启满金额包邮
            base_info["free_shipping_amount"] = act_info.free_shipping_amount  #包邮金额
            base_info["is_home_page_notice"] = act_info.is_home_page_notice  #是否开启首页中奖公告（0否1是）
        self.client_reponse_json_success(base_info)


def client_filter_check_act_open():
    """
    :description: 小程序活动是否开启校验 仅限handler使用,
    :param must_params: 必须传递的参数集合
    :last_editors: WangQiang
    """
    def check_params(handler):
        def wrapper(self, **args):
            act_id = int(self.get_param("act_id", 0))
            if act_id == 0:
                return self.client_reponse_json_error("ParamError", "参数错误,缺少活动id参数")
            act_info_model = ActInfoModel()
            act_info = act_info_model.get_entity(f"id={act_id}")
            if not act_info:
                return self.client_reponse_json_error("ParamError", "未找到活动信息")
            if not act_info.is_open:
                return self.client_reponse_json_error("ParamError", act_info.close_word if act_info.close_word else "对不起，小程序未开启")
            return handler(self, **args)

        return wrapper

    return check_params
