Wholesale.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * 批发活动模型
  4. *
  5. */
  6. namespace app\common\model;
  7. use think\facade\Db;
  8. /**
  9. * ============================================================================
  10. *
  11. * ============================================================================
  12. *
  13. * ----------------------------------------------------------------------------
  14. *
  15. * ============================================================================
  16. * 数据层模型
  17. */
  18. class Wholesale extends BaseModel
  19. {
  20. public $page_info;
  21. const WHOLESALE_STATE_NORMAL = 1;
  22. const WHOLESALE_STATE_CLOSE = 2;
  23. const WHOLESALE_STATE_CANCEL = 3;
  24. private $wholesale_state_array = array(
  25. 0 => '全部',
  26. self::WHOLESALE_STATE_NORMAL => '正常',
  27. self::WHOLESALE_STATE_CLOSE => '已结束',
  28. self::WHOLESALE_STATE_CANCEL => '管理员关闭'
  29. );
  30. /**
  31. * 读取批发列表
  32. * @access public
  33. * @author csdeshang
  34. * @param type $condition 条件
  35. * @param type $pagesize 分页
  36. * @param type $order 排序
  37. * @param type $field 字段
  38. * @return type
  39. */
  40. public function getWholesaleList($condition, $pagesize = null, $order = '', $field = '*')
  41. {
  42. if ($pagesize) {
  43. $res = Db::name('wholesale')->field($field)->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  44. $this->page_info = $res;
  45. $wholesale_list = $res->items();
  46. } else {
  47. $wholesale_list = Db::name('wholesale')->field($field)->where($condition)->order($order)->select()->toArray();
  48. }
  49. if (!empty($wholesale_list)) {
  50. for ($i = 0, $j = count($wholesale_list); $i < $j; $i++) {
  51. $wholesale_list[$i] = $this->getWholesaleExtendInfo($wholesale_list[$i]);
  52. }
  53. }
  54. return $wholesale_list;
  55. }
  56. /**
  57. * 根据条件读取限制折扣信息
  58. * @access public
  59. * @author csdeshang
  60. * @param type $condition 条件
  61. * @return type
  62. */
  63. public function getWholesaleInfo($condition)
  64. {
  65. $wholesale_info = Db::name('wholesale')->where($condition)->find();
  66. $wholesale_info = $this->getWholesaleExtendInfo($wholesale_info);
  67. return $wholesale_info;
  68. }
  69. /**
  70. * 根据批发编号读取限制折扣信息
  71. * @access public
  72. * @author csdeshang
  73. * @param type $wholesale_id 限制折扣活动编号
  74. * @param type $store_id 如果提供店铺编号,判断是否为该店铺活动,如果不是返回null
  75. * @return array
  76. */
  77. public function getWholesaleInfoByID($wholesale_id, $store_id = 0)
  78. {
  79. if (intval($wholesale_id) <= 0) {
  80. return null;
  81. }
  82. $condition = array();
  83. $condition[] = array('wholesale_id', '=', $wholesale_id);
  84. $wholesale_info = $this->getWholesaleInfo($condition);
  85. if ($store_id > 0 && $wholesale_info['store_id'] != $store_id) {
  86. return null;
  87. } else {
  88. return $wholesale_info;
  89. }
  90. }
  91. /**
  92. * 批发状态数组
  93. * @access public
  94. * @author csdeshang
  95. * @return type
  96. */
  97. public function getWholesaleStateArray()
  98. {
  99. return $this->wholesale_state_array;
  100. }
  101. /**
  102. * 增加
  103. * @access public
  104. * @author csdeshang
  105. * @param array $data 数据
  106. * @return bool
  107. */
  108. public function addWholesale($data)
  109. {
  110. $data['wholesale_state'] = self::WHOLESALE_STATE_NORMAL;
  111. return Db::name('wholesale')->insertGetId($data);
  112. }
  113. /**
  114. * 更新
  115. * @access public
  116. * @author csdeshang
  117. * @param type $update 数据
  118. * @param type $condition 条件
  119. * @return type
  120. */
  121. public function editWholesale($update, $condition)
  122. {
  123. return Db::name('wholesale')->where($condition)->update($update);
  124. }
  125. /**
  126. * 删除批发活动,同时删除批发商品
  127. * @access public
  128. * @author csdeshang
  129. * @param type $condition 条件
  130. * @return bool
  131. */
  132. public function delWholesale($condition)
  133. {
  134. $wholesale_list = $this->getWholesaleList($condition);
  135. $wholesale_id_string = '';
  136. if (!empty($wholesale_list)) {
  137. foreach ($wholesale_list as $value) {
  138. $wholesale_id_string .= $value['wholesale_id'] . ',';
  139. }
  140. }
  141. //删除批发商品
  142. if ($wholesale_id_string !== '') {
  143. $wholesalegoods_model = model('wholesalegoods');
  144. $wholesalegoods_model->delWholesalegoods(array(array('wholesale_id', 'in', $wholesale_id_string)));
  145. }
  146. return Db::name('wholesale')->where($condition)->delete();
  147. }
  148. /**
  149. * 取消批发活动,同时取消批发商品
  150. * @access public
  151. * @author csdeshang
  152. * @param type $condition 条件
  153. * @return type
  154. */
  155. public function cancelWholesale($condition)
  156. {
  157. $wholesale_list = $this->getWholesaleList($condition);
  158. $wholesale_id_string = '';
  159. if (!empty($wholesale_list)) {
  160. foreach ($wholesale_list as $value) {
  161. $wholesale_id_string .= $value['wholesale_id'] . ',';
  162. }
  163. }
  164. $update = array();
  165. $update['wholesale_state'] = self::WHOLESALE_STATE_CANCEL;
  166. //删除批发商品
  167. if ($wholesale_id_string !== '') {
  168. $wholesalegoods_model = model('wholesalegoods');
  169. $condition = array();
  170. $condition[] = array('wholesalegoods_state', '=', self::WHOLESALE_STATE_CANCEL);
  171. $condition[] = array('wholesale_id', 'in', $wholesale_id_string);
  172. $wholesalegoods_model->editWholesalegoods($condition);
  173. }
  174. return $this->editWholesale($update, $condition);
  175. }
  176. /**
  177. * 获取批发扩展信息,包括状态文字和是否可编辑状态
  178. * @access public
  179. * @author csdeshang
  180. * @param type $wholesale_info 批发信息
  181. * @return boolean
  182. */
  183. public function getWholesaleExtendInfo($wholesale_info)
  184. {
  185. if (!$wholesale_info) {
  186. return false;
  187. }
  188. if ($wholesale_info['wholesale_end_time'] > TIMESTAMP) {
  189. $wholesale_info['wholesale_state_text'] = $this->wholesale_state_array[$wholesale_info['wholesale_state']];
  190. } else {
  191. $wholesale_info['wholesale_state_text'] = $this->wholesale_state_array[self::WHOLESALE_STATE_CLOSE];
  192. }
  193. if ($wholesale_info['wholesale_state'] == self::WHOLESALE_STATE_NORMAL && $wholesale_info['wholesale_end_time'] > TIMESTAMP) {
  194. $wholesale_info['editable'] = true;
  195. } else {
  196. $wholesale_info['editable'] = false;
  197. }
  198. return $wholesale_info;
  199. }
  200. /**
  201. * 编辑过期修改状态
  202. * @access public
  203. * @author csdeshang
  204. * @param type $condition
  205. * @return boolean
  206. */
  207. public function editExpireWholesale($condition)
  208. {
  209. $condition[] = array('wholesale_end_time', '<', TIMESTAMP);
  210. // 更新商品促销价格
  211. $wholesalegoods_list = model('wholesalegoods')->getWholesalegoodsList(array(array('wholesale_end_time', '<', TIMESTAMP)));
  212. $condition[] = array('wholesale_state', '=', self::WHOLESALE_STATE_NORMAL);
  213. $updata = array();
  214. $update['wholesale_state'] = self::WHOLESALE_STATE_CLOSE;
  215. $result = $this->editWholesale($update, $condition);
  216. if ($result) {
  217. foreach ($wholesalegoods_list as $value) {
  218. $this->_unlockGoods($value['goods_commonid']);
  219. }
  220. }
  221. return true;
  222. }
  223. /**
  224. * 解锁商品
  225. * @access private
  226. * @author csdeshang
  227. * @param type $goods_commonid 商品编号ID
  228. */
  229. private function _unlockGoods($goods_commonid)
  230. {
  231. $goods_model = model('goods');
  232. $goods_model->editGoodsCommonUnlock(array('goods_commonid' => $goods_commonid));
  233. $goods_model->editGoodsUnlock(array('goods_commonid' => $goods_commonid));
  234. }
  235. }