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