CompanyController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. $data = DB::getInfoById(self::$table, $id);
  12. if (!$data) {
  13. Helper::error('数据不存在');
  14. }
  15. $cri = DbCriteria::simpleCompare(['id' => [$data['last_user_id'], $data['bind_user_id']]])->setSelect('id, username');
  16. $users = Helper::arrayColumn(DB::getListWithCriteria('useradmin', $cri), 'username', 'id');
  17. $data['last_user_name'] = $users[$data['last_user_id']] ?? '';
  18. $data['bind_user_name'] = $users[$data['bind_user_id']] ?? '';
  19. $data['distinct'] = [
  20. $data['province'],
  21. $data['city'],
  22. $data['area'],
  23. ];
  24. // 关联食堂
  25. $data['canteen_names'] = [];
  26. $data['canteens'] = [];
  27. $cri = DbCriteria::simpleCompare(['t.company_id' => $id])
  28. ->setAlias('t')
  29. ->setSelect('t.school_id, t.canteen_id, s.name as school_name, c.name as canteen_name')
  30. ->setJoin('left join wx_school s on t.school_id = s.id')
  31. ->addJoin('left join wx_canteen c on t.canteen_id = c.id');
  32. $canteens = DB::getListWithCriteria('company_canteen_relation', $cri);
  33. foreach ($canteens['records'] as $item) {
  34. $data['canteens'][] = [(int)$item['school_id'], (int)$item['canteen_id']];
  35. $data['canteen_names'][] = "{$item['canteen_name']}({$item['school_name']})";
  36. }
  37. // 关系人
  38. $relations = DB::getListWithCriteria(
  39. 'company_contact',
  40. DbCriteria::simpleCompare(['company_id' => $id])->setSelect('id, name, phone, position, weixin'),
  41. );
  42. $data['relations'] = $relations['records'];
  43. Helper::ok($data);
  44. }
  45. public function actionList()
  46. {
  47. $filter = [
  48. 'is_del' => 0,
  49. 'phone' => Helper::getPostString('phone')
  50. ];
  51. if ($name = Helper::getPostString('name')) {
  52. $filter['name'] = '%' . $name;
  53. }
  54. $address = Helper::getArrParam($_POST, 'address', Helper::PARAM_KEY_TYPE['array_string']);
  55. $filter['province'] = $address[0]?? null;
  56. $filter['city'] = $address[1]?? null;
  57. $filter['area'] = $address[2]?? null;
  58. if ($school_id = Helper::getPostInt('school_id')) {
  59. $cri = DbCriteria::simpleCompare(['school_id' => $school_id])->setSelect('company_id');
  60. $ids = Helper::arrayColumn(
  61. DB::getListWithCriteria('company_canteen_relation', $cri),
  62. 'company_id'
  63. );
  64. $filter['id'] = $ids?: -1;
  65. }
  66. $cri = DbCriteria::simpleCompareWithPage($filter)->setOrder('id desc');;
  67. $data = DB::getListWithCriteria(self::$table, $cri);
  68. if (!empty($data['records'])) {
  69. $users = Helper::arrayColumn(
  70. DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username')),
  71. 'username',
  72. 'id'
  73. );
  74. $data['records'] = array_map(function ($item) use ($users) {
  75. $item['last_user_name'] = $users[$item['last_user_id']] ?? '-';
  76. $item['bind_user_name'] = $users[$item['bind_user_id']] ?? '-';
  77. return $item;
  78. }, $data['records']);
  79. }
  80. Helper::ok($data);
  81. }
  82. /**
  83. * 下拉列表获取
  84. * @return void
  85. */
  86. public function actionGetSelectList()
  87. {
  88. $cri = DbCriteria::simpleCompare([])->setSelect('id, name');
  89. $data = DB::getListWithCriteria(self::$table, $cri);
  90. Helper::ok($data['records']??[]);
  91. }
  92. public function actionDelete()
  93. {
  94. $id = Helper::getPostInt('id');
  95. if ($id < 1) {
  96. Helper::error('参数错误');
  97. }
  98. Db::updateById(self::$table, ['is_del' => 1], $id);
  99. Helper::ok();
  100. }
  101. public function actionAdd()
  102. {
  103. $this->_save();
  104. }
  105. public function actionEdit()
  106. {
  107. $id = Helper::getPostInt('id');
  108. if (!$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) != 3) {
  129. Helper::error('地区参数错误');
  130. }
  131. $data['province'] = $district[0];
  132. $data['city'] = $district[1];
  133. $data['area'] = $district[2];
  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. $trans = \Yii::app()->db->beginTransaction();
  155. try {
  156. if ($id) {
  157. DB::updateById(self::$table, $data, $id);
  158. DB::deleteByCondition('company_canteen_relation', ['company_id' => $id]);
  159. } else {
  160. $id = DB::addData(self::$table, $data);
  161. if (!$id) {
  162. throw new \Exception('添加失败');
  163. }
  164. }
  165. $batchArr = [];
  166. foreach ($canteens as $canteen) {
  167. $batchArr[] = [
  168. 'company_id' => $id,
  169. 'school_id' => $canteen[0],
  170. 'canteen_id' => $canteen[1],
  171. ];
  172. }
  173. DB::safeBatchInsert('company_canteen_relation', $batchArr);
  174. $trans->commit();
  175. } catch (\Exception $e) {
  176. $trans->rollback();
  177. Helper::error($e->getMessage());
  178. }
  179. Helper::ok();
  180. }
  181. public function actionUpdateAttr()
  182. {
  183. $id = Helper::getPostInt('id');
  184. $attr = Helper::getPostString('attr');
  185. $value = Helper::getPostString('value');
  186. if ($id <= 0 || !$attr) {
  187. Helper::error('参数错误');
  188. }
  189. if (!in_array($attr, ['is_eleme_in_school', 'person_num'])) {
  190. Helper::error('参数错误2');
  191. }
  192. if ($attr == 'is_eleme_in_school' && !in_array($value, [1, 0])) {
  193. Helper::error('参数错误3');
  194. }
  195. if (DB::updateById(self::$table, [$attr => $value], $id) === false) {
  196. Helper::error('更新失败');
  197. }
  198. Helper::ok();
  199. }
  200. }