Mallvouchertemplate.php 17 KB

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