Wholesale.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * 批发活动模型
  4. *
  5. */
  6. namespace app\common\model;
  7. use think\facade\Db;
  8. /**
  9. * ============================================================================
  10. * DSMall多用户商城
  11. * ============================================================================
  12. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  13. * 网站地址: http://www.csdeshang.com
  14. * ----------------------------------------------------------------------------
  15. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  16. * 不允许对程序代码以任何形式任何目的的再发布。
  17. * ============================================================================
  18. * 数据层模型
  19. */
  20. class Wholesale extends BaseModel {
  21. public $page_info;
  22. const WHOLESALE_STATE_NORMAL = 1;
  23. const WHOLESALE_STATE_CLOSE = 2;
  24. const WHOLESALE_STATE_CANCEL = 3;
  25. private $wholesale_state_array = array(
  26. 0 => '全部',
  27. self::WHOLESALE_STATE_NORMAL => '正常',
  28. self::WHOLESALE_STATE_CLOSE => '已结束',
  29. self::WHOLESALE_STATE_CANCEL => '管理员关闭'
  30. );
  31. /**
  32. * 读取批发列表
  33. * @access public
  34. * @author csdeshang
  35. * @param type $condition 条件
  36. * @param type $pagesize 分页
  37. * @param type $order 排序
  38. * @param type $field 字段
  39. * @return type
  40. */
  41. public function getWholesaleList($condition, $pagesize = null, $order = '', $field = '*') {
  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. $wholesale_info = Db::name('wholesale')->where($condition)->find();
  65. $wholesale_info = $this->getWholesaleExtendInfo($wholesale_info);
  66. return $wholesale_info;
  67. }
  68. /**
  69. * 根据批发编号读取限制折扣信息
  70. * @access public
  71. * @author csdeshang
  72. * @param type $wholesale_id 限制折扣活动编号
  73. * @param type $store_id 如果提供店铺编号,判断是否为该店铺活动,如果不是返回null
  74. * @return array
  75. */
  76. public function getWholesaleInfoByID($wholesale_id, $store_id = 0) {
  77. if (intval($wholesale_id) <= 0) {
  78. return null;
  79. }
  80. $condition = array();
  81. $condition[] = array('wholesale_id','=',$wholesale_id);
  82. $wholesale_info = $this->getWholesaleInfo($condition);
  83. if ($store_id > 0 && $wholesale_info['store_id'] != $store_id) {
  84. return null;
  85. } else {
  86. return $wholesale_info;
  87. }
  88. }
  89. /**
  90. * 批发状态数组
  91. * @access public
  92. * @author csdeshang
  93. * @return type
  94. */
  95. public function getWholesaleStateArray() {
  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. $data['wholesale_state'] = self::WHOLESALE_STATE_NORMAL;
  107. return Db::name('wholesale')->insertGetId($data);
  108. }
  109. /**
  110. * 更新
  111. * @access public
  112. * @author csdeshang
  113. * @param type $update 数据
  114. * @param type $condition 条件
  115. * @return type
  116. */
  117. public function editWholesale($update, $condition) {
  118. return Db::name('wholesale')->where($condition)->update($update);
  119. }
  120. /**
  121. * 删除批发活动,同时删除批发商品
  122. * @access public
  123. * @author csdeshang
  124. * @param type $condition 条件
  125. * @return bool
  126. */
  127. public function delWholesale($condition) {
  128. $wholesale_list = $this->getWholesaleList($condition);
  129. $wholesale_id_string = '';
  130. if (!empty($wholesale_list)) {
  131. foreach ($wholesale_list as $value) {
  132. $wholesale_id_string .= $value['wholesale_id'] . ',';
  133. }
  134. }
  135. //删除批发商品
  136. if ($wholesale_id_string !== '') {
  137. $wholesalegoods_model = model('wholesalegoods');
  138. $wholesalegoods_model->delWholesalegoods(array(array('wholesale_id','in', $wholesale_id_string)));
  139. }
  140. return Db::name('wholesale')->where($condition)->delete();
  141. }
  142. /**
  143. * 取消批发活动,同时取消批发商品
  144. * @access public
  145. * @author csdeshang
  146. * @param type $condition 条件
  147. * @return type
  148. */
  149. public function cancelWholesale($condition) {
  150. $wholesale_list = $this->getWholesaleList($condition);
  151. $wholesale_id_string = '';
  152. if (!empty($wholesale_list)) {
  153. foreach ($wholesale_list as $value) {
  154. $wholesale_id_string .= $value['wholesale_id'] . ',';
  155. }
  156. }
  157. $update = array();
  158. $update['wholesale_state'] = self::WHOLESALE_STATE_CANCEL;
  159. //删除批发商品
  160. if ($wholesale_id_string !== '') {
  161. $wholesalegoods_model = model('wholesalegoods');
  162. $condition = array();
  163. $condition[] = array('wholesalegoods_state','=',self::WHOLESALE_STATE_CANCEL);
  164. $condition[] = array('wholesale_id','in',$wholesale_id_string);
  165. $wholesalegoods_model->editWholesalegoods($condition);
  166. }
  167. return $this->editWholesale($update, $condition);
  168. }
  169. /**
  170. * 获取批发扩展信息,包括状态文字和是否可编辑状态
  171. * @access public
  172. * @author csdeshang
  173. * @param type $wholesale_info 批发信息
  174. * @return boolean
  175. */
  176. public function getWholesaleExtendInfo($wholesale_info) {
  177. if(!$wholesale_info){
  178. return false;
  179. }
  180. if ($wholesale_info['wholesale_end_time'] > TIMESTAMP) {
  181. $wholesale_info['wholesale_state_text'] = $this->wholesale_state_array[$wholesale_info['wholesale_state']];
  182. } else {
  183. $wholesale_info['wholesale_state_text'] = $this->wholesale_state_array[self::WHOLESALE_STATE_CLOSE];
  184. }
  185. if ($wholesale_info['wholesale_state'] == self::WHOLESALE_STATE_NORMAL && $wholesale_info['wholesale_end_time'] > TIMESTAMP) {
  186. $wholesale_info['editable'] = true;
  187. } else {
  188. $wholesale_info['editable'] = false;
  189. }
  190. return $wholesale_info;
  191. }
  192. /**
  193. * 编辑过期修改状态
  194. * @access public
  195. * @author csdeshang
  196. * @param type $condition
  197. * @return boolean
  198. */
  199. public function editExpireWholesale($condition) {
  200. $condition[] = array('wholesale_end_time','<', TIMESTAMP);
  201. // 更新商品促销价格
  202. $wholesalegoods_list = model('wholesalegoods')->getWholesalegoodsList(array(array('wholesale_end_time','<', TIMESTAMP)));
  203. $condition[] = array('wholesale_state','=',self::WHOLESALE_STATE_NORMAL);
  204. $updata = array();
  205. $update['wholesale_state'] = self::WHOLESALE_STATE_CLOSE;
  206. $result = $this->editWholesale($update, $condition);
  207. if ($result) {
  208. foreach ($wholesalegoods_list as $value) {
  209. $this->_unlockGoods($value['goods_commonid']);
  210. }
  211. }
  212. return true;
  213. }
  214. /**
  215. * 解锁商品
  216. * @access private
  217. * @author csdeshang
  218. * @param type $goods_commonid 商品编号ID
  219. */
  220. private function _unlockGoods($goods_commonid)
  221. {
  222. $goods_model = model('goods');
  223. $goods_model->editGoodsCommonUnlock(array('goods_commonid' => $goods_commonid));
  224. $goods_model->editGoodsUnlock(array('goods_commonid' => $goods_commonid));
  225. }
  226. }