Wholesale.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * 批发活动模型
  4. *
  5. */
  6. namespace app\common\model;
  7. use think\facade\Db;
  8. /**
  9. * ============================================================================
  10. *
  11. * ============================================================================
  12. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  13. * 网站地址: https://www.valimart.net/
  14. * ----------------------------------------------------------------------------
  15. *
  16. * ============================================================================
  17. * 数据层模型
  18. */
  19. class Wholesale extends BaseModel {
  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. if($pagesize){
  42. $res = Db::name('wholesale')->field($field)->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  43. $this->page_info=$res;
  44. $wholesale_list= $res->items();
  45. }else{
  46. $wholesale_list= Db::name('wholesale')->field($field)->where($condition)->order($order)->select()->toArray();
  47. }
  48. if (!empty($wholesale_list)) {
  49. for ($i = 0, $j = count($wholesale_list); $i < $j; $i++) {
  50. $wholesale_list[$i] = $this->getWholesaleExtendInfo($wholesale_list[$i]);
  51. }
  52. }
  53. return $wholesale_list;
  54. }
  55. /**
  56. * 根据条件读取限制折扣信息
  57. * @access public
  58. * @author csdeshang
  59. * @param type $condition 条件
  60. * @return type
  61. */
  62. public function getWholesaleInfo($condition) {
  63. $wholesale_info = Db::name('wholesale')->where($condition)->find();
  64. $wholesale_info = $this->getWholesaleExtendInfo($wholesale_info);
  65. return $wholesale_info;
  66. }
  67. /**
  68. * 根据批发编号读取限制折扣信息
  69. * @access public
  70. * @author csdeshang
  71. * @param type $wholesale_id 限制折扣活动编号
  72. * @param type $store_id 如果提供店铺编号,判断是否为该店铺活动,如果不是返回null
  73. * @return array
  74. */
  75. public function getWholesaleInfoByID($wholesale_id, $store_id = 0) {
  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. return $this->wholesale_state_array;
  96. }
  97. /**
  98. * 增加
  99. * @access public
  100. * @author csdeshang
  101. * @param array $data 数据
  102. * @return bool
  103. */
  104. public function addWholesale($data) {
  105. $data['wholesale_state'] = self::WHOLESALE_STATE_NORMAL;
  106. return Db::name('wholesale')->insertGetId($data);
  107. }
  108. /**
  109. * 更新
  110. * @access public
  111. * @author csdeshang
  112. * @param type $update 数据
  113. * @param type $condition 条件
  114. * @return type
  115. */
  116. public function editWholesale($update, $condition) {
  117. return Db::name('wholesale')->where($condition)->update($update);
  118. }
  119. /**
  120. * 删除批发活动,同时删除批发商品
  121. * @access public
  122. * @author csdeshang
  123. * @param type $condition 条件
  124. * @return bool
  125. */
  126. public function delWholesale($condition) {
  127. $wholesale_list = $this->getWholesaleList($condition);
  128. $wholesale_id_string = '';
  129. if (!empty($wholesale_list)) {
  130. foreach ($wholesale_list as $value) {
  131. $wholesale_id_string .= $value['wholesale_id'] . ',';
  132. }
  133. }
  134. //删除批发商品
  135. if ($wholesale_id_string !== '') {
  136. $wholesalegoods_model = model('wholesalegoods');
  137. $wholesalegoods_model->delWholesalegoods(array(array('wholesale_id','in', $wholesale_id_string)));
  138. }
  139. return Db::name('wholesale')->where($condition)->delete();
  140. }
  141. /**
  142. * 取消批发活动,同时取消批发商品
  143. * @access public
  144. * @author csdeshang
  145. * @param type $condition 条件
  146. * @return type
  147. */
  148. public function cancelWholesale($condition) {
  149. $wholesale_list = $this->getWholesaleList($condition);
  150. $wholesale_id_string = '';
  151. if (!empty($wholesale_list)) {
  152. foreach ($wholesale_list as $value) {
  153. $wholesale_id_string .= $value['wholesale_id'] . ',';
  154. }
  155. }
  156. $update = array();
  157. $update['wholesale_state'] = self::WHOLESALE_STATE_CANCEL;
  158. //删除批发商品
  159. if ($wholesale_id_string !== '') {
  160. $wholesalegoods_model = model('wholesalegoods');
  161. $condition = array();
  162. $condition[] = array('wholesalegoods_state','=',self::WHOLESALE_STATE_CANCEL);
  163. $condition[] = array('wholesale_id','in',$wholesale_id_string);
  164. $wholesalegoods_model->editWholesalegoods($condition);
  165. }
  166. return $this->editWholesale($update, $condition);
  167. }
  168. /**
  169. * 获取批发扩展信息,包括状态文字和是否可编辑状态
  170. * @access public
  171. * @author csdeshang
  172. * @param type $wholesale_info 批发信息
  173. * @return boolean
  174. */
  175. public function getWholesaleExtendInfo($wholesale_info) {
  176. if(!$wholesale_info){
  177. return false;
  178. }
  179. if ($wholesale_info['wholesale_end_time'] > TIMESTAMP) {
  180. $wholesale_info['wholesale_state_text'] = $this->wholesale_state_array[$wholesale_info['wholesale_state']];
  181. } else {
  182. $wholesale_info['wholesale_state_text'] = $this->wholesale_state_array[self::WHOLESALE_STATE_CLOSE];
  183. }
  184. if ($wholesale_info['wholesale_state'] == self::WHOLESALE_STATE_NORMAL && $wholesale_info['wholesale_end_time'] > TIMESTAMP) {
  185. $wholesale_info['editable'] = true;
  186. } else {
  187. $wholesale_info['editable'] = false;
  188. }
  189. return $wholesale_info;
  190. }
  191. /**
  192. * 编辑过期修改状态
  193. * @access public
  194. * @author csdeshang
  195. * @param type $condition
  196. * @return boolean
  197. */
  198. public function editExpireWholesale($condition) {
  199. $condition[] = array('wholesale_end_time','<', TIMESTAMP);
  200. // 更新商品促销价格
  201. $wholesalegoods_list = model('wholesalegoods')->getWholesalegoodsList(array(array('wholesale_end_time','<', TIMESTAMP)));
  202. $condition[] = array('wholesale_state','=',self::WHOLESALE_STATE_NORMAL);
  203. $updata = array();
  204. $update['wholesale_state'] = self::WHOLESALE_STATE_CLOSE;
  205. $result = $this->editWholesale($update, $condition);
  206. if ($result) {
  207. foreach ($wholesalegoods_list as $value) {
  208. $this->_unlockGoods($value['goods_commonid']);
  209. }
  210. }
  211. return true;
  212. }
  213. /**
  214. * 解锁商品
  215. * @access private
  216. * @author csdeshang
  217. * @param type $goods_commonid 商品编号ID
  218. */
  219. private function _unlockGoods($goods_commonid)
  220. {
  221. $goods_model = model('goods');
  222. $goods_model->editGoodsCommonUnlock(array('goods_commonid' => $goods_commonid));
  223. $goods_model->editGoodsUnlock(array('goods_commonid' => $goods_commonid));
  224. }
  225. }