Mallvouchertemplate.php 17 KB

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