Mallvouchertemplate.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 数据层模型
  10. */
  11. class Mallvouchertemplate extends BaseModel
  12. {
  13. const VOUCHER_STATE_UNUSED = 1;
  14. const VOUCHER_STATE_USED = 2;
  15. const VOUCHER_STATE_EXPIRE = 3;
  16. public $page_info;
  17. private $voucher_state_array = array(
  18. self::VOUCHER_STATE_UNUSED => '未使用', self::VOUCHER_STATE_USED => '已使用', self::VOUCHER_STATE_EXPIRE => '已过期',
  19. );
  20. private $templatestate_arr;
  21. /**
  22. * 构造函数
  23. * @access public
  24. * @author csdeshang
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * 返回当前可用的代金券列表,每种类型(模板)的代金券里取出一个代金券码(同一个模板所有码面额和到期时间都一样)
  32. * @access public
  33. * @author csdeshang
  34. * @param array $condition 条件
  35. * @param int $goods_total 商品总金额
  36. * @return string
  37. */
  38. public function getCurrentAvailableMallVoucherUser($condition = array(), $goods_total = 0)
  39. {
  40. $condition[] = array('mallvoucheruser_state', '=', 1);
  41. $condition[] = array('mallvoucheruser_enddate', '>', TIMESTAMP);
  42. $mallvoucheruser_list = Db::name('mallvoucheruser')->where($condition)->select()->toArray();
  43. if (!empty($mallvoucheruser_list)) {
  44. $mallvoucheruser_list = ds_change_arraykey($mallvoucheruser_list, 'mallvouchertemplate_id');
  45. }
  46. foreach ($mallvoucheruser_list as $key => $voucher) {
  47. if ($goods_total < $voucher['mallvoucheruser_limit']) {
  48. unset($mallvoucheruser_list[$key]);
  49. } else {
  50. $mallvoucheruser_list[$key]['desc'] = sprintf('%s 面额%s元 ', $voucher['mallvoucheruser_title'], $voucher['mallvoucheruser_price']);
  51. if ($voucher['mallvoucheruser_limit'] > 0) {
  52. $mallvoucheruser_list[$key]['desc'] .= sprintf(' 消费满%s可用', $voucher['mallvoucheruser_limit']);
  53. }
  54. }
  55. }
  56. return $mallvoucheruser_list;
  57. }
  58. /**
  59. * 获得平台代金券列表
  60. * @access public
  61. * @author csdeshang
  62. * @return type
  63. */
  64. public function getMallvouchertemplateList($condition, $pagesize = '', $order = 'mallvouchertemplate_price asc')
  65. {
  66. if ($pagesize) {
  67. $voucherprice_list = Db::name('mallvouchertemplate')->where($condition)->order($order)->paginate(['list_rows' => 10, 'query' => request()->param()], false);
  68. $this->page_info = $voucherprice_list;
  69. return $voucherprice_list->items();
  70. } else {
  71. return Db::name('mallvouchertemplate')->where($condition)->order($order)->select()->toArray();
  72. }
  73. }
  74. /**
  75. * 获取单个代金券
  76. * @param type $where
  77. * @return type
  78. */
  79. public function getOneMallvouchertemplate($condition = array())
  80. {
  81. $malloucherinfotemplate = Db::name('mallvouchertemplate')->where($condition)->find();
  82. if ($malloucherinfotemplate['mallvouchertemplate_enddate'] > TIMESTAMP) {
  83. $malloucherinfotemplate['editable'] = true;
  84. } else {
  85. $malloucherinfotemplate['editable'] = false;
  86. }
  87. return $malloucherinfotemplate;
  88. }
  89. /**
  90. * 增加代金券面额
  91. * @param type $data
  92. * @return type
  93. */
  94. public function addMallvouchertemplate($data)
  95. {
  96. return Db::name('mallvouchertemplate')->insert($data);
  97. }
  98. /**
  99. * 修改代金券类型
  100. * @param type $where
  101. * @param type $data
  102. * @return type
  103. */
  104. public function editMallvouchertemplate($condition, $data)
  105. {
  106. return Db::name('mallvouchertemplate')->where($condition)->update($data);
  107. }
  108. /**
  109. * 删除代金券
  110. * @param type $condition
  111. * @return type
  112. */
  113. public function delMallvouchertemplate($condition)
  114. {
  115. return Db::name('mallvouchertemplate')->where($condition)->delete();
  116. }
  117. /**
  118. * 查询可兑换代金券模板详细信息,包括店铺信息
  119. * @access public
  120. * @author csdeshang
  121. * @param type $vid
  122. * @param type $member_id 会员id
  123. * @return type
  124. */
  125. public function getCanChangeTemplateInfo($vid, $member_id, $no_point = false)
  126. {
  127. echo $no_point;
  128. if ($vid <= 0 || $member_id <= 0) {
  129. return array('state' => false, 'msg' => '参数错误');
  130. }
  131. //查询可用代金券模板
  132. $where = array();
  133. $where[] = array('mallvouchertemplate_id', '=', $vid);
  134. $where[] = array('mallvouchertemplate_startdate', '<', TIMESTAMP);
  135. $where[] = array('mallvouchertemplate_enddate', '>', TIMESTAMP);
  136. $template_info = $this->getOneMallvouchertemplate($where);
  137. if (empty($template_info) || $template_info['mallvouchertemplate_quantity'] <= $template_info['mallvouchertemplate_giveout']) { //代金券不存在或者已兑换完
  138. return array('state' => false, 'msg' => '代金券已兑换完');
  139. }
  140. $member_model = model('member');
  141. $member_info = $member_model->getMemberInfoByID($member_id);
  142. if (empty($member_info)) {
  143. return array('state' => false, 'msg' => '参数错误');
  144. }
  145. if (!$no_point) {
  146. //验证会员积分是否足够
  147. if ($template_info['mallvouchertemplate_points'] > 0) {
  148. if (intval($member_info['member_points']) < intval($template_info['mallvouchertemplate_points'])) {
  149. return array('state' => false, 'msg' => '您的积分不足,暂时不能兑换该代金券');
  150. }
  151. }
  152. }
  153. //查询代金券列表
  154. $where = array();
  155. $where[] = array('mallvoucheruser_ownerid', '=', $member_id);
  156. $mallvoucheruser_list = $this->getMallvoucheruserList($where);
  157. if (!empty($mallvoucheruser_list)) {
  158. if (!$no_point) {
  159. $mallvoucher_count = 0; //兑换的代金券数量
  160. $mallvoucherone_count = 0; //该张代金券兑换的数量
  161. foreach ($mallvoucheruser_list as $k => $v) {
  162. //如果代金券未用且未过期
  163. if ($v['mallvoucheruser_state'] == 1 && $v['mallvoucheruser_enddate'] > TIMESTAMP) {
  164. $mallvoucher_count += 1;
  165. }
  166. if ($v['mallvouchertemplate_id'] == $template_info['mallvouchertemplate_id']) {
  167. $mallvoucherone_count += 1;
  168. }
  169. }
  170. //买家最多只能拥有同一个店铺尚未消费抵用的店铺代金券最大数量的验证
  171. if ($mallvoucher_count >= intval(config('ds_config.voucher_buyertimes_limit'))) {
  172. $message = sprintf('您的可用代金券已有%s张,不可再兑换了', config('ds_config.voucher_buyertimes_limit'));
  173. return array('state' => false, 'msg' => $message);
  174. }
  175. //同一张代金券最多能兑换的次数
  176. if (!empty($template_info['mallvouchertemplate_eachlimit']) && $mallvoucherone_count >= $template_info['mallvouchertemplate_eachlimit']) {
  177. $message = sprintf('该代金券您已兑换%s次,不可再兑换了', $template_info['mallvouchertemplate_eachlimit']);
  178. return array('state' => false, 'msg' => $message);
  179. }
  180. }
  181. }
  182. return array('state' => true, 'info' => $template_info);
  183. }
  184. /**
  185. * 积分兑换代金券
  186. * @access public
  187. * @author csdeshang
  188. * @param type $template_info 信息模板
  189. * @param type $member_id 会员ID
  190. * @param type $member_name 会员名
  191. * @return type
  192. */
  193. public function exchangeMallvoucher($template_info, $member_id, $member_name = '', $no_point = false)
  194. {
  195. if (intval($member_id) <= 0 || empty($template_info)) {
  196. return array('state' => false, 'msg' => '参数错误');
  197. }
  198. //查询会员信息
  199. if (!$member_name) {
  200. $member_info = model('member')->getMemberInfoByID($member_id);
  201. if (empty($template_info)) {
  202. return array('state' => false, 'msg' => '参数错误');
  203. }
  204. $member_name = $member_info['member_name'];
  205. }
  206. //添加代金券信息
  207. $insert_arr = array();
  208. $insert_arr['mallvouchertemplate_id'] = $template_info['mallvouchertemplate_id'];
  209. $insert_arr['mallvoucheruser_code'] = $this->getMallvoucherCode($member_id);
  210. $insert_arr['mallvoucheruser_title'] = $template_info['mallvouchertemplate_title'];
  211. $insert_arr['mallvoucheruser_startdate'] = $template_info['mallvouchertemplate_startdate'];
  212. $insert_arr['mallvoucheruser_enddate'] = $template_info['mallvouchertemplate_enddate'];
  213. $insert_arr['mallvoucheruser_price'] = $template_info['mallvouchertemplate_price'];
  214. $insert_arr['mallvoucheruser_limit'] = $template_info['mallvouchertemplate_limit'];
  215. $insert_arr['mallvoucheruser_state'] = 1;
  216. $insert_arr['mallvoucheruser_activedate'] = TIMESTAMP;
  217. $insert_arr['mallvoucheruser_ownerid'] = $member_id;
  218. $insert_arr['mallvoucheruser_ownername'] = $member_name;
  219. $insert_arr['mallvouchertemplate_gcidarr'] = $template_info['mallvouchertemplate_gcidarr'];
  220. $insert_arr['mallvouchertemplate_gcid'] = $template_info['mallvouchertemplate_gcid'];
  221. $insert_arr['mallvouchertemplate_points'] = $template_info['mallvouchertemplate_points'];
  222. $result = Db::name('mallvoucheruser')->insertGetId($insert_arr);
  223. if (!$result) {
  224. return array('state' => false, 'msg' => '兑换失败');
  225. }
  226. if (!$no_point) {
  227. //扣除会员积分
  228. if ($template_info['mallvouchertemplate_points'] > 0) {
  229. $points_arr['pl_memberid'] = $member_id;
  230. $points_arr['pl_membername'] = $member_name;
  231. $points_arr['pl_points'] = -$template_info['mallvouchertemplate_points'];
  232. $points_arr['pl_desc'] = lang('home_voucher') . lang('points_pointorderdesc');
  233. $result = model('points')->savePointslog('app', $points_arr, true);
  234. if (!$result) {
  235. return array('state' => false, 'msg' => '兑换失败');
  236. }
  237. }
  238. }
  239. if ($result) {
  240. //代金券模板的兑换数增加
  241. $result = $this->editMallvouchertemplate(array('mallvouchertemplate_id' => $template_info['mallvouchertemplate_id']), array(
  242. 'mallvouchertemplate_giveout' => Db::raw('mallvouchertemplate_giveout+1')
  243. ));
  244. if (!$result) {
  245. return array('state' => false, 'msg' => '兑换失败');
  246. }
  247. return array('state' => true, 'msg' => '兑换成功');
  248. } else {
  249. return array('state' => false, 'msg' => '兑换失败');
  250. }
  251. }
  252. /**
  253. * 获取买家代金券列表
  254. * @access public
  255. * @author csdeshang
  256. * @param int $member_id 用户编号
  257. * @param int $mallvoucher_state 代金券状态
  258. * @param int $pagesize 分页数
  259. * @return array
  260. */
  261. public function getMemberMallvoucherList($member_id, $mallvoucher_state, $pagesize = null)
  262. {
  263. if (empty($member_id)) {
  264. return false;
  265. }
  266. //更新过期代金券状态
  267. $this->_checkVoucherExpire($member_id);
  268. $where = array();
  269. $where[] = array('mallvoucheruser_ownerid', '=', $member_id);
  270. $mallvoucher_state = intval($mallvoucher_state);
  271. if (intval($mallvoucher_state) > 0 && array_key_exists($mallvoucher_state, $this->voucher_state_array)) {
  272. $where[] = array('mallvoucheruser_state', '=', $mallvoucher_state);
  273. }
  274. if ($pagesize) {
  275. $list = Db::name('mallvoucheruser')->where($where)->order('mallvoucheruser_id desc')->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  276. $this->page_info = $list;
  277. $list = $list->items();
  278. } else {
  279. $list = Db::name('mallvoucheruser')->where($where)->order('mallvoucheruser_id desc')->select()->toArray();
  280. }
  281. if (!empty($list) && is_array($list)) {
  282. foreach ($list as $key => $val) {
  283. //代金券状态文字
  284. $list[$key]['mallvoucheruser_state_text'] = $this->voucher_state_array[$val['mallvoucheruser_state']];
  285. $list[$key]['mallvoucheruser_end_date_text'] = $val['mallvoucheruser_enddate'] ? @date('Y.m.d', $val['mallvoucheruser_enddate']) : '';
  286. }
  287. }
  288. return $list;
  289. }
  290. /**
  291. * 获得代金券列表
  292. * @access public
  293. * @author csdeshang
  294. * @param type $where 条件
  295. * @param type $field 字段
  296. * @return type
  297. */
  298. public function getMallvoucheruserList($where, $field = '*')
  299. {
  300. $mallvoucheruser_list = array();
  301. $mallvoucheruser_list = Db::name('mallvoucheruser')->field($field)->where($where)->select()->toArray();
  302. return $mallvoucheruser_list;
  303. }
  304. /**
  305. * 更新过期代金券状态
  306. * @access public
  307. * @author csdeshang
  308. * @param type $member_id 会员ID
  309. * @return type
  310. */
  311. private function _checkVoucherExpire($member_id)
  312. {
  313. $condition = array();
  314. $condition[] = array('mallvoucheruser_ownerid', '=', $member_id);
  315. $condition[] = array('mallvoucheruser_state', '=', self::VOUCHER_STATE_UNUSED);
  316. $condition[] = array('mallvoucheruser_enddate', '<', TIMESTAMP);
  317. Db::name('mallvoucheruser')->where($condition)->update(array('mallvoucheruser_state' => self::VOUCHER_STATE_EXPIRE));
  318. }
  319. /**
  320. * 返回代金券状态数组
  321. * @access public
  322. * @author csdeshang
  323. * @return array
  324. */
  325. public function getVoucherStateArray()
  326. {
  327. return $this->voucher_state_array;
  328. }
  329. /**
  330. * 更新平台代金券信息
  331. * @param type $data 数据
  332. * @param type $condition 条件
  333. * @param type $member_id 会员id
  334. * @return type
  335. */
  336. public function editMallVoucherUser($data, $condition, $member_id = 0)
  337. {
  338. $result = Db::name('mallvoucheruser')->where($condition)->update($data);
  339. if ($result && $member_id > 0) {
  340. wcache($member_id, array('mallvoucher_count' => null), 'm_mallvoucher');
  341. }
  342. return $result;
  343. }
  344. /**
  345. * 更新使用的代金券状态
  346. * @param $input_voucher_list
  347. * @throws Exception
  348. */
  349. public function editMallvoucherState($mallvoucher_list)
  350. {
  351. $update = $this->editMallVoucherUser(array('mallvoucheruser_state' => 2), array('mallvoucheruser_id' => $mallvoucher_list['mallvoucheruser_id']), $mallvoucher_list['mallvoucheruser_ownerid']);
  352. if ($update) {
  353. $this->editMallvouchertemplate(array('mallvouchertemplate_id' => $mallvoucher_list['mallvouchertemplate_id']), array('mallvouchertemplate_used' => Db::raw('mallvouchertemplate_used+1')));
  354. } else {
  355. return ds_callback(false, '更新平台代金券状态失败');
  356. }
  357. return ds_callback(true);
  358. }
  359. /**
  360. * 获取代金券编码
  361. * @access public
  362. * @author csdeshang
  363. * @staticvar int $num
  364. * @param type $member_id 会员id
  365. * @return type
  366. */
  367. public function getMallvoucherCode($member_id = 0)
  368. {
  369. static $num = 1;
  370. $sign_arr = array();
  371. $sign_arr[] = sprintf('%02d', mt_rand(10, 99));
  372. $sign_arr[] = sprintf('%03d', (float)microtime() * 1000);
  373. $sign_arr[] = sprintf('%010d', TIMESTAMP - 946656000);
  374. if ($member_id) {
  375. $sign_arr[] = sprintf('%03d', (int)$member_id % 1000);
  376. } else {
  377. //自增变量
  378. $tmpnum = 0;
  379. if ($num > 99) {
  380. $tmpnum = substr($num, -1, 2);
  381. } else {
  382. $tmpnum = $num;
  383. }
  384. $sign_arr[] = sprintf('%02d', $tmpnum);
  385. $sign_arr[] = mt_rand(1, 9);
  386. }
  387. $code = implode('', $sign_arr);
  388. $num += 1;
  389. return $code;
  390. }
  391. }