CompanyController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. class CompanyController extends Controller
  3. {
  4. public static string $table = 'company';
  5. public function actionInfo()
  6. {
  7. $id = Helper::getPostInt('id');
  8. if ($id <= 0) {
  9. Helper::error('参数错误');
  10. }
  11. if (!$this->checkCompanyId($id)) {
  12. Helper::error('您没有权限操作此数据');
  13. }
  14. $data = DB::getInfoById(self::$table, $id);
  15. if (!$data) {
  16. Helper::error('数据不存在');
  17. }
  18. $cri = DbCriteria::simpleCompare(['id' => [$data['last_user_id'], $data['bind_user_id']]])->setSelect('id, username');
  19. $users = Helper::arrayColumn(DB::getListWithCriteria('useradmin', $cri), 'username', 'id');
  20. $data['last_user_name'] = $users[$data['last_user_id']] ?? '';
  21. $data['bind_user_name'] = $users[$data['bind_user_id']] ?? '';
  22. $data['distinct'] = [
  23. $data['province'],
  24. $data['city'],
  25. // $data['area'],
  26. ];
  27. // 关联食堂
  28. $data['canteen_names'] = [];
  29. $data['canteens'] = [];
  30. $cri = DbCriteria::simpleCompare(['t.company_id' => $id])
  31. ->setAlias('t')
  32. ->setSelect('t.school_id, t.canteen_id, s.name as school_name, c.name as canteen_name')
  33. ->setJoin('left join wx_school s on t.school_id = s.id')
  34. ->addJoin('left join wx_canteen c on t.canteen_id = c.id');
  35. $canteens = DB::getListWithCriteria('company_canteen_relation', $cri);
  36. foreach ($canteens['records'] as $item) {
  37. $data['canteens'][] = [(int)$item['school_id'], (int)$item['canteen_id']];
  38. $data['canteen_names'][] = "{$item['canteen_name']}({$item['school_name']})";
  39. }
  40. // 关系人
  41. $relations = DB::getListWithCriteria(
  42. 'company_contact',
  43. DbCriteria::simpleCompare(['company_id' => $id])->setSelect('id, name, phone, position, weixin'),
  44. );
  45. $data['relations'] = $relations['records'];
  46. Helper::ok($data);
  47. }
  48. public function actionList()
  49. {
  50. $filter = [
  51. 'is_del' => 0,
  52. 'phone' => Helper::getPostString('phone'),
  53. 'id' => $this->getcompanyFilter(),
  54. ];
  55. if ($name = Helper::getPostString('name')) {
  56. $filter['name'] = '%' . $name;
  57. }
  58. $address = Helper::getArrParam($_POST, 'address', Helper::PARAM_KEY_TYPE['array_string']);
  59. $filter['province'] = $address[0]?? null;
  60. $filter['city'] = $address[1]?? null;
  61. $filter['area'] = $address[2]?? null;
  62. if ($school_id = Helper::getPostInt('school_id')) {
  63. $cri = DbCriteria::simpleCompare(['school_id' => $school_id])->setSelect('company_id');
  64. $ids = Helper::arrayColumn(
  65. DB::getListWithCriteria('company_canteen_relation', $cri),
  66. 'company_id'
  67. );
  68. $filter['id'] = $ids?: -1;
  69. }
  70. $cri = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc');;
  71. $data = DB::getListWithCriteria(self::$table, $cri);
  72. if (!empty($data['records'])) {
  73. $users = Helper::arrayColumn(
  74. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username')),
  75. 'username',
  76. 'id'
  77. );
  78. $data['records'] = array_map(function ($item) use ($users) {
  79. $item['last_user_name'] = $users[$item['last_user_id']] ?? '-';
  80. $item['bind_user_name'] = $users[$item['bind_user_id']] ?? '-';
  81. return $item;
  82. }, $data['records']);
  83. }
  84. Helper::ok($data);
  85. }
  86. public function actionDelete()
  87. {
  88. $id = Helper::getPostInt('id');
  89. if ($id < 1) {
  90. Helper::error('参数错误');
  91. }
  92. if (!$this->checkCompanyId($id)) {
  93. Helper::error('您没有权限操作此数据');
  94. }
  95. Db::updateById(self::$table, ['is_del' => 1], $id);
  96. Helper::ok();
  97. }
  98. public function actionAdd()
  99. {
  100. $this->_save();
  101. }
  102. public function actionEdit()
  103. {
  104. $id = Helper::getPostInt('id');
  105. if (!$id) {
  106. Helper::error('参数错误');
  107. }
  108. if (!$this->checkCompanyId($id)) {
  109. Helper::error('您没有权限操作此数据');
  110. }
  111. $this->_save($id);
  112. }
  113. private function _save($id = 0)
  114. {
  115. $data = [
  116. 'name' => Helper::getPostString('name'),
  117. 'address' => Helper::getPostString('address'),
  118. 'memo' => Helper::getPostString('memo'),
  119. 'bind_user_id' => Helper::getPostInt('bind_user_id'),
  120. ];
  121. // 空字段检测
  122. if (!Helper::checkEmptyKey($data, ['name', 'address', 'memo'], ['memo'])) {
  123. Helper::error('参数错误');
  124. }
  125. // 处理地区
  126. $district = Helper::getArrParam($_POST, 'distinct', Helper::PARAM_KEY_TYPE['array_string']);
  127. $district = array_filter($district);
  128. if (count($district) != 2) {
  129. Helper::error('地区参数错误');
  130. }
  131. $data['province'] = $district[0];
  132. $data['city'] = $district[1];
  133. $data['area'] = '';
  134. // 关联食堂
  135. $canteens = $_POST['canteens']?? [];
  136. if (!$canteens) {
  137. Helper::error('请选择关联的食堂');
  138. }
  139. foreach ($canteens as $k => $canteen) {
  140. $canteens[$k] = array_filter(explode(',', $canteen));
  141. if (count($canteens[$k]) != 2 || empty($canteens[$k][0]) || empty($canteens[$k][1])) {
  142. Helper::error('选择的食堂参数有误 ' . json_encode($canteens));
  143. }
  144. }
  145. $name = $data['name'];
  146. // 检测名称重复
  147. $cri = DbCriteria::simpleCompare(['name' => $name])->setSelect('id');
  148. if ($id > 0) {
  149. $cri->addCondition('id!=' . $id);
  150. }
  151. if ($fid = DB::getScalerWithCriteria(self::$table, $cri)) {
  152. Helper::error('公司名称已存在 ' . $fid);
  153. }
  154. $this->dobuleCheck();
  155. $trans = \Yii::app()->db->beginTransaction();
  156. try {
  157. if ($id) {
  158. DB::updateById(self::$table, $data, $id);
  159. DB::deleteByCondition('company_canteen_relation', ['company_id' => $id]);
  160. } else {
  161. $id = DB::addData(self::$table, $data);
  162. if (!$id) {
  163. throw new \Exception('添加失败');
  164. }
  165. // 给用户操作权限
  166. $user = DB::getInfoById('useradmin', $this->getUserId());
  167. if (!str_contains($user['company_ids'], '-1')) {
  168. DB::updateById(
  169. 'useradmin',
  170. ['company_ids' => trim($user['company_ids'] . ',' . $id, ',')],
  171. $this->getUserId()
  172. );
  173. }
  174. $this->clearAuth();
  175. }
  176. $batchArr = [];
  177. foreach ($canteens as $canteen) {
  178. $batchArr[] = [
  179. 'company_id' => $id,
  180. 'school_id' => $canteen[0],
  181. 'canteen_id' => $canteen[1],
  182. ];
  183. }
  184. DB::safeBatchInsert('company_canteen_relation', $batchArr);
  185. $trans->commit();
  186. } catch (\Exception $e) {
  187. $trans->rollback();
  188. Helper::error($e->getMessage());
  189. }
  190. Helper::ok();
  191. }
  192. public function actionUpdateAttr()
  193. {
  194. $id = Helper::getPostInt('id');
  195. $attr = Helper::getPostString('attr');
  196. $value = Helper::getPostString('value');
  197. if ($id <= 0 || !$attr) {
  198. Helper::error('参数错误');
  199. }
  200. if (!$this->checkCompanyId($id)) {
  201. Helper::error('您没有权限操作此数据');
  202. }
  203. if (!in_array($attr, ['is_eleme_in_school', 'person_num'])) {
  204. Helper::error('参数错误2');
  205. }
  206. if ($attr == 'is_eleme_in_school' && !in_array($value, [1, 0])) {
  207. Helper::error('参数错误3');
  208. }
  209. if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
  210. Helper::error('更新失败');
  211. }
  212. Helper::ok();
  213. }
  214. }