<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 商户管理
 */
namespace app\allwin\model;
use think\Model;

class MchId extends Model{
    
    protected $pk     = 'id';
    protected $table  = 'ai_allwin_mch';

    /**
     * 获取商户号
     * @return void
     */
    public static function getMch(int $id = 0,int $miniapp_id = 0){
        $where = [];
        $where['is_pass'] = 2;
        $where['status']  = 0;
        if($miniapp_id > 0){
            $where['member_miniapp_id'] = $miniapp_id;
            $where['is_default']        = 1;
        }elseif($id > 0){
            $where['id'] = $id;
        }else{
            $where['member_miniapp_id'] = 0;
            $where['is_types']          = 0;
        }
        return self::where($where)->field('mchid,store_name,merchant_shortname,id')->find();
    }

    /**
     * 添加与编辑
     * @return void
     */
    public static function edit($param){
        $data['mchid']              = trim($param['mchid']);
        $data['store_name']         = trim($param['store_name']);
        $data['merchant_shortname'] = trim($param['merchant_shortname']);
        $data['contact_phone']      = trim($param['contact_phone']);
        $data['store_street']       = trim($param['store_street']);
        $data['update_time']        = time();
        if(isset($param['id'])){
            return self::where(['id'=>(int)$param['id']])->update($data);
        }else{
            $data['member_miniapp_id'] = $param['member_miniapp_id'];
            $data['member_id']         = $param['member_id'];  //所属用户
            $data['business_code']     = $param['business_code'];
            $data['create_time']       = time();
            $data['is_types']          = 0;  //小微商户
            $data['is_pass']           = 2;  //未提交审核
            $data['is_default']        = 0;  //非默认
            $data['status']            = 0;  //正常
            $data['is_delete']         = 0;  //允许删除
            return self::insert($data);
        }
    }

    /**
     * 注册小微商户
     * @return void
     */
    public static function reg($param){
        $data['merchant_shortname'] = trim($param['merchant_shortname']);
        $data['store_name']         = trim($param['store_name']);
        $data['contact_phone']      = trim($param['contact_phone']);
        $data['store_street']       = trim($param['store_street']);
        $data['product_desc']       = trim($param['product_desc']);
        $data['indoor_pic']         = trim($param['indoor_pic']);
        $data['store_entrance_pic'] = trim($param['store_entrance_pic']);
        $data['id_card_name']       = trim($param['id_card_name']);
        $data['id_card_number']     = trim($param['id_card_number']);
        $data['id_card_copy']       = trim($param['id_card_copy']);
        $data['id_card_national']   = trim($param['id_card_national']);
        $data['account_bank']       = trim($param['account_bank']);
        $data['account_number']     = trim($param['account_number']);
        $data['bank_address_code']  = trim($param['bank_address_code']);
        $data['id_card_start_time'] = trim($param['id_card_start_time']);
        $data['id_card_end_time']   = trim($param['id_card_end_time']);
        $data['update_time']        = time();
        $data['is_pass']            = 0;  //充值审核状态
        $data['is_default']         = 0;  //非默认
        if(isset($param['id'])){
            $rel = self::where(['member_miniapp_id' =>$param['member_miniapp_id'],'id'=>(int)$param['id']])->field('id,store_entrance_pic,indoor_pic,id_card_copy,id_card_national')->find();
            if(empty($rel)) return;
            if(md5($data['store_entrance_pic']) != md5($rel->store_entrance_pic)){
                $data['store_entrance_pic_media_id'] = '';
            }
            if(md5($data['indoor_pic']) != md5($rel->indoor_pic)){
                $data['indoor_pic_media_id'] = '';
            }
            if(md5($data['id_card_copy']) != md5($rel->id_card_copy)){
                $data['id_card_copy_media_id'] = '';
            }
            if(md5($data['id_card_national']) != md5($rel->id_card_national)){
                $data['id_card_national_media_id'] = '';
            }
            return self::update($data,['id'=>$rel->id]);
        }else{
            $data['member_miniapp_id'] = $param['member_miniapp_id'];
            $data['create_time']       = time();
            $data['is_types']          = 1;  //小微商户
            $data['is_delete']         = 0;  //是否允许删除
            $data['status']            = 0;  //正常
            $data['member_id']         = $param['member_id'];  //所属用户
            $data['business_code']     = $param['business_code'];
            return self::insert($data);
        }
    }

    public static function isLock(int $id){
        $result = self::get($id);
        $data['status'] = $result['status'] ? 0 : 1;
        return self::where('id',$id)->update($data);
    }
}