Files
spacestation/common/models/agent/Auto_product_model.php
T
2025-10-15 10:13:45 +08:00

170 lines
5.8 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Auto_product_model extends HD_Model
{
private $table_name = 'lc_auto_product';
const STATUS_DELETE = -1; //删除
const STATUS_NORMAL = 1; //正常
const STATUS_DISABLE = 0; //下架
const EXTEND_FIELDS = [
'guide_price' => '', //指导价
'discounts' => '', //优惠信息
'discounts2' => '', //优惠信息2
'discounts3' => '', //优惠信息3
'promotion_text' => [], //多条 json数据{"文案1","文案2"}
'posterBg' => '', //海报背景图
'banner' => '', //banner图
];
protected $_setting_values = [];
public function __construct()
{
parent::__construct($this->table_name, 'default');
$this->load->model('agent/auto_product_label_model', 'autoProductLabel');
$this->load->model('agent/Auto_product_city_model', 'autoProductCity');
}
/**
* 获取第一张图片
* @param $imgs
* @return string
*/
public function getImageFirst($imgs)
{
$imgArray = explode(',', $imgs);
return $imgArray[0] ? build_qiniu_image_url($imgArray[0]) : '';
}
public function saveOrUpdate($params)
{
$promotionText = [];
if ($params['promotion_text']) {
foreach ($params['promotion_text'] as $k => $v) {
$v && $promotionText[] = $v;
}
}
$params['promotion_text'] = $promotionText;
$imgs = getImageFromArray($params['imgs']);
$posterBg = getImageFromArray($params['posterBg']);
$params['posterBg'] = $posterBg ? implode(',', $posterBg) : '';
$banner = getImageFromArray($params['banner']);
$params['banner'] = $banner ? implode(',', $banner) : '';
$addData = [
'title' => $params['title'],
'timeStart' => $params['dateRange'][0],
'timeEnd' => $params['dateRange'][1],
'imgs' => $imgs ? implode(',', $imgs) : '',
'crowdProfiling' => json_encode($params['crowdProfiling'], JSON_UNESCAPED_UNICODE),
];
$addData['brandId'] = $params['selectedCar']['brandId'] ?: 0;
$addData['seriesId'] = $params['selectedCar']['seriesId'] ?: 0;
$addData['sort'] = $params['sort'] ?: 0;
if ($params['id']) {
$result = $this->update($addData, ['id' => $params['id']]);
} else {
$result = $this->add($addData);
$params['id'] = $result;
}
foreach ($params as $k => $v) {
if (array_key_exists($k, self::EXTEND_FIELDS)) {
$this->_setting_values[$k] = $v;
}
}
if (intval($params['id']) > 0) {
$this->saveExtendFields($params['id']);
}
//更新标签
if (!$params['carProductLabel']) {
$this->autoProductLabel->delete(['product_id' => $params['id']]);
} else {
$labelIdString = implode(',', $params['carProductLabel']);
$this->autoProductLabel->delete(['product_id' => $params['id'], "label_id not in ($labelIdString)" => null]);
$replace_batch = [];
foreach ($params['carProductLabel'] as $item) {
$replace_batch[] = [
'product_id' => $params['id'],
'label_id' => $item,
];
}
is_array($replace_batch) && $this->autoProductLabel->replace_batch($replace_batch);
}
//更新城市
$this->autoProductCity->updateProductCity($params['id'], $params['provinceCity']);
return $result;
}
/**
* 更新扩展信息
* @param $id
* @return void
*/
public function saveExtendFields($id)
{
if ($this->_setting_values) {
$this->load->model('agent/auto_product_setting_model', 'autoProductSetting');
foreach ($this->_setting_values as $k => $v) {
$this->autoProductSetting->setValue($id, $k, $v);
}
}
}
/**
* @param $where
* @return mixed
*/
public function selectProductCount($where = array())
{
$fileds = 'lc_auto_product.*';
$this->db->distinct()->select($fileds);
$this->db->from('lc_auto_product');
$this->db->join('lc_auto_product_city', 'lc_auto_product_city.productId = lc_auto_product.id', 'left');
if ($where) {
$this->db->where($where);
}
return $this->db->count_all_results();
}
/**
* @param $where
* @param $order
* @param $page
* @param $page_size
* @param $fileds
* @return mixed
*/
public function selectProduct($where = array(), $order = '', $page = 0, $page_size = 20, $fileds = '', $obj = '')
{
!$fileds && $fileds = 'lc_auto_product.*';
$this->db->distinct()->select($fileds);
$this->db->from('lc_auto_product');
$this->db->join('lc_auto_product_city', 'lc_auto_product_city.productId = lc_auto_product.id', 'left');
if ($where) {
$this->db->where($where);
}
if ($order) {
$this->db->order_by($order);
}
if ($page) {
$offset = ($page - 1) * $page_size;
$limit = $page_size;
} else {
$offset = null;
$limit = null;
}
$this->db->limit($limit, $offset);
$query = $this->db->get();
if ($obj && file_exists($class = COMMPATH . 'libraries/entity/' . ucfirst($obj) . '.php')) {
require_once $class;
if (class_exists($obj)) {
return $query->custom_result_object($obj);
}
}
return $query ? $query->result_array() : [];
}
}