<?php

/**
 * +----------------------------------------------------------------------
 * | Created By PhpStorm
 * +----------------------------------------------------------------------
 * | DATE: 2021/8/25
 * +----------------------------------------------------------------------
 * | TIME: 9:18
 * +----------------------------------------------------------------------
 * | Author: YangXing(925032253@qq.com)
 * +----------------------------------------------------------------------
 * | Class: barcode
 * +----------------------------------------------------------------------
 */

namespace model\pro;

/**
 * Class barcode
 * @package model\pro
 */
class barcode extends \model
{
    /**
     * 错误信息
     * @var string
     */
    public $error = '';

    /**
     * 表名
     * @var string
     */
    public $table = 'pro_barcode';

    /**
     * @param $data
     * @return false|int|string
     * @throws \AlibabaCloud\Client\Exception\ClientException
     */
    public function createBarCode($data)
    {
        $barCode = $this->db->get($this->table,'*',['pb_active' => 1]);

        if(empty($barCode))
        {
            $this->error = lg('当前没有激活的企业商品编码');
            return false;
        }

        if($barCode['pb_use_total'] > 999 )
        {
            $this->error = lg('企业商品编码使用次数超过了999次');
            return false;
        }

        //TODO 没有考虑没有配置的情况
//        if($barCode['pb_use_total'] >= \mvc::$cfg['barcode_sms_max_value'])
//        {
//            $sms = new \servers\aliyunSms('barcode');
//            $result = $sms->smsaliyun(\mvc::$cfg['barcode_sms_user_mobile'],$barCode['bc_use_total']);
//            if($result === false){
//                $this->error = $sms->getError();
//                return false;
//            }
//        }
        $num = $barCode['pb_com_code'].sprintf("%04d", $barCode['pb_use_total'] + 1);
        $num = $this->codeAlgorithm($num);

        $numidx = new numidx();
        $data['pn_barcode'] = $num;
        $result = $numidx->setBarcode($data);
        if($result === false){
            return false;
        }
        $this->db->update($this->table,['pb_use_total' => $barCode['pb_use_total']+1],['pb_id' => $barCode['pb_id']]);
        return $num;
    }

    /**
     * 生成条码算法
     * @return int
     */
    public function codeAlgorithm(string $num)
    {
        //$str= 694133840010 测试样例
        //MOD(3,2) = 3 / 2 的余数
        //c = SUMPRODUCT(MID(C9,ROW($1:$6)*2,1)*3)
        //b = SUMPRODUCT(MID(C9,ROW($1:$6)*2-1,1)*1)
        //a = c+b
        //补位公式=10-MOD($a,10)
        if(!is_numeric($num) || strlen($num) < 10)
        {
            $this->error = lg('要生成的条形码格式不正确');
            return false;
        }

        $group1 = 0;
        for($i=1;$i<=6;$i++){
            $pos = $i * 2 -1;
            $group1 = intval($num[$pos-1])*1 + $group1;
        }
        $group2 = 0;
        for($i=1;$i<=6;$i++){
            $pos = $i * 2;
            $group2 = intval($num[$pos-1])*3 + $group2;
        }
        $result = 10 - (($group1 + $group2) % 10);
        $result = $num.substr($result,-1,1);
        return $result;
    }

    public function edit($data)
    {
        if($data['pb_active'] == 1){
            $this->db->update($this->table,['pb_active' => 0]);
        }
        if($data['pb_id'] > 0){
            return $this->db->update($this->table,['pb_com_code' => $data['pb_com_code'],'pb_active' => $data['pb_active']],['pb_id' => $data['pb_id']]);
        }else{
            return $this->db->insert($this->table,['pb_com_code' => $data['pb_com_code'],'pb_active' => $data['pb_active']]);
        }
    }
}
