<?php

/**
 * +----------------------------------------------------------------------
 * | Created By PhpStorm
 * +----------------------------------------------------------------------
 * | DATE: 2021/8/12
 * +----------------------------------------------------------------------
 * | TIME: 10:59
 * +----------------------------------------------------------------------
 * | Author: YangXing(925032253@qq.com)
 * +----------------------------------------------------------------------
 * | Class: paramAction
 * +----------------------------------------------------------------------
 */

namespace admin\action\pro;
use admin\action\baseAction;
use model\file;
use model\pro\cate;
use model\pro\param;

/**
 * Class paramAction
 * @package admin\action\pro
 */
class paramAction extends baseAction
{

    /**
     * 取品名集中所有的参数名称，相同的放在一起
     */
    public function getParamNamesByCates($ids)
    {
        $param = new param();
        $data = $param->getParamNamesByCates($ids);
        $this->successJson($data);
    }

    /**
     * 获取产品参数
     * @param $pId
     */
    public function proParam($pId)
    {
        $db = new param();
        $list = $db->getProParamByPid($pId);
        $this->successJson($list);
    }

    public function imageList()
    {
        $param = new cate();
        $param = $param->getRowByKey(\mvc::$req['key']);
        $list = [];
        if($param['photo_param']){
            $file = new file();
            $list[] = $file->getRowByKey($param['photo_param']);
        }
        $this->successJson($list);
    }

    public function imageDelete()
    {
       $id = \mvc::$req['id'];
       $photoParam = \mvc::$req['photo_param'];
       $cate = new cate();
       $result = $cate->deleteImage($id,$photoParam);
       if($result === false){
           $this->msgJson('参数错误');
       }
       $this->successJson();
    }

    /**
     * @param $cId
     */
    public function paramList($cId)
    {
        $db = new param();
        $list = $db->getParamByCid($cId);
        $this->successJson($list);
    }

    /**
     * 产品详情页
     * 零件自定义参数保存
     */
    public function addParamValue()
    {
        $data = \mvc::$req['data'];
        $data = json_decode($data,true);
        $db = new param();
        $result = $db->setParam($data,\mvc::$req['p_id']);
        if($result === false){
            $this->msgJson('更新失败');
        }
        $this->successJson();
    }


    /**
     * 计算小数点的后位数
     * @param $num
     * @return int
     */
    private function _getFloatLength($num)
    {
        $count = 0;
        $temp = explode('.', $num);
        if (sizeof($temp) > 1) {
            $decimal = end($temp);
            $count = strlen($decimal);
        }
        return $count;
    }
}