spec.model.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * 规格管理
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class specModel extends Model
  11. {
  12. public function __construct() {
  13. parent::__construct('spec');
  14. }
  15. /**
  16. * 规格列表
  17. * @param array $param 规格资料
  18. * @param object $page
  19. * @param string $field
  20. */
  21. public function specList($param, $page = '', $field = '*') {
  22. $condition_str = $this->getCondition($param);
  23. $array = array();
  24. $array['table'] = 'spec';
  25. $array['where'] = $condition_str;
  26. $array['field'] = $field;
  27. $array['order'] = $param['order'];
  28. $list_spec = Db::select($array, $page);
  29. return $list_spec;
  30. }
  31. /**
  32. * 单条规格信息
  33. *
  34. * @param int $id 规格id
  35. * @param string $field 字段
  36. * @return array 一维数组
  37. */
  38. public function getSpecInfo($id, $field = '*') {
  39. return $this->field($field)->find($id);
  40. }
  41. /**
  42. * 规格值列表
  43. *
  44. * @param array $where 添加
  45. * @param string $field 字段
  46. * @param string $order 排序
  47. */
  48. public function getSpecValueList($where, $field = '*', $order = 'sp_value_sort asc,sp_value_id asc') {
  49. $result = $this->table('spec_value')->field($field)->where($where)->order($order)->select();
  50. return empty($result) ? array() : $result;
  51. }
  52. /**
  53. * 更新规格值
  54. *
  55. * @param array $update 更新数据
  56. * @param array $where 条件
  57. * @return boolean
  58. */
  59. public function editSpecValue($update, $where) {
  60. $result = $this->table('spec_value')->where($where)->update($update);
  61. return $result;
  62. }
  63. /**
  64. * 添加数据
  65. *
  66. * @param array $insert 添加数据
  67. * @return boolean
  68. */
  69. public function addSpecValue($insert) {
  70. $result = $this->table('spec_value')->insert($insert);
  71. return $result;
  72. }
  73. /**
  74. * 添加数据 多条
  75. *
  76. * @param array $insert 添加数据
  77. * @return boolean
  78. */
  79. public function addSpecValueALL($insert) {
  80. $result = $this->table('spec_value')->insertAll($insert);
  81. return $result;
  82. }
  83. /**
  84. * 删除规格值
  85. *
  86. * @param array $where 条件
  87. * @return boolean
  88. */
  89. public function delSpecValue($where) {
  90. $result = $this->table('spec_value')->where($where)->delete();
  91. return $result;
  92. }
  93. /**
  94. * 更新规格信息
  95. * @param array $update 更新数据
  96. * @param array $param 条件
  97. * @param string $table 表名
  98. * @return bool
  99. */
  100. public function specUpdate($update, $param, $table){
  101. $condition_str = $this->getCondition($param);
  102. if (empty($update)){
  103. return false;
  104. }
  105. if (is_array($update)){
  106. $tmp = array();
  107. foreach ($update as $k => $v){
  108. $tmp[$k] = $v;
  109. }
  110. $result = Db::update($table,$tmp,$condition_str);
  111. return $result;
  112. }else {
  113. return false;
  114. }
  115. }
  116. /**
  117. * 添加规格信息
  118. * @param array $param 一维数组
  119. * @return bool
  120. */
  121. public function addSpec($param) {
  122. // 规格表插入数据
  123. $sp_id = $this->insert($param);
  124. if (!$sp_id) {
  125. return false;
  126. } else {
  127. return true;
  128. }
  129. }
  130. /**
  131. * 新增
  132. *
  133. * @param array $param 参数内容
  134. * @return bool 布尔类型的返回结果
  135. */
  136. public function specValueAdd($param){
  137. if (empty($param)){
  138. return false;
  139. }
  140. if (is_array($param)){
  141. $tmp = array();
  142. foreach ($param as $k => $v){
  143. $tmp[$k] = $v;
  144. }
  145. $result = Db::insert('spec_value',$tmp);
  146. return $result;
  147. }else {
  148. return false;
  149. }
  150. }
  151. /**
  152. * 规格值列表
  153. * @param array $param 商品资料
  154. * @param object $page
  155. * @param string $field
  156. * @return array
  157. */
  158. public function specValueList($param, $page = '', $field = '*') {
  159. $condition_str = $this->getCondition($param);
  160. $array = array();
  161. $array['table'] = 'spec_value';
  162. $array['where'] = $condition_str;
  163. $array['field'] = $field;
  164. $array['order'] = $param['order'];
  165. $list_spec = Db::select($array, $page);
  166. return $list_spec;
  167. }
  168. /**
  169. * 规格值
  170. * @param array $param 商品资料
  171. * @param array $field
  172. * @return 一维数组
  173. */
  174. public function specValueOne($param, $field = '*') {
  175. $condition_str = $this->getCondition($param);
  176. $array = array();
  177. $array['table'] = 'spec_value';
  178. $array['where'] = $condition_str;
  179. $array['field'] = $field;
  180. $list_spec = Db::select($array);
  181. return $list_spec['0'];
  182. }
  183. /**
  184. * 删除规格
  185. *
  186. * @param 表名 spec,spec_value
  187. * @param 一维数组
  188. * @return bool
  189. */
  190. public function delSpec($table,$param){
  191. $condition_str = $this->getCondition($param);
  192. return Db::delete($table, $condition_str);
  193. }
  194. /**
  195. * 将条件数组组合为SQL语句的条件部分
  196. *
  197. * @param array $condition_array
  198. * @return string
  199. */
  200. private function getCondition($condition_array) {
  201. $condition_str = '';
  202. if($condition_array['sp_id'] != ''){
  203. $condition_str .= ' and sp_id = "'.$condition_array['sp_id'].'"';
  204. }
  205. if($condition_array['in_sp_id'] != ''){
  206. $condition_str .= ' and sp_id in ('.$condition_array['in_sp_id'].')';
  207. }
  208. if($condition_array['sp_value_id'] != ''){
  209. $condition_str .= ' and sp_value_id = "'.$condition_array['sp_value_id'].'"';
  210. }
  211. if($condition_array['in_sp_value_id'] != ''){
  212. $condition_str .= ' and sp_value_id in ('.$condition_array['in_sp_value_id'].')';
  213. }
  214. return $condition_str;
  215. }
  216. }