LySilentSign.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. namespace addons\AvoidTax\common\models;
  3. use Yii;
  4. use yii\data\Pagination;
  5. use yii\helpers\Json;
  6. /**
  7. * This is the model class for table "{{%addons_tax_ly_silent_sign}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $user_id
  12. * @property string $name 真实姓名
  13. * @property string $phone 手机号码
  14. * @property string $id_card 身份证
  15. * @property string $account_id 企业账号ID
  16. * @property string|null $template_id 企业合同ID
  17. * @property string|null $email 邮箱
  18. * @property string|null $callback_url 成功回调链接
  19. * @property string|null $extra_msg 附加信息
  20. * @property int $default
  21. * @property int $status
  22. * @property string|null $response 响应信息
  23. * @property int|null $created_at
  24. * @property int|null $updated_at
  25. */
  26. class LySilentSign extends \common\models\base\BaseActiveRecord
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%addons_tax_ly_silent_sign}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['mall_id', 'user_id', 'name', 'phone', 'id_card', 'account_id', 'default'], 'required'],
  42. [['mall_id', 'user_id', 'status', 'created_at', 'updated_at'], 'integer'],
  43. [['response'], 'string'],
  44. [['name', 'phone', 'id_card', 'account_id', 'template_id'], 'string', 'max' => 32],
  45. [['email', 'account'], 'string', 'max' => 64],
  46. [['callback_url', 'extra_msg'], 'string', 'max' => 255],
  47. ];
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'id' => 'ID',
  56. 'mall_id' => 'Mall ID',
  57. 'user_id' => 'User ID',
  58. 'name' => 'Name',
  59. 'phone' => 'Phone',
  60. 'id_card' => 'Id Card',
  61. 'account_id' => 'Account ID',
  62. 'template_id' => 'Template ID',
  63. 'email' => 'Email',
  64. 'callback_url' => 'Callback Url',
  65. 'extra_msg' => 'Extra Msg',
  66. 'default' => 'Default',
  67. 'status' => 'Status',
  68. 'response' => 'Response',
  69. 'created_at' => 'Created At',
  70. 'updated_at' => 'Updated At',
  71. ];
  72. }
  73. /**
  74. * @return ActiveQueryInterface the relational query object.
  75. */
  76. public function getUser()
  77. {
  78. return $this->hasOne(User::class, ['id' => 'user_id']);
  79. }
  80. /**
  81. * 获取分页数据
  82. *
  83. * @param integer $page
  84. * @param integer $limit
  85. * @param callable $callback
  86. * @param string $order_by
  87. * @return array
  88. */
  89. public static function getPageList(
  90. int $page, int $limit, callable $callback, string $order_by = 'id'
  91. ) :array
  92. {
  93. $model = static::find();
  94. if (!empty($callback)) call_user_func($callback, $model);
  95. $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
  96. $model->limit($pagination->limit)->offset($pagination->offset);
  97. $list = $model->asArray()->orderBy($order_by)->all();
  98. $data['list'] = $list;
  99. $data['page_count'] = $pagination->pageCount;
  100. $data['current_page'] = $page + 1;
  101. return $data;
  102. }
  103. /**
  104. * ID查询
  105. *
  106. * @param integer $mall_id
  107. * @param integer $id
  108. * @return LySilentSign|array|null a single row of query result. Depending on the setting of [[asArray]],
  109. */
  110. public static function getUserByIdAndMallId(int $mall_id, int $id)
  111. {
  112. return static::find()
  113. ->where(['mall_id' => $mall_id, 'id' => $id])
  114. ->one();
  115. }
  116. /**
  117. * 修改账号
  118. *
  119. * @param string $mobile
  120. * @param string|null $account
  121. * @return boolean
  122. */
  123. public function changeMobile(string $mobile, $account = '')
  124. {
  125. $this->phone = $mobile;
  126. $this->account = $account;
  127. return $this->save();
  128. }
  129. /**
  130. * 添加电签记录
  131. *
  132. * @param integer $mall_id
  133. * @param integer $user_id
  134. * @param string $name
  135. * @param string $phone
  136. * @param string $id_card
  137. * @param string $account_id
  138. * @param array $response
  139. * @param string|null $template_id
  140. * @param string|null $email
  141. * @param string|null $callback_url
  142. * @param string|null $extra_msg
  143. * @return boolean
  144. */
  145. public static function addSilentSign(
  146. int $mall_id,
  147. int $user_id,
  148. string $name,
  149. string $phone,
  150. string $id_card,
  151. string $account_id,
  152. array $response,
  153. string $template_id = null,
  154. string $email = null,
  155. string $callback_url = null,
  156. string $extra_msg = null
  157. )
  158. {
  159. $model = new static;
  160. $model->mall_id = $mall_id;
  161. $model->user_id = $user_id;
  162. $model->name = $name;
  163. $model->phone = $phone;
  164. $model->id_card = $id_card;
  165. $model->account_id = $account_id;
  166. $model->response = Json::encode($response);
  167. $model->template_id = $template_id;
  168. $model->email = $email;
  169. $model->callback_url = $callback_url;
  170. $model->extra_msg = $extra_msg;
  171. $model->default = (int)static::isSetDefault($user_id);
  172. $model->status = $model->checkStatus();
  173. return $model->save();
  174. }
  175. /**
  176. * 查询是否设置默认
  177. *
  178. * @param integer $uid
  179. * @return boolean
  180. */
  181. public static function isSetDefault(int $uid)
  182. {
  183. return static::find()->where(['user_id' => $uid, 'status' => 1])->count() > 0
  184. ? false
  185. : true;
  186. }
  187. /**
  188. * 获取该用户是否签约
  189. *
  190. * @param integer $user_id
  191. * @return boolean
  192. */
  193. public static function getUserIsSign(int $user_id)
  194. {
  195. return static::find()
  196. ->where(['user_id' => $user_id, 'status' => 1])
  197. ->select('id')
  198. ->one() ? true : false;
  199. }
  200. /**
  201. * 获取签约用户信息
  202. *
  203. * @param integer $user_id
  204. * @return LySilentSign|array|null
  205. */
  206. public static function getSignUser(int $user_id)
  207. {
  208. return static::find()
  209. ->where(['user_id' => $user_id, 'status' => 1])
  210. ->one();
  211. }
  212. /**
  213. * 身份信息获取
  214. *
  215. * @param integer $mall_id
  216. * @param string $idcard
  217. * @param string $name
  218. * @return LySilentSign|array|null
  219. */
  220. public static function getAutnByIdcardAndName(int $mall_id, string $idcard, string $name)
  221. {
  222. return static::find()
  223. ->where(['id_card' => $idcard, 'name' => $name, 'mall_id' => $mall_id, 'status' => 1])
  224. ->one();
  225. }
  226. /**
  227. * uid是否签约
  228. *
  229. * @param integer $mall_id
  230. * @param integer $uid
  231. * @return boolean
  232. */
  233. public static function uidIsSign(int $mall_id, int $uid)
  234. {
  235. return static::find()
  236. ->select('id')
  237. ->where(['user_id' => $uid, 'mall_id' => $mall_id, 'status' => 1])
  238. ->one() ? true : false;
  239. }
  240. /**
  241. * 创建一条新的认证记录
  242. *
  243. * @param integer $user_id
  244. * @return boolean
  245. */
  246. public function createNewAuth(int $user_id, string $mobile)
  247. {
  248. $model = new static;
  249. $model->user_id = $user_id;
  250. $model->mall_id = $this->mall_id;
  251. $model->name = $this->name;
  252. $model->phone = $mobile;
  253. $model->id_card = $this->id_card;
  254. $model->account_id = $this->account_id;
  255. $model->template_id = $this->template_id;
  256. $model->email = $this->email;
  257. $model->callback_url = $this->callback_url;
  258. $model->extra_msg = $this->extra_msg;
  259. $model->default = (int)static::isSetDefault($user_id);
  260. $model->status = $this->status;
  261. $model->response = $this->response;
  262. return $model->save();
  263. }
  264. /**
  265. * @return integer
  266. */
  267. private function checkStatus()
  268. {
  269. $response = Json::decode($this->response);
  270. if ($response['code'] == '0000') {
  271. return 1;
  272. }
  273. if ($response['code'] == '200' && !empty($response['data']) && $response['data']['status'] == 1) {
  274. return 1;
  275. }
  276. return 0;
  277. }
  278. /**
  279. * 获取用户的认证列表
  280. *
  281. * @param integer $mall_id
  282. * @param integer $user_id
  283. * @return array|static[]
  284. */
  285. public static function getUserAuthList(int $mall_id, int $user_id)
  286. {
  287. return static::find()
  288. ->where(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => 1])
  289. ->select(['id', 'mall_id', 'user_id', 'name', 'phone as mobile', 'id_card', 'default'])
  290. ->asArray()
  291. ->all();
  292. }
  293. /**
  294. * 获取
  295. *
  296. * @param integer $mall_id
  297. * @param integer $id
  298. * @return static|null
  299. */
  300. public static function getAuthById(int $mall_id, int $id)
  301. {
  302. return static::find()
  303. ->where(['mall_id' => $mall_id, 'id' => $id])
  304. ->one();
  305. }
  306. /**
  307. * @param integer $mall_id
  308. * @param integer $user_id
  309. * @return boolean
  310. */
  311. public static function setNotDefault(int $mall_id, int $user_id)
  312. {
  313. return static::updateAll(
  314. ['default' => 0],
  315. ['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => 1]
  316. );
  317. }
  318. /**
  319. * @return boolean
  320. */
  321. public function setDefault()
  322. {
  323. $this->default = 1;
  324. return $this->save();
  325. }
  326. /**
  327. * 获取默认的签约信息
  328. *
  329. * @param integer $mall_id
  330. * @param integer $user_id
  331. * @return static|null
  332. */
  333. public static function getDefaultSignInfo(int $mall_id, int $user_id)
  334. {
  335. return static::find()
  336. ->select(['id', 'mall_id', 'user_id', 'name', 'phone as mobile', 'account', 'id_card', 'default'])
  337. ->where(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => 1, 'default' => 1])
  338. ->asArray()
  339. ->one();
  340. }
  341. /**
  342. * 通过name mobile idcard获取用户已签约信息
  343. *
  344. * @param integer $user_id
  345. * @param string $name
  346. * @param string $mobile
  347. * @param string $idcard
  348. * @return static|null
  349. */
  350. public static function getSignByNMI(int $user_id, string $name, string $mobile, string $idcard)
  351. {
  352. return static::find()
  353. ->where([
  354. 'user_id' => $user_id,
  355. 'name' => $name,
  356. 'phone' => $mobile,
  357. 'id_card' => $idcard,
  358. 'status' => 1
  359. ])
  360. ->one();
  361. }
  362. /**
  363. * 查询用户是否有通过名字手机身份证签约
  364. *
  365. * @param integer $uid
  366. * @param string $name
  367. * @param string $mobile
  368. * @param string $idcard
  369. * @return boolean
  370. */
  371. public static function userIsSignByNMI(int $uid, string $name, string $mobile, string $idcard)
  372. {
  373. return !empty(
  374. static::find()
  375. ->where(['user_id' => $uid, 'name' => $name, 'phone' => $mobile, 'id_card' => $idcard])
  376. ->select('id')
  377. ->one()
  378. ) ? true : false;
  379. }
  380. /**
  381. * 获取用户签约信息
  382. *
  383. * @param integer $user_id
  384. * @param integer $id
  385. * @return static|null
  386. */
  387. public static function getSignById(int $user_id, int $id)
  388. {
  389. return static::find()
  390. ->where(['user_id' => $user_id, 'id' => $id, 'status' => 1])
  391. ->one();
  392. }
  393. /**
  394. * 获取签约成功详情
  395. *
  396. * @param integer $mall_id
  397. * @param integer $id
  398. * @return static|null
  399. */
  400. public static function getOkAuthById(int $mall_id, int $id)
  401. {
  402. return static::find()
  403. ->where(['mall_id' => $mall_id, 'id' => $id, 'status' => 1])
  404. ->one();
  405. }
  406. /**
  407. * 设置解约状态
  408. *
  409. * @return boolean
  410. */
  411. public function setLift()
  412. {
  413. $this->status = 2;
  414. return $this->save();
  415. }
  416. }