Vote.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace app\admin\controller\vote;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\system\SystemAttachment;
  5. use app\admin\model\system\SystemUserLevel;
  6. use Exception;
  7. use crmeb\services\{FormBuilder as Form,
  8. JsonService,
  9. UtilService as Util,
  10. JsonService as Json,
  11. UtilService
  12. };
  13. use FormBuilder\exception\FormBuilderException;
  14. use think\facade\Route as Url;
  15. use app\models\vote\{
  16. Vote as VoteModel, VoteSub, UserVote
  17. };
  18. /**
  19. * 图文管理
  20. * Class WechatNews
  21. * @package app\admin\controller\wechat
  22. */
  23. class Vote extends AuthController
  24. {
  25. /**
  26. * TODO 显示后台管理员添加的图文
  27. * @return mixed
  28. * @throws Exception
  29. */
  30. public function index()
  31. {
  32. $money_type = json_encode(sys_data('money_type'));
  33. $this->assign(compact('money_type'));
  34. $this->assign('count_vote', VoteModel::count());
  35. return $this->fetch();
  36. }
  37. /**
  38. * 获取vote表
  39. *
  40. * @return void
  41. */
  42. public function get_list()
  43. {
  44. $where = Util::getMore([
  45. ['page', 1],
  46. ['limit', 20],
  47. ['title', ''],
  48. ['status', ''],
  49. ['money_type', ''],
  50. ]);
  51. Json::successlayui(VoteModel::getList($where));
  52. }
  53. /**
  54. * 获取voteSub表
  55. *
  56. * @return void
  57. */
  58. public function get_sub_list()
  59. {
  60. $where = Util::getMore([
  61. ['page', 1],
  62. ['limit', 20],
  63. ['id', 0],
  64. ]);
  65. Json::successlayui(VoteSub::getList($where));
  66. }
  67. /**
  68. * 创建form表单
  69. *
  70. * @param int $id
  71. * @return string
  72. * @throws Exception
  73. * @throws FormBuilderException
  74. */
  75. public function create($id = 0)
  76. {
  77. $money_type = sys_data('money_type');
  78. $money_type_select = [];
  79. foreach ($money_type as $v) {
  80. $money_type_select[] = ['label' => $v['name'], 'value' => $v['code']];
  81. }
  82. if ($id) $info = VoteModel::get($id);
  83. $field[] = Form::input('vote_name', '投票标题', isset($info) ? $info->vote_name : '')->col(Form::col(24))->required();
  84. $field[] = Form::select('money_type', '投票目标', isset($info) ? $info->money_type : '')->options($money_type_select)->col(24)->required();
  85. $field[] = Form::number('money_precision', '币种精度', isset($info) ? $info->money_precision : 0)->min(0)->col(24)->placeholder('保留x位小数')->required();
  86. $field[] = Form::time('day_start', '完成后下一期开始时间', isset($info) ? $info->day_start : 0)->col(24)->placeholder('第二天时间');
  87. $field[] = Form::number('start_vote_num', '第一期投票数量', isset($info) ? $info->start_vote_num : 0)->min(0)->col(12)->step(0.00000001)->required();
  88. $field[] = Form::number('step_vote_num', '投票数量每期增量(%)', isset($info) ? $info->step_vote_num : 0)->min(0)->col(12)->required();
  89. $field[] = Form::number('start_max_vote_num', '第一期个人投票上限', isset($info) ? $info->start_max_vote_num : 0)->min(0)->col(12)->step(0.00000001)->required();
  90. $field[] = Form::number('step_max_vote_num', '投票上限每期增加', isset($info) ? $info->step_max_vote_num : 0)->min(0)->col(12)->step(0.00000001)->required();
  91. $field[] = Form::number('start_still_time', '第一期投票时长(小时)', isset($info) ? $info->start_still_time : 0)->min(0)->col(12)->required();
  92. $field[] = Form::number('step_still_time', '投票时长每期增加(小时)', isset($info) ? $info->step_still_time : 0)->min(0)->col(12)->required();
  93. $field[] = Form::number('success_back', '投票成功返还(不含本金)(%)', isset($info) ? $info->success_back : 0)->min(0)->col(12)->required();
  94. $field[] = Form::number('fail_back_now', '投票失败当期返还(%)', isset($info) ? $info->fail_back_now : 0)->min(0)->col(12)->required();
  95. $field[] = Form::number('fail_back_last', '投票失败上期返还(%)', isset($info) ? $info->fail_back_last : 0)->min(0)->col(12)->required();
  96. $field[] = Form::number('fail_back_before_last', '投票失败上上期返还(%)', isset($info) ? $info->fail_back_before_last : 0)->min(0)->col(12)->required();
  97. $field[] = Form::select('other_back_money_type', '未返还部分转化为', isset($info) ? $info->other_back_money_type : '')->options($money_type_select)->col(24);
  98. if (isset($info) && $info['status'] != 2)
  99. $field[] = Form::radio('status', '状态', isset($info) ? $info->status : 0)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  100. $form = Form::make_post_form('添加投票', $field, Url::buildUrl('save', ['id' => $id]), 2);
  101. $this->assign(compact('form'));
  102. return $this->fetch('public/form-builder');
  103. }
  104. /**
  105. * 添加或者修改
  106. * @param $id
  107. * @return void
  108. */
  109. public function save($id = 0)
  110. {
  111. $data = UtilService::postMore([
  112. ['vote_name', ''],
  113. ['money_type', ''],
  114. ['money_precision', 0],
  115. ['start_vote_num', 0],
  116. ['step_vote_num', 0],
  117. ['start_max_vote_num', 0],
  118. ['step_max_vote_num', 0],
  119. ['start_still_time', 0],
  120. ['step_still_time', 0],
  121. ['success_back', 0],
  122. ['fail_back_now', 0],
  123. ['fail_back_last', 0],
  124. ['fail_back_before_last', 0],
  125. ['other_back_money_type', ''],
  126. ['status', 0],
  127. ['day_start', '10:00:00'],
  128. ]);
  129. VoteModel::beginTrans();
  130. try {
  131. //修改
  132. if ($id) {
  133. if (VoteModel::edit($data, $id)) {
  134. VoteModel::commitTrans();
  135. JsonService::successful('修改成功');
  136. } else {
  137. VoteModel::rollbackTrans();
  138. JsonService::fail('修改失败');
  139. }
  140. } else {
  141. //新增
  142. $data['add_time'] = time();
  143. $res = VoteModel::create($data);
  144. if ($res) {
  145. //生成第一期子投票
  146. VoteSub::create([
  147. 'vote_sub_name' => $data['vote_name'] . '第1期',
  148. 'vote_id' => $res->id,
  149. 'vote_num' => $data['start_vote_num'],
  150. 'max_vote_num' => $data['start_max_vote_num'],
  151. 'start_time' => time(),
  152. 'end_time' => time() + 3600 * $data['start_still_time'],
  153. 'loop' => 1,
  154. 'status' => 0,
  155. 'add_time' => time(),
  156. ]);
  157. VoteModel::commitTrans();
  158. JsonService::successful('添加成功');
  159. } else {
  160. VoteModel::rollbackTrans();
  161. JsonService::fail('添加失败');
  162. }
  163. }
  164. } catch (\Exception $e) {
  165. VoteModel::rollbackTrans();
  166. JsonService::fail($e->getMessage());
  167. }
  168. }
  169. /**
  170. * 会员详情
  171. */
  172. public function see($id = '')
  173. {
  174. $this->assign([
  175. 'id' => $id,
  176. 'vote_info' => VoteModel::getInfo($id),
  177. 'is_layui' => true,
  178. ]);
  179. return $this->fetch();
  180. }
  181. /**
  182. * 创建form表单
  183. *
  184. * @param int $id
  185. * @return string
  186. * @throws Exception
  187. * @throws FormBuilderException
  188. */
  189. public function create_sub($id = 0, $vid = 0)
  190. {
  191. if (!$id && !$vid) {
  192. $this->failed('参数异常');
  193. }
  194. if ($id) $info = VoteSub::get($id);
  195. if (isset($info)) $vid = $info['vote_id'];
  196. // var_dump($id);
  197. $field[] = Form::hidden('vote_id', $vid);
  198. $field[] = Form::input('vote_sub_name', '投票标题', isset($info) ? $info->vote_sub_name : '')->col(Form::col(24))->required();
  199. $field[] = Form::number('vote_num', '投票数量', isset($info) ? $info->vote_num : 0)->min(0)->col(12)->step(0.00000001)->required();
  200. $field[] = Form::number('max_vote_num', '个人投票上限', isset($info) ? $info->max_vote_num : 0)->min(0)->col(12)->step(0.00000001)->required();
  201. $field[] = Form::dateTime('start_time', '开始时间', isset($info) ? date('Y-m-d H:i:s', $info->start_time) : '');
  202. $field[] = Form::dateTime('end_time', '结束时间', isset($info) ? date('Y-m-d H:i:s', $info->end_time) : '');
  203. if ((isset($info) && $info['status'] != 2 && $info['status'] != 3) || !isset($info))
  204. $field[] = Form::radio('status', '状态', isset($info) ? $info->status : 0)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  205. $form = Form::make_post_form('添加子投票', $field, Url::buildUrl('save_sub', ['id' => $id]), 2);
  206. $this->assign(compact('form'));
  207. return $this->fetch('public/form-builder');
  208. }
  209. /**
  210. * 添加或者修改
  211. * @param $id
  212. * @return void
  213. */
  214. public function save_sub($id = 0)
  215. {
  216. $data = UtilService::postMore([
  217. ['vote_sub_name', ''],
  218. ['vote_id', ''],
  219. ['vote_num', ''],
  220. ['max_vote_num', ''],
  221. ['end_time', ''],
  222. ['start_time', ''],
  223. ['status', 0],
  224. ]);
  225. VoteSub::beginTrans();
  226. try {
  227. if (!$data['vote_id']) JsonService::fail('请选择投票');
  228. if (!$data['start_time']) JsonService::fail('请输入开始时间');
  229. if (!$data['end_time']) JsonService::fail('请输入结束时间');
  230. $data['start_time'] = strtotime($data['start_time']);
  231. $data['end_time'] = strtotime($data['end_time']);
  232. if ($data['end_time'] <= $data['start_time']) JsonService::fail('结束时间要大于开始时间');
  233. if (!$id) $data['loop'] = VoteSub::where('vote_id', $data['vote_id'])->max('loop') + 1;
  234. //修改
  235. if ($id) {
  236. if (VoteSub::edit($data, $id)) {
  237. VoteSub::commitTrans();
  238. JsonService::successful('修改成功');
  239. } else {
  240. VoteSub::rollbackTrans();
  241. JsonService::fail('修改失败');
  242. }
  243. } else {
  244. //新增
  245. $data['add_time'] = time();
  246. $res = VoteSub::create($data);
  247. if ($res) {
  248. VoteModel::commitTrans();
  249. JsonService::successful('添加成功');
  250. } else {
  251. VoteModel::rollbackTrans();
  252. JsonService::fail('添加失败');
  253. }
  254. }
  255. } catch (\Exception $e) {
  256. VoteModel::rollbackTrans();
  257. JsonService::fail($e->getMessage());
  258. }
  259. }
  260. public function see_people($id = 0)
  261. {
  262. $this->assign([
  263. 'id' => $id,
  264. ]);
  265. return $this->fetch();
  266. }
  267. public function people_list()
  268. {
  269. $where = Util::getMore([
  270. ['page', 1],
  271. ['limit', 20],
  272. ['id', 0],
  273. ]);
  274. Json::successlayui(UserVote::getUserList($where));
  275. }
  276. }