Mallvouchertemplate.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 数据层模型
  13. */
  14. class Mallvouchertemplate extends BaseModel
  15. {
  16. const VOUCHER_STATE_UNUSED = 1;
  17. const VOUCHER_STATE_USED = 2;
  18. const VOUCHER_STATE_EXPIRE = 3;
  19. public $page_info;
  20. private $voucher_state_array = array(
  21. self::VOUCHER_STATE_UNUSED => '未使用', self::VOUCHER_STATE_USED => '已使用', self::VOUCHER_STATE_EXPIRE => '已过期',
  22. );
  23. private $templatestate_arr;
  24. /**
  25. * 构造函数
  26. * @access public
  27. * @author csdeshang
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * 返回当前可用的代金券列表,每种类型(模板)的代金券里取出一个代金券码(同一个模板所有码面额和到期时间都一样)
  35. * @access public
  36. * @author csdeshang
  37. * @param array $condition 条件
  38. * @param int $goods_total 商品总金额
  39. * @return string
  40. */
  41. public function getCurrentAvailableMallVoucherUser($condition = array(), $goods_total = 0)
  42. {
  43. $condition[] = array('mallvoucheruser_state', '=', 1);
  44. $condition[] = array('mallvoucheruser_enddate', '>', TIMESTAMP);
  45. $mallvoucheruser_list = Db::name('mallvoucheruser')->where($condition)->select()->toArray();
  46. if(!empty($mallvoucheruser_list)){
  47. $mallvoucheruser_list = ds_change_arraykey($mallvoucheruser_list,'mallvouchertemplate_id');
  48. }
  49. foreach ($mallvoucheruser_list as $key => $voucher) {
  50. if ($goods_total < $voucher['mallvoucheruser_limit']) {
  51. unset($mallvoucheruser_list[$key]);
  52. }
  53. else {
  54. $mallvoucheruser_list[$key]['desc'] = sprintf('%s 面额%s元 ',$voucher['mallvoucheruser_title'], $voucher['mallvoucheruser_price']);
  55. if ($voucher['mallvoucheruser_limit'] > 0) {
  56. $mallvoucheruser_list[$key]['desc'] .= sprintf(' 消费满%s可用', $voucher['mallvoucheruser_limit']);
  57. }
  58. }
  59. }
  60. return $mallvoucheruser_list;
  61. }
  62. /**
  63. * 获得平台代金券列表
  64. * @access public
  65. * @author csdeshang
  66. * @return type
  67. */
  68. public function getMallvouchertemplateList($condition,$pagesize='',$order='mallvouchertemplate_price asc')
  69. {
  70. if($pagesize){
  71. $voucherprice_list = Db::name('mallvouchertemplate')->where($condition)->order($order)->paginate(['list_rows'=>10,'query' => request()->param()],false);
  72. $this->page_info = $voucherprice_list;
  73. return $voucherprice_list->items();
  74. } else {
  75. return Db::name('mallvouchertemplate')->where($condition)->order($order)->select()->toArray();
  76. }
  77. }
  78. /**
  79. * 获取单个代金券
  80. * @param type $where
  81. * @return type
  82. */
  83. public function getOneMallvouchertemplate($condition = array()){
  84. $malloucherinfotemplate = Db::name('mallvouchertemplate')->where($condition)->find();
  85. if ( $malloucherinfotemplate['mallvouchertemplate_enddate'] > TIMESTAMP) {
  86. $malloucherinfotemplate['editable'] = true;
  87. } else {
  88. $malloucherinfotemplate['editable'] = false;
  89. }
  90. return $malloucherinfotemplate;
  91. }
  92. /**
  93. * 增加代金券面额
  94. * @param type $data
  95. * @return type
  96. */
  97. public function addMallvouchertemplate($data){
  98. return Db::name('mallvouchertemplate')->insert($data);
  99. }
  100. /**
  101. * 修改代金券类型
  102. * @param type $where
  103. * @param type $data
  104. * @return type
  105. */
  106. public function editMallvouchertemplate($condition,$data){
  107. return Db::name('mallvouchertemplate')->where($condition)->update($data);
  108. }
  109. /**
  110. * 删除代金券
  111. * @param type $condition
  112. * @return type
  113. */
  114. public function delMallvouchertemplate($condition){
  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. }
  249. else {
  250. return array('state' => false, 'msg' => '兑换失败');
  251. }
  252. }
  253. /**
  254. * 获取买家代金券列表
  255. * @access public
  256. * @author csdeshang
  257. * @param int $member_id 用户编号
  258. * @param int $mallvoucher_state 代金券状态
  259. * @param int $pagesize 分页数
  260. * @return array
  261. */
  262. public function getMemberMallvoucherList($member_id, $mallvoucher_state, $pagesize = null)
  263. {
  264. if (empty($member_id)) {
  265. return false;
  266. }
  267. //更新过期代金券状态
  268. $this->_checkVoucherExpire($member_id);
  269. $where = array();
  270. $where[] = array('mallvoucheruser_ownerid','=',$member_id);
  271. $mallvoucher_state = intval($mallvoucher_state);
  272. if (intval($mallvoucher_state) > 0 && array_key_exists($mallvoucher_state, $this->voucher_state_array)) {
  273. $where[] = array('mallvoucheruser_state','=',$mallvoucher_state);
  274. }
  275. if($pagesize){
  276. $list = Db::name('mallvoucheruser')->where($where)->order('mallvoucheruser_id desc')->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  277. $this->page_info=$list;
  278. $list=$list->items();
  279. }else{
  280. $list=Db::name('mallvoucheruser')->where($where)->order('mallvoucheruser_id desc')->select()->toArray();
  281. }
  282. if (!empty($list) && is_array($list)) {
  283. foreach ($list as $key => $val) {
  284. //代金券状态文字
  285. $list[$key]['mallvoucheruser_state_text'] = $this->voucher_state_array[$val['mallvoucheruser_state']];
  286. $list[$key]['mallvoucheruser_end_date_text'] = $val['mallvoucheruser_enddate'] ? @date('Y.m.d', $val['mallvoucheruser_enddate']) : '';
  287. }
  288. }
  289. return $list;
  290. }
  291. /**
  292. * 获得代金券列表
  293. * @access public
  294. * @author csdeshang
  295. * @param type $where 条件
  296. * @param type $field 字段
  297. * @return type
  298. */
  299. public function getMallvoucheruserList($where, $field = '*')
  300. {
  301. $mallvoucheruser_list = array();
  302. $mallvoucheruser_list = Db::name('mallvoucheruser')->field($field)->where($where)->select()->toArray();
  303. return $mallvoucheruser_list;
  304. }
  305. /**
  306. * 更新过期代金券状态
  307. * @access public
  308. * @author csdeshang
  309. * @param type $member_id 会员ID
  310. * @return type
  311. */
  312. private function _checkVoucherExpire($member_id)
  313. {
  314. $condition = array();
  315. $condition[] = array('mallvoucheruser_ownerid','=',$member_id);
  316. $condition[] = array('mallvoucheruser_state','=',self::VOUCHER_STATE_UNUSED);
  317. $condition[] = array('mallvoucheruser_enddate','<', TIMESTAMP);
  318. Db::name('mallvoucheruser')->where($condition)->update(array('mallvoucheruser_state' => self::VOUCHER_STATE_EXPIRE));
  319. }
  320. /**
  321. * 返回代金券状态数组
  322. * @access public
  323. * @author csdeshang
  324. * @return array
  325. */
  326. public function getVoucherStateArray()
  327. {
  328. return $this->voucher_state_array;
  329. }
  330. /**
  331. * 更新平台代金券信息
  332. * @param type $data 数据
  333. * @param type $condition 条件
  334. * @param type $member_id 会员id
  335. * @return type
  336. */
  337. public function editMallVoucherUser($data, $condition, $member_id = 0)
  338. {
  339. $result = Db::name('mallvoucheruser')->where($condition)->update($data);
  340. if ($result && $member_id > 0) {
  341. wcache($member_id, array('mallvoucher_count' => null), 'm_mallvoucher');
  342. }
  343. return $result;
  344. }
  345. /**
  346. * 更新使用的代金券状态
  347. * @param $input_voucher_list
  348. * @throws Exception
  349. */
  350. public function editMallvoucherState($mallvoucher_list)
  351. {
  352. $update = $this->editMallVoucherUser(array('mallvoucheruser_state' => 2), array('mallvoucheruser_id' => $mallvoucher_list['mallvoucheruser_id']), $mallvoucher_list['mallvoucheruser_ownerid']);
  353. if ($update) {
  354. $this->editMallvouchertemplate(array('mallvouchertemplate_id' => $mallvoucher_list['mallvouchertemplate_id']),array('mallvouchertemplate_used'=>Db::raw('mallvouchertemplate_used+1')));
  355. }
  356. else {
  357. return ds_callback(false, '更新平台代金券状态失败');
  358. }
  359. return ds_callback(true);
  360. }
  361. /**
  362. * 获取代金券编码
  363. * @access public
  364. * @author csdeshang
  365. * @staticvar int $num
  366. * @param type $member_id 会员id
  367. * @return type
  368. */
  369. public function getMallvoucherCode($member_id = 0)
  370. {
  371. static $num = 1;
  372. $sign_arr = array();
  373. $sign_arr[] = sprintf('%02d', mt_rand(10, 99));
  374. $sign_arr[] = sprintf('%03d', (float)microtime() * 1000);
  375. $sign_arr[] = sprintf('%010d', TIMESTAMP - 946656000);
  376. if ($member_id) {
  377. $sign_arr[] = sprintf('%03d', (int)$member_id % 1000);
  378. }
  379. else {
  380. //自增变量
  381. $tmpnum = 0;
  382. if ($num > 99) {
  383. $tmpnum = substr($num, -1, 2);
  384. }
  385. else {
  386. $tmpnum = $num;
  387. }
  388. $sign_arr[] = sprintf('%02d', $tmpnum);
  389. $sign_arr[] = mt_rand(1, 9);
  390. }
  391. $code = implode('', $sign_arr);
  392. $num += 1;
  393. return $code;
  394. }
  395. }