Department.Class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * 部门管理Controller
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/10/31
  7. * Time: 17:00
  8. */
  9. namespace JinDouYun\Controller\Department;
  10. use JinDouYun\Dao\Department\DDepartment;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use JinDouYun\Controller\BaseController;
  14. use JinDouYun\Model\Department\MDepartment;
  15. class Department extends BaseController
  16. {
  17. private $objMDepartment;
  18. public function __construct($isCheckAcl = true, $isMustLogin = true)
  19. {
  20. parent::__construct($isCheckAcl, $isMustLogin);
  21. $this->objMDepartment = new MDepartment($this->onlineEnterpriseId);
  22. }
  23. /**
  24. * 获取参数
  25. *
  26. * @return array
  27. */
  28. public function commonFieldFilter()
  29. {
  30. $params = $this->request->getRawJson();
  31. if (empty($params)) {
  32. $this->sendOutput('参数为空', ErrorCode::$paramError);
  33. }
  34. $returnData = [
  35. "departmentName" => isset($params['departmentName']) ? $params['departmentName'] : '', //varchar(255) NOT NULL COMMENT '部门名称',
  36. ];
  37. //必填项
  38. foreach ($returnData as $key => $value) {
  39. if (empty($value) && $value !== 0) {
  40. $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
  41. }
  42. }
  43. //选填项
  44. $returnData['pid'] = isset($params['pid']) ? $params['pid'] : 0;
  45. $returnData['order'] = isset($params['order']) ? $params['order'] : 0;
  46. $returnData['desc'] = isset($params['desc']) ? $params['desc'] : '';
  47. $returnData['extend']['departmentPidPath'] = isset($params['departmentPidPath']) ? $params['departmentPidPath'] : '';
  48. $returnData['extend'] = json_encode($returnData['extend']);
  49. $returnData['updateTime'] = time();
  50. return $returnData;
  51. }
  52. /**
  53. * 部门添加
  54. */
  55. public function addDepartment()
  56. {
  57. $addDepartmentData = $this->commonFieldFilter();
  58. $addDepartmentData['shopId'] = $this->shopId;
  59. $result = $this->objMDepartment->addDepartment($addDepartmentData);
  60. if ($result->isSuccess()) {
  61. parent::sendOutput($result->getData());
  62. } else {
  63. parent::sendOutput($result->getData(), $result->getErrorCode());
  64. }
  65. }
  66. /**
  67. * 部门删除
  68. */
  69. public function deleteDepartment()
  70. {
  71. $id = $this->request->param('request_id');
  72. if(empty($id)){
  73. $this->sendOutput('参数为空', ErrorCode::$paramError);
  74. }
  75. $params['id'] = $id;
  76. $result = $this->objMDepartment->deleteDepartment($params);
  77. if ($result->isSuccess()) {
  78. parent::sendOutput($result->getData());
  79. } else {
  80. parent::sendOutput($result->getData(), $result->getErrorCode());
  81. }
  82. }
  83. /**
  84. * 部门修改
  85. */
  86. public function updateDepartment()
  87. {
  88. $id['id'] = $this->request->param('request_id');
  89. if (empty($id['id'])) {
  90. $this->sendOutput('参数为空', ErrorCode::$paramError);
  91. }
  92. $params = $this->commonFieldFilter();
  93. $result = $this->objMDepartment->updateDepartment($params, $id);
  94. if ($result->isSuccess()) {
  95. parent::sendOutput($result->getData());
  96. } else {
  97. parent::sendOutput($result->getData(), $result->getErrorCode());
  98. }
  99. }
  100. /**
  101. * 部门列表
  102. */
  103. public function getAllDepartment()
  104. {
  105. $params = $this->request->getRawJson();
  106. //$pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  107. $selectParams['limit'] = null;
  108. $selectParams['offset'] = null;
  109. if(isset($params['keyword']) && !empty($params['keyword'])) {
  110. $selectParams['departmentName'] = $params['keyword'];
  111. }
  112. $selectParams['shopId'] = $this->shopId;
  113. $result = $this->objMDepartment->getAllDepartment($selectParams);
  114. if ($result->isSuccess()) {
  115. $returnData = $result->getData();
  116. /*$pageData = [
  117. 'pageIndex' => $params['page'],
  118. 'pageSize' => $params['pageSize'],
  119. 'pageTotal' => $returnData['total'],
  120. ];
  121. parent::sendOutput($returnData['data'], 0, $pageData);*/
  122. parent::sendOutput($returnData['data']);
  123. } else {
  124. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  125. }
  126. }
  127. /**
  128. * 部门详情
  129. */
  130. public function getDepartmentInfo()
  131. {
  132. $params['id'] = $this->request->param('request_id');
  133. if (empty($params['id'])) {
  134. $this->sendOutput('参数为空', ErrorCode::$paramError);
  135. }
  136. //自增id
  137. $result = $this->objMDepartment->getDepartmentInfo($params);
  138. if ($result->isSuccess()) {
  139. parent::sendOutput($result->getData());
  140. } else {
  141. parent::sendOutput($result->getData(), $result->getErrorCode());
  142. }
  143. }
  144. public function department()
  145. {
  146. $data = $this->get_downline();
  147. foreach ($data as $item)
  148. {
  149. if ($item['parent_id'] == 1){
  150. $department = $this->objMDepartment->getDepartmentInfo(['dept_id' => $item['dept_id']])->getData();
  151. if ($department){
  152. $this->objMDepartment->updateDepartment([
  153. 'shopId' => $this->shopId,
  154. 'pid' => 0,
  155. 'departmentName' => $item['name'],
  156. 'dept_id' => $item['dept_id'],
  157. 'updateTime' => time(),
  158. ], $department['id']);
  159. }else{
  160. $this->objMDepartment->addDepartment([
  161. 'shopId' => $this->shopId,
  162. 'pid' => 0,
  163. 'departmentName' => $item['name'],
  164. 'dept_id' => $item['dept_id'],
  165. 'updateTime' => time(),
  166. ]);
  167. }
  168. }
  169. }
  170. foreach ($data as $item)
  171. {
  172. if ($item['parent_id'] > 1){
  173. $department = $this->objMDepartment->getDepartmentInfo(['dept_id' => $item['dept_id']])->getData();
  174. $pid = $this->objMDepartment->getDepartmentInfo(['dept_id' => $item['parent_id']])->getData();
  175. if ($department){
  176. $this->objMDepartment->updateDepartment([
  177. 'shopId' => $this->shopId,
  178. 'pid' => $pid['id'],
  179. 'departmentName' => $item['name'],
  180. 'dept_id' => $item['dept_id'],
  181. 'updateTime' => time(),
  182. ], $department['id']);
  183. }else{
  184. $this->objMDepartment->addDepartment([
  185. 'shopId' => $this->shopId,
  186. 'pid' => $pid['id'],
  187. 'departmentName' => $item['name'],
  188. 'dept_id' => $item['dept_id'],
  189. 'updateTime' => time(),
  190. ]);
  191. }
  192. }
  193. }
  194. parent::sendOutput('同步成功');
  195. }
  196. public function get_downline($dept_id= 1){
  197. $url = 'https://oapi.dingtalk.com/topapi/v2/department/listsub';
  198. $data = $this->curl_get($url.'?access_token='.$this->voucher().'&dept_id='.$dept_id);
  199. $data = json_decode($data)->result;// 部门
  200. $arr=array();
  201. foreach ($data as $key => $v) {
  202. if($v->parent_id == $dept_id){ //pid为0的是顶级分类
  203. $arr[]=[
  204. 'auto_add_user' => $v->auto_add_user,
  205. 'create_dept_group' => $v->create_dept_group,
  206. 'dept_id' => $v->dept_id,
  207. 'name' => $v->name,
  208. 'parent_id' => $v->parent_id,
  209. ];
  210. $arr = array_merge($arr,$this->get_downline($v->dept_id));
  211. }
  212. }
  213. return $arr;
  214. }
  215. }