Sellervoucher.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. use think\facade\Db;
  6. /**
  7. *
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * 卖家代金券控制器
  12. */
  13. class Sellervoucher extends MobileSeller
  14. {
  15. private $quotastate_arr;
  16. private $templatestate_arr;
  17. public function initialize()
  18. {
  19. parent::initialize(); // TODO: Change the autogenerated stub
  20. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellervoucher.lang.php');
  21. if (config('ds_config.voucher_allow') != 1) {
  22. ds_json_encode(10001, lang('voucher_unavailable'));
  23. }
  24. //套餐状态
  25. $this->quotastate_arr = array(
  26. 'activity' => array(1, lang('voucher_quotastate_activity')),
  27. 'cancel' => array(2, lang('voucher_quotastate_cancel')),
  28. 'expire' => array(3, lang('voucher_quotastate_expire'))
  29. );
  30. //代金券模板状态
  31. $this->templatestate_arr = array(
  32. 'usable' => array(1, lang('voucher_templatestate_usable')),
  33. 'disabled' => array(2, lang('voucher_templatestate_disabled'))
  34. );
  35. }
  36. /**
  37. * @api {POST} api/Sellervoucher/templatelist 代金券列表
  38. * @apiVersion 1.0.0
  39. * @apiGroup Sellervoucher
  40. *
  41. * @apiHeader {String} X-DS-KEY 卖家授权token
  42. *
  43. * @apiParam {String} txt_keyword 关键词
  44. * @apiParam {String} select_state 状态 1:有效 2:失效
  45. * @apiParam {String} txt_startdate 有效期开始时间 YYYY-MM-DD
  46. * @apiParam {String} txt_enddate 有效期结束时间 YYYY-MM-DD
  47. * @apiParam {String} page 页码
  48. * @apiParam {String} pagesize 每页显示数量
  49. *
  50. * @apiSuccess {String} code 返回码,10000为成功
  51. * @apiSuccess {String} message 返回消息
  52. * @apiSuccess {Object} result 返回数据
  53. * @apiSuccess {Object} result.current_quota 代金券活动信息 (返回字段参考voucherquota)
  54. * @apiSuccess {String} result.promotion_voucher_price 购买代金劵活动所需费用
  55. * @apiSuccess {String} result.voucher_storetimes_limit 每月最多可以发布的代金劵促销活动数量
  56. * @apiSuccess {Object} result.vouchertemplate_list 代金券模板列表 (返回字段参考vouchertemplate)
  57. * @apiSuccess {Int} result.page_total 总页数
  58. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  59. */
  60. public function templatelist()
  61. {
  62. //检查过期的代金券模板状态设为失效
  63. $this->check_voucher_template_expire();
  64. $voucher_model = model('voucher');
  65. $isPlatformStore = false;
  66. $current_quota = false;
  67. if (!$this->store_info['is_platform_store']) {
  68. //查询是否存在可用套餐
  69. $current_quota = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  70. }
  71. //查询列表
  72. $param = array();
  73. $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
  74. if (trim(input('param.txt_keyword'))) {
  75. $param[] = array('vouchertemplate_title', 'like', '%' . trim(input('param.txt_keyword')) . '%');
  76. }
  77. $select_state = intval(input('param.select_state'));
  78. if ($select_state) {
  79. $param[] = array('vouchertemplate_state', '=', $select_state);
  80. }
  81. if (input('param.txt_startdate')) {
  82. $param[] = array('vouchertemplate_enddate', '>=', strtotime(input('param.txt_startdate')));
  83. }
  84. if (input('param.txt_enddate')) {
  85. $param[] = array('vouchertemplate_startdate', '<=', strtotime(input('param.txt_enddate')));
  86. }
  87. $vouchertemplate_list_ = Db::name('vouchertemplate')->where($param)->order('vouchertemplate_id desc')->paginate(['list_rows' => 10, 'query' => request()->param()], false);
  88. $vouchertemplate_list = $vouchertemplate_list_->items();
  89. foreach ($vouchertemplate_list as $key => $val) {
  90. if (!$val['vouchertemplate_customimg']) {
  91. $vouchertemplate_list[$key]['vouchertemplate_customimg'] = ds_get_pic(ATTACH_COMMON, config('ds_config.default_goods_image'));
  92. } else {
  93. $vouchertemplate_list[$key]['vouchertemplate_customimg'] = ds_get_pic(ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'], $val['vouchertemplate_customimg']);
  94. }
  95. }
  96. $result = array_merge(array('vouchertemplate_list' => $vouchertemplate_list, 'current_quota' => $current_quota, 'promotion_voucher_price' => config('ds_config.promotion_voucher_price'), 'voucher_storetimes_limit' => config('ds_config.voucher_storetimes_limit')), mobile_page($vouchertemplate_list_));
  97. ds_json_encode(10000, '', $result);
  98. }
  99. /**
  100. * @api {POST} api/Sellervoucher/quotaadd 购买套餐
  101. * @apiVersion 1.0.0
  102. * @apiGroup Sellervoucher
  103. *
  104. * @apiHeader {String} X-DS-KEY 卖家授权token
  105. *
  106. * @apiParam {Int} quota_quantity 购买数量
  107. *
  108. * @apiSuccess {String} code 返回码,10000为成功
  109. * @apiSuccess {String} message 返回消息
  110. * @apiSuccess {Object} result 返回数据
  111. * @apiSuccess {Int} result.voucherquota_endtime 代金券活动结束时间
  112. */
  113. public function quotaadd()
  114. {
  115. if (intval(config('ds_config.promotion_voucher_price')) == 0) {
  116. ds_json_encode(10001, lang('param_error'));
  117. }
  118. $quota_quantity = intval(input('post.quota_quantity'));
  119. if ($quota_quantity <= 0 || $quota_quantity > 12) {
  120. ds_json_encode(10001, lang('voucher_apply_num_error'));
  121. }
  122. //获取当前价格
  123. $current_price = intval(config('ds_config.promotion_voucher_price'));
  124. $voucher_model = model('voucher');
  125. //获取该用户已有套餐
  126. $current_quota = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  127. $quota_add_time = 86400 * 30 * $quota_quantity;
  128. if (empty($current_quota)) {
  129. //生成套餐
  130. $param = array();
  131. $param['voucherquota_memberid'] = $this->seller_info['member_id'];
  132. $param['voucherquota_membername'] = $this->seller_info['seller_name'];
  133. $param['voucherquota_storeid'] = $this->store_info['store_id'];
  134. $param['voucherquota_storename'] = $this->store_info['store_name'];
  135. $param['voucherquota_starttime'] = TIMESTAMP;
  136. $param['voucherquota_endtime'] = TIMESTAMP + $quota_add_time;
  137. $param['voucherquota_state'] = 1;
  138. $reault = Db::name('voucherquota')->insert($param);
  139. } else {
  140. $param = array();
  141. $param['voucherquota_endtime'] = $current_quota['voucherquota_endtime'] + $quota_add_time;
  142. $reault = Db::name('voucherquota')->where(array('voucherquota_id' => $current_quota['voucherquota_id']))->update($param);
  143. }
  144. //记录店铺费用
  145. $this->recordStorecost($current_price * $quota_quantity, lang('buy_voucher_package'));
  146. $this->recordSellerlog(lang('buy') . $quota_quantity . lang('voucher_plan') . $current_price . lang('ds_yuan'));
  147. if ($reault) {
  148. ds_json_encode(10000, lang('voucher_apply_buy_succ'), array('voucherquota_endtime' => $param['voucherquota_endtime']));
  149. } else {
  150. ds_json_encode(10001, lang('ds_common_op_fail'));
  151. }
  152. }
  153. /**
  154. * @api {POST} api/Sellervoucher/templateadd 代金券模版添加
  155. * @apiVersion 1.0.0
  156. * @apiGroup Sellervoucher
  157. *
  158. * @apiHeader {String} X-DS-KEY 卖家授权token
  159. *
  160. * @apiParam {String} txt_template_title 代金券模版名称
  161. * @apiParam {String} txt_template_total 代金券总数
  162. * @apiParam {String} select_template_price 面额
  163. * @apiParam {String} txt_template_limit 订单限额
  164. * @apiParam {String} txt_template_describe 代金券模版描述
  165. * @apiParam {String} txt_template_enddate 有效期结束时间 YYYY-MM-DD
  166. * @apiParam {String} storeclass_id 所属店铺分类ID
  167. * @apiParam {String} eachlimit 每人限领张数
  168. *
  169. * @apiSuccess {String} code 返回码,10000为成功
  170. * @apiSuccess {String} message 返回消息
  171. * @apiSuccess {Object} result 返回数据
  172. */
  173. public function templateadd()
  174. {
  175. $voucher_model = model('voucher');
  176. $isPlatformStore = $this->store_info['is_platform_store'];
  177. $quotainfo = array();
  178. if (!$isPlatformStore) {
  179. //查询当前可以使用的套餐
  180. $quotainfo = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  181. if (empty($quotainfo)) {
  182. if (intval(config('ds_config.promotion_voucher_price')) == 0) {
  183. $quotainfo = array('voucherquota_id' => 0, 'voucherquota_starttime' => TIMESTAMP, 'voucherquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  184. } else {
  185. ds_json_encode(10001, lang('voucher_template_quotanull'));
  186. }
  187. }
  188. //查询该套餐下代金券模板列表
  189. $count = Db::name('vouchertemplate')->where(array('vouchertemplate_quotaid' => $quotainfo['voucherquota_id'], 'vouchertemplate_state' => $this->templatestate_arr['usable'][0]))->count();
  190. if ($count >= config('ds_config.voucher_storetimes_limit')) {
  191. $message = sprintf(lang('voucher_template_noresidual'), config('ds_config.voucher_storetimes_limit'));
  192. ds_json_encode(10001, $message);
  193. }
  194. }
  195. $common_data = $this->common_data();
  196. $pricelist = $common_data['pricelist'];
  197. //验证提交的内容面额不能大于限额
  198. $data = [
  199. 'txt_template_title' => input('post.txt_template_title'),
  200. 'txt_template_total' => input('post.txt_template_total'),
  201. 'select_template_price' => input('post.select_template_price'),
  202. 'txt_template_limit' => input('post.txt_template_limit'),
  203. 'txt_template_describe' => input('post.txt_template_describe'),
  204. ];
  205. $sellervoucher_validate = ds_validate('sellervoucher');
  206. if (!$sellervoucher_validate->scene('templateadd')->check($data)) {
  207. ds_json_encode(10001, $sellervoucher_validate->getError());
  208. }
  209. //金额验证
  210. $price = intval(input('post.select_template_price')) > 0 ? intval(input('post.select_template_price')) : 0;
  211. foreach ($pricelist as $k => $v) {
  212. if ($v['voucherprice'] == $price) {
  213. $chooseprice = $v; //取得当前选择的面额记录
  214. }
  215. }
  216. if (empty($chooseprice)) {
  217. ds_json_encode(10001, lang('voucher_template_pricelisterror'));
  218. }
  219. $limit = intval(input('post.txt_template_limit')) > 0 ? intval(input('post.txt_template_limit')) : 0;
  220. if ($price >= $limit) {
  221. ds_json_encode(10001, lang('voucher_template_price_error'));
  222. }
  223. $insert_arr = array();
  224. $insert_arr['vouchertemplate_title'] = trim(input('post.txt_template_title'));
  225. $insert_arr['vouchertemplate_desc'] = trim(input('post.txt_template_describe'));
  226. $insert_arr['vouchertemplate_startdate'] = TIMESTAMP; //默认代金券模板的有效期为当前时间
  227. if (input('post.txt_template_enddate')) {
  228. $enddate = strtotime(input('post.txt_template_enddate'));
  229. if (!$isPlatformStore && $enddate > $quotainfo['voucherquota_endtime']) {
  230. $enddate = $quotainfo['voucherquota_endtime'];
  231. }
  232. $insert_arr['vouchertemplate_enddate'] = $enddate;
  233. } else { //如果没有添加有效期则默认为套餐的结束时间
  234. if ($isPlatformStore)
  235. $insert_arr['vouchertemplate_enddate'] = TIMESTAMP + 2592000; // 自营店 默认30天到期
  236. else
  237. $insert_arr['vouchertemplate_enddate'] = $quotainfo['voucherquota_endtime'];
  238. }
  239. $insert_arr['vouchertemplate_price'] = $price;
  240. $insert_arr['vouchertemplate_limit'] = $limit;
  241. $insert_arr['vouchertemplate_store_id'] = $this->store_info['store_id'];
  242. $insert_arr['vouchertemplate_storename'] = $this->store_info['store_name'];
  243. $insert_arr['vouchertemplate_sc_id'] = intval(input('post.storeclass_id'));
  244. $insert_arr['vouchertemplate_creator_id'] = $this->seller_info['member_id'];
  245. $insert_arr['vouchertemplate_state'] = $this->templatestate_arr['usable'][0];
  246. $insert_arr['vouchertemplate_total'] = intval(input('post.txt_template_total')) > 0 ? intval(input('post.txt_template_total')) : 0;
  247. $insert_arr['vouchertemplate_giveout'] = 0;
  248. $insert_arr['vouchertemplate_used'] = 0;
  249. $insert_arr['vouchertemplate_gettype'] = 1;
  250. $insert_arr['vouchertemplate_adddate'] = TIMESTAMP;
  251. $insert_arr['vouchertemplate_quotaid'] = isset($quotainfo['voucherquota_id']) ? $quotainfo['voucherquota_id'] : 0;
  252. $insert_arr['vouchertemplate_points'] = $chooseprice['voucherprice_defaultpoints'];
  253. $insert_arr['vouchertemplate_eachlimit'] = intval(input('post.eachlimit')) > 0 ? intval(input('post.eachlimit')) : 0;
  254. $insert_arr['vouchertemplate_if_private'] = intval(input('post.vouchertemplate_if_private'));
  255. //自定义图片
  256. if (!empty($_FILES['customimg']['name'])) {
  257. $file_name = $this->store_info['store_id'] . '_' . date('YmdHis') . rand(10000, 99999) . '.png';
  258. $res = ds_upload_pic(ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'], 'customimg', $file_name);
  259. if ($res['code']) {
  260. $file_name = $res['data']['file_name'];
  261. $insert_arr['vouchertemplate_customimg'] = $file_name;
  262. } else {
  263. ds_json_encode(10001, $res['msg']);
  264. }
  265. }
  266. $rs = Db::name('vouchertemplate')->insert($insert_arr);
  267. if ($rs) {
  268. ds_json_encode(10000, lang('ds_common_op_succ'));
  269. } else {
  270. ds_json_encode(10001, lang('ds_common_op_fail'));
  271. }
  272. }
  273. public function common_data()
  274. {
  275. $voucher_model = model('voucher');
  276. //查询面额列表
  277. $pricelist = Db::name('voucherprice')->order('voucherprice asc')->select()->toArray();
  278. $quotainfo = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  279. $quotainfo['voucherquota_endtime'] = date('Y-m-d', $quotainfo['voucherquota_endtime']);
  280. if (empty($pricelist)) {
  281. ds_json_encode(10001, lang('voucher_template_pricelisterror'));
  282. }
  283. return array('pricelist' => $pricelist, 'quotainfo' => $quotainfo, 'voucher_buyertimes_limit' => config('ds_config.voucher_buyertimes_limit'));
  284. }
  285. /**
  286. * @api {POST} api/Sellervoucher/get_common_data 代金券新增/编辑公共数据
  287. * @apiVersion 1.0.0
  288. * @apiGroup Sellervoucher
  289. *
  290. * @apiHeader {String} X-DS-KEY 卖家授权token
  291. *
  292. * @apiSuccess {String} code 返回码,10000为成功
  293. * @apiSuccess {String} message 返回消息
  294. * @apiSuccess {Object} result 返回数据
  295. * @apiSuccess {Object[]} result.pricelist 面额列表
  296. * @apiSuccess {Object[]} result.pricelist.voucherprice 面额
  297. * @apiSuccess {Object[]} result.pricelist.voucherprice_defaultpoints 兑换积分
  298. * @apiSuccess {Object[]} result.pricelist.voucherprice_describe 描述
  299. * @apiSuccess {Object[]} result.pricelist.voucherprice_id 面额ID
  300. * @apiSuccess {String} result.voucher_buyertimes_limit 买家最多只能拥有同一个店铺尚未消费抵用的店铺代金券最大数量
  301. */
  302. public function get_common_data()
  303. {
  304. $common_data = $this->common_data();
  305. ds_json_encode(10000, '', $common_data);
  306. }
  307. /**
  308. * @api {POST} api/Sellervoucher/templateadd 代金券模版编辑
  309. * @apiVersion 1.0.0
  310. * @apiGroup Sellervoucher
  311. *
  312. * @apiHeader {String} X-DS-KEY 卖家授权token
  313. *
  314. * @apiParam {Int} tid 代金券模版ID
  315. * @apiParam {String} txt_template_title 代金券模版名称
  316. * @apiParam {String} txt_template_total 代金券总数
  317. * @apiParam {String} select_template_price 面额
  318. * @apiParam {String} txt_template_limit 订单限额
  319. * @apiParam {String} txt_template_describe 代金券模版描述
  320. * @apiParam {String} txt_template_enddate 有效期结束时间 YYYY-MM-DD
  321. * @apiParam {String} storeclass_id 所属店铺分类ID
  322. * @apiParam {String} eachlimit 每人限领张数
  323. * @apiParam {Int} tstate 代金券模版状态 1:有效 2:失效
  324. *
  325. * @apiSuccess {String} code 返回码,10000为成功
  326. * @apiSuccess {String} message 返回消息
  327. * @apiSuccess {Object} result 返回数据
  328. */
  329. public function templateedit()
  330. {
  331. $t_id = intval(input('param.tid'));
  332. if ($t_id <= 0) {
  333. ds_json_encode(10001, lang('param_error'));
  334. }
  335. //查询模板信息
  336. $param = array();
  337. $param[] = array('vouchertemplate_id', '=', $t_id);
  338. $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
  339. $param[] = array('vouchertemplate_state', '=', $this->templatestate_arr['usable'][0]);
  340. $param[] = array('vouchertemplate_giveout', '<=', '0');
  341. $param[] = array('vouchertemplate_enddate', '>', TIMESTAMP);
  342. $t_info = Db::name('vouchertemplate')->where($param)->find();
  343. if (empty($t_info)) {
  344. ds_json_encode(10001, lang('param_error'));
  345. }
  346. $isPlatformStore = $this->store_info['is_platform_store'];
  347. View::assign('isPlatformStore', $isPlatformStore);
  348. $quotainfo = array();
  349. if (!$isPlatformStore) {
  350. //查询套餐信息
  351. $quotainfo = Db::name('voucherquota')->where(array(
  352. 'voucherquota_id' => $t_info['vouchertemplate_quotaid'],
  353. 'voucherquota_storeid' => $this->store_info['store_id']
  354. ))->find();
  355. if (empty($quotainfo)) {
  356. ds_json_encode(10001, lang('voucher_template_quotanull'));
  357. }
  358. }
  359. $common_data = $this->common_data();
  360. $pricelist = $common_data['pricelist'];
  361. //验证提交的内容面额不能大于限额
  362. $data = [
  363. 'txt_template_title' => input('post.txt_template_title'),
  364. 'txt_template_total' => input('post.txt_template_total'),
  365. 'select_template_price' => input('post.select_template_price'),
  366. 'txt_template_limit' => input('post.txt_template_limit'),
  367. 'txt_template_describe' => input('post.txt_template_describe'),
  368. ];
  369. $sellervoucher_validate = ds_validate('sellervoucher');
  370. if (!$sellervoucher_validate->scene('templateedit')->check($data)) {
  371. ds_json_encode(10001, $sellervoucher_validate->getError());
  372. }
  373. //金额验证
  374. $price = intval(input('post.select_template_price')) > 0 ? intval(input('post.select_template_price')) : 0;
  375. foreach ($pricelist as $k => $v) {
  376. if ($v['voucherprice'] == $price) {
  377. $chooseprice = $v; //取得当前选择的面额记录
  378. }
  379. }
  380. if (empty($chooseprice)) {
  381. ds_json_encode(10001, lang('voucher_template_pricelisterror'));
  382. }
  383. $limit = intval(input('post.txt_template_limit')) > 0 ? intval(input('post.txt_template_limit')) : 0;
  384. if ($price >= $limit) {
  385. ds_json_encode(10001, lang('voucher_template_price_error'));
  386. }
  387. $update_arr = array();
  388. $update_arr['vouchertemplate_title'] = trim(input('post.txt_template_title'));
  389. $update_arr['vouchertemplate_desc'] = trim(input('post.txt_template_describe'));
  390. if (input('post.txt_template_enddate')) {
  391. $enddate = strtotime(input('post.txt_template_enddate'));
  392. if (!$isPlatformStore && $enddate > $quotainfo['voucherquota_endtime']) {
  393. $enddate = $quotainfo['voucherquota_endtime'];
  394. }
  395. $update_arr['vouchertemplate_enddate'] = $enddate;
  396. } else { //如果没有添加有效期则默认为套餐的结束时间
  397. if ($isPlatformStore)
  398. $update_arr['vouchertemplate_enddate'] = TIMESTAMP + 2592000; // 自营店 默认30天到期
  399. else
  400. $update_arr['vouchertemplate_enddate'] = $quotainfo['voucherquota_endtime'];
  401. }
  402. $update_arr['vouchertemplate_price'] = $price;
  403. $update_arr['vouchertemplate_limit'] = $limit;
  404. $update_arr['vouchertemplate_sc_id'] = intval(input('post.storeclass_id'));
  405. $update_arr['vouchertemplate_state'] = intval(input('post.tstate')) == $this->templatestate_arr['usable'][0] ? $this->templatestate_arr['usable'][0] : $this->templatestate_arr['disabled'][0];
  406. $update_arr['vouchertemplate_total'] = intval(input('post.txt_template_total')) > 0 ? intval(input('post.txt_template_total')) : 0;
  407. $update_arr['vouchertemplate_points'] = $chooseprice['voucherprice_defaultpoints'];
  408. $update_arr['vouchertemplate_eachlimit'] = intval(input('post.eachlimit')) > 0 ? intval(input('post.eachlimit')) : 0;
  409. $update_arr['vouchertemplate_if_private'] = intval(input('post.vouchertemplate_if_private'));
  410. //自定义图片
  411. if (!empty($_FILES['customimg']['name'])) {
  412. $file_name = $this->store_info['store_id'] . '_' . date('YmdHis') . rand(10000, 99999);
  413. $res = ds_upload_pic(ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'], 'customimg', $file_name);
  414. if ($res['code']) {
  415. $file_name = $res['data']['file_name'];
  416. //删除原图
  417. if (!empty($t_info['vouchertemplate_customimg'])) { //如果模板存在,则删除原模板图片
  418. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'] . DIRECTORY_SEPARATOR . $t_info['vouchertemplate_customimg']);
  419. }
  420. $update_arr['vouchertemplate_customimg'] = $file_name;
  421. } else {
  422. ds_json_encode(10001, $res['msg']);
  423. }
  424. }
  425. $rs = Db::name('vouchertemplate')->where(array('vouchertemplate_id' => $t_info['vouchertemplate_id']))->update($update_arr);
  426. if ($rs) {
  427. ds_json_encode(10000, lang('ds_common_op_succ'));
  428. } else {
  429. ds_json_encode(10001, lang('ds_common_op_fail'));
  430. }
  431. }
  432. /**
  433. * @api {POST} api/Sellervoucher/templatedel 删除代金券
  434. * @apiVersion 1.0.0
  435. * @apiGroup Sellervoucher
  436. *
  437. * @apiHeader {String} X-DS-KEY 卖家授权token
  438. *
  439. * @apiParam {Int} tid 代金券模版ID
  440. *
  441. * @apiSuccess {String} code 返回码,10000为成功
  442. * @apiSuccess {String} message 返回消息
  443. * @apiSuccess {Object} result 返回数据
  444. */
  445. public function templatedel()
  446. {
  447. $t_id = intval(input('param.tid'));
  448. if ($t_id <= 0) {
  449. ds_json_encode(10001, lang('param_error'));
  450. }
  451. //查询模板信息
  452. $param = array();
  453. $param[] = array('vouchertemplate_id', '=', $t_id);
  454. $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
  455. $param[] = array('vouchertemplate_giveout', '<=', '0'); //会员没领取过代金券才可删除
  456. $t_info = Db::name('vouchertemplate')->where($param)->find();
  457. if (empty($t_info)) {
  458. ds_json_encode(10001, lang('param_error'));
  459. }
  460. $rs = Db::name('vouchertemplate')->where(array('vouchertemplate_id' => $t_info['vouchertemplate_id']))->delete();
  461. if ($rs) {
  462. //删除自定义的图片
  463. if (trim($t_info['vouchertemplate_customimg'])) {
  464. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'] . DIRECTORY_SEPARATOR . $t_info['vouchertemplate_customimg']);
  465. }
  466. ds_json_encode(10000, lang('ds_common_del_succ'));
  467. } else {
  468. ds_json_encode(10001, lang('ds_common_del_fail'));
  469. }
  470. }
  471. /**
  472. * 查看代金券详细
  473. */
  474. public function templateinfo()
  475. {
  476. $t_id = intval(input('param.tid'));
  477. if ($t_id <= 0) {
  478. ds_json_encode(10001, lang('param_error'), 'Sellervoucher/templatelist');
  479. }
  480. //查询模板信息
  481. $param = array();
  482. $param['vouchertemplate_id'] = $t_id;
  483. $param['vouchertemplate_store_id'] = $this->store_info['store_id'];
  484. $t_info = Db::name('vouchertemplate')->where($param)->find();
  485. ds_json_encode(10000, lang('ds_common_del_succ'), array('t_info' => $t_info));
  486. }
  487. /*
  488. * 把代金券模版设为失效
  489. */
  490. private function check_voucher_template_expire($voucher_template_id = '')
  491. {
  492. $where_array = array();
  493. if (empty($voucher_template_id)) {
  494. $where_array[] = array('vouchertemplate_enddate', '<', TIMESTAMP);
  495. } else {
  496. $where_array[] = array('vouchertemplate_id', '=', $voucher_template_id);
  497. }
  498. $where_array[] = array('vouchertemplate_state', '=', $this->templatestate_arr['usable'][0]);
  499. Db::name('vouchertemplate')->where($where_array)->update(array('vouchertemplate_state' => $this->templatestate_arr['disabled'][0]));
  500. }
  501. }