* @day: 2017/11/11 */ namespace app\models\store; use app\admin\model\system\SystemStoreBill; use app\models\system\SystemStore; use crmeb\services\PHPExcelService; use crmeb\traits\ModelTrait; use crmeb\basic\BaseModel; /** * 商品浏览分析 * Class StoreVisit * @package app\admin\model\store */ class Card extends BaseModel { /** * 数据表主键 * @var string */ protected $pk = 'id'; /** * 模型名称 * @var string */ protected $name = 'card'; use ModelTrait; /** * @param $where * @return array */ public static function systemPage($where) { $model = new self; $model = $model->alias('s'); if ($where['status'] != '') $model = $model->where('s.status', $where['status']); if (isset($where['store_id']) && $where['store_id'] != '') $model = $model->where('s.store_id', $where['store_id']); if (isset($where['name']) && $where['name'] != '') $model = $model->where('s.name|s.code', 'LIKE', "%$where[name]%"); $model = $model->page(bcmul($where['page'], $where['limit'], 0), $where['limit']); $model = $model->order('s.id desc'); return self::page($model, function ($item) { if ($item['status'] == 0) { $item['start_name'] = '待赠送'; } else if ($item['status'] == 1) { $item['start_name'] = '待核销'; } else $item['start_name'] = '已使用'; $item['store'] = $item['store_id'] ? SystemStore::where('id', $item['store_id'])->value('name') . '/' . $item['store_id'] : '--'; $item['check_store'] = $item['check_store_id'] ? SystemStore::where('id', $item['check_store_id'])->value('name') . '/' . $item['check_store_id'] : '--'; $item['add_time_text'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '--'; $item['send_time_text'] = $item['send_time'] ? date('Y-m-d H:i:s', $item['send_time']) : '--'; $item['check_time_text'] = $item['check_time'] ? date('Y-m-d H:i:s', $item['check_time']) : '--'; }, $where, $where['limit']); } public static function SaveExcel($where) { $model = new self; if ($where['status'] != '') $model = $model->where('s.status', $where['status']); if ($where['name'] != '') $model = $model->where('s.name|s.code', 'LIKE', "%$where[name]%"); $list = $model->order('id desc')->select(); count($list) && $list = $list->toArray(); $excel = []; foreach ($list as $item) { if ($item['status'] == 0) { $item['start_name'] = '待赠送'; } else if ($item['status'] == 1) { $item['start_name'] = '待核销'; } else $item['start_name'] = '已使用'; $item['store'] = $item['store_id'] ? SystemStore::where('id', $item['store_id'])->value('name') . '/' . $item['store_id'] : '--'; $item['check_store'] = $item['check_store_id'] ? SystemStore::where('id', $item['check_store_id'])->value('name') . '/' . $item['check_store_id'] : '--'; $item['add_time_text'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '--'; $item['send_time_text'] = $item['send_time'] ? date('Y-m-d H:i:s', $item['send_time']) : '--'; $item['check_time_text'] = $item['check_time'] ? date('Y-m-d H:i:s', $item['check_time']) : '--'; $excel[] = [ $item['id'], $item['name'], $item['code'], $item['password'], $item['price'], $item['store_award'], $item['start_name'], $item['store'], $item['check_store'], $item['add_time_text'], $item['send_time_text'], $item['check_time_text'], ]; } PHPExcelService::setExcelHeader(['编号', '标题', '卡号', '密码', '价格', '奖金', '状态' , '发放门店', '核销门店', '添加时间', '发放时间', '核销时间',]) ->setExcelTile('礼品卡导出', ' ', ' 生成时间:' . date('Y-m-d H:i:s', time())) ->setExcelContent($excel) ->ExcelSave(); } public static function createCode() { do { $str = date('Y') . '-' . substr(time(), 6, 4) . '-' . rand(1000, 9999) . '-' . rand(1000, 9999); } while (self::be(['code' => $str])); return $str; } public static function createPassword() { return substr(md5(md5('lpk.zccy.rand' . rand(100000, 999999)) . rand(0, 100000)), 0, 16); } public static function sendCard($store_id, $card_code) { $card = self::where('code', $card_code)->find(); if (!$card) return self::setErrorInfo('礼品卡不存在'); if ($card['status'] != 0) return self::setErrorInfo('礼品卡不可赠送'); $card['store_id'] = $store_id; $card['send_time'] = time(); $card['status'] = 1; $res = $card->save(); if ($res) return true; else return self::setErrorInfo('礼品卡送出失败'); } public static function checkCard($store_id, $card_code, $password) { $card = self::where('code', $card_code)->find(); if (!$card) return self::setErrorInfo('礼品卡不存在'); if ($card['status'] != 1) return self::setErrorInfo('礼品卡不可核销'); if ($card['password'] !== $password) return self::setErrorInfo('卡密错误'); $card['check_store_id'] = $store_id; $card['check_time'] = time(); $card['status'] = 2; $res = $card->save() && self::sendAward($card); if ($res) return true; else return self::setErrorInfo(self::getErrorInfo('核销失败')); } public static function sendAward($card) { if ($card['store_id'] && $card['store_award'] && SystemStore::be(['id' => $card['store_id']])) { $store = SystemStore::get($card['store_id']); $mark = '赠送的礼品卡' . $card['name'] . '(' . $card['id'] . ')核销成功,获得佣金。'; SystemStoreBill::income('礼品卡佣金', $card['store_id'], 'brokerage_price', 'card_award', $card['store_award'], $card['id'], bcadd($store['brokerage_price'], $card['store_award'], 2), $mark); SystemStore::bcInc($card['store_id'], 'brokerage_price', $card['store_award'], 'id'); } return true; } }