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