OrganizationService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Employee;
  4. use App\Model\Organization;
  5. use App\Model\OrganizationDetails;
  6. use Illuminate\Support\Facades\DB;
  7. class OrganizationService extends Service
  8. {
  9. public function organizationEdit($data,$user){
  10. list($status,$msg) = $this->organizationRule($data, $user, false);
  11. if(!$status) return [$status,$msg];
  12. try {
  13. DB::beginTransaction();
  14. $model = Organization::where('id',$data['id'])->first();
  15. $model->title = $data['title'] ?? '';
  16. $model->industry_name = $data['industry_name'] ?? "";
  17. $model->industry_ranking = $data['industry_ranking'] ?? "";
  18. $model->address = $data['address'] ?? "";
  19. $model->founding_time = $data['founding_time'] ?? 0;
  20. $model->type = $data['type'] ?? 0;
  21. $model->decision_depart = $data['decision_depart'] ?? "";
  22. $model->decision_maker = $data['decision_maker'] ?? "";
  23. $model->new_product_rule = $data['new_product_rule'] ?? "";
  24. $model->old_product_rule = $data['old_product_rule'] ?? "";
  25. $model->save();
  26. $time = time();
  27. OrganizationDetails::where('del_time',0)
  28. ->where('organization_id', $model->id)
  29. ->update(['del_time' => $time]);
  30. $this->saveDetail($model->id, $time, $data);
  31. DB::commit();
  32. }catch (\Exception $exception){
  33. DB::rollBack();
  34. return [false,$exception->getMessage()];
  35. }
  36. return [true, ''];
  37. }
  38. public function organizationAdd($data,$user){
  39. list($status,$msg) = $this->organizationRule($data, $user);
  40. if(!$status) return [$status,$msg];
  41. try {
  42. DB::beginTransaction();
  43. $model = new Organization();
  44. $model->code = $this->generateCode($user['id']);
  45. $model->title = $data['title'] ?? '';
  46. $model->industry_name = $data['industry_name'] ?? "";
  47. $model->industry_ranking = $data['industry_ranking'] ?? "";
  48. $model->address = $data['address'] ?? "";
  49. $model->founding_time = $data['founding_time'] ?? 0;
  50. $model->type = $data['type'] ?? 0;
  51. $model->decision_depart = $data['decision_depart'] ?? "";
  52. $model->decision_maker = $data['decision_maker'] ?? "";
  53. $model->new_product_rule = $data['new_product_rule'] ?? "";
  54. $model->old_product_rule = $data['old_product_rule'] ?? "";
  55. $model->crt_id = $user['id'];
  56. $model->save();
  57. $this->saveDetail($model->id, time(), $data);
  58. $id = $model->id;
  59. DB::commit();
  60. }catch (\Exception $exception){
  61. DB::rollBack();
  62. if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
  63. return [false, '网络波动,请重新操作!'];
  64. }
  65. return [false,$exception->getMessage()];
  66. }
  67. return [true, ['id' => $id]];
  68. }
  69. private function saveDetail($id, $time, $data){
  70. if(! empty($data['unit_list'])){
  71. $unit = [];
  72. foreach ($data['unit_list'] as $value){
  73. if(empty($value['type'])) continue;
  74. if(empty($value['unit'])) continue;
  75. $unit[] = [
  76. 'organization_id' => $id,
  77. 'type' => $value['type'],
  78. 'unit' => $value['unit'],
  79. 'crt_time' => $time,
  80. ];
  81. }
  82. if(! empty($unit)) OrganizationDetails::insert($unit);
  83. }
  84. if(! empty($data['receipt_list'])){
  85. $receipt = [];
  86. foreach ($data['receipt_list'] as $value){
  87. if(empty($value['type'])) continue;
  88. if(empty($value['receipt'])) continue;
  89. $receipt[] = [
  90. 'organization_id' => $id,
  91. 'type' => $value['type'],
  92. 'year' => $value['year'],
  93. 'receipt' => $value['receipt'] ?? '',
  94. 'crt_time' => $time,
  95. ];
  96. }
  97. if(! empty($receipt)) OrganizationDetails::insert($receipt);
  98. }
  99. if(! empty($data['requirement_list'])){
  100. $requirement = [];
  101. foreach ($data['requirement_list'] as $value){
  102. if(empty($value['type'])) continue;
  103. if(empty($value['requirement'])) continue;
  104. $requirement[] = [
  105. 'organization_id' => $id,
  106. 'type' => $value['type'],
  107. 'requirement' => $value['requirement'],
  108. 'month_usage' => $value['month_usage'] ?? '',
  109. 'reference_value' => $value['reference_value'] ?? '',
  110. 'crt_time' => $time,
  111. ];
  112. }
  113. if(! empty($requirement)) OrganizationDetails::insert($requirement);
  114. }
  115. }
  116. private function getDetail($id){
  117. $data = OrganizationDetails::where('del_time',0)
  118. ->where('organization_id', $id)
  119. ->get()->toArray();
  120. $unit = $receipt = $requirement = [];
  121. foreach ($data as $value){
  122. if(in_array($value['type'], [OrganizationDetails::type_one, OrganizationDetails::type_two])) {
  123. $unit[] = [
  124. 'type' => $value['type'],
  125. 'unit' => $value['unit'],
  126. ];
  127. }elseif(in_array($value['type'], [OrganizationDetails::type_three])){
  128. $receipt[] = [
  129. 'type' => $value['type'],
  130. 'year' => $value['year'],
  131. 'receipt' => $value['receipt'],
  132. ];
  133. }else{
  134. $requirement[] = [
  135. 'type' => $value['type'],
  136. 'requirement' => $value['requirement'],
  137. 'month_usage' => $value['month_usage'],
  138. 'reference_value' => $value['reference_value'],
  139. ];
  140. }
  141. }
  142. $detail = [
  143. 'unit_list' => $unit,
  144. 'receipt_list' => $receipt,
  145. 'requirement_list' => $requirement,
  146. ];
  147. foreach ($detail as $key => $value) {
  148. if (empty($value)) {
  149. $detail[$key] = (object)[]; // 转成 stdClass 对象
  150. }
  151. }
  152. return $detail;
  153. }
  154. public function organizationDel($data){
  155. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  156. try {
  157. DB::beginTransaction();
  158. $time = time();
  159. Organization::where('del_time',0)
  160. ->whereIn('id',$data['id'])
  161. ->update(['del_time' => $time]);
  162. OrganizationDetails::where('del_time',0)
  163. ->where('organization_id', $data['id'])
  164. ->update(['del_time' => $time]);
  165. DB::commit();
  166. }catch (\Exception $exception){
  167. DB::rollBack();
  168. return [false,$exception->getMessage()];
  169. }
  170. return [true, ''];
  171. }
  172. public function organizationDetail($data, $user){
  173. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  174. $customer = Organization::where('del_time',0)
  175. ->where('id',$data['id'])
  176. ->first();
  177. if(empty($customer)) return [false,'组织不存在或已被删除'];
  178. $customer = $customer->toArray();
  179. $customer['founding_time'] = ! empty($customer['founding_time']) ? date("Y-m-d", $customer['founding_time']) : "";
  180. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  181. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  182. $details = $this->getDetail($data['id']);
  183. $customer = array_merge($customer, $details);
  184. return [true, $customer];
  185. }
  186. public function organizationCommon($data,$user, $field = []){
  187. if(empty($field)) $field = Organization::$field;
  188. $model = Organization::Clear($user,$data);
  189. $model = $model->where('del_time',0)
  190. ->select($field)
  191. ->orderby('id', 'desc');
  192. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  193. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  194. if(! empty($data['type'])) $model->where('type', $data['type']);
  195. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  196. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  197. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  198. $model->where('crt_time','>=',$return[0]);
  199. $model->where('crt_time','<=',$return[1]);
  200. }
  201. return $model;
  202. }
  203. public function organizationListForSearch($data,$user){
  204. $model = $this->organizationCommon($data, $user,['id']);
  205. $id = $model->get()->toArray();
  206. $id = array_column($id,'id');
  207. return [true, $id];
  208. }
  209. public function organizationList($data,$user){
  210. $model = $this->organizationCommon($data, $user);
  211. $list = $this->limit($model,'',$data);
  212. $list = $this->fillData($list,$user,$data);
  213. return [true, $list];
  214. }
  215. public function organizationRule(&$data, $user, $is_add = true){
  216. if(empty($data['title'])) return [false, '名称不能为空'];
  217. if(empty($data['type']) || ! isset(Organization::$type_name[$data['type']])) return [false, '组织类型错误'];
  218. if(! empty($data['founding_time'])) $data['founding_time'] = $this->changeDateToDate($data['founding_time']);
  219. // if(! empty($data['unit_list'])){
  220. // foreach ($data['unit_list'] as $value){
  221. // if(empty($value['type'])) return [false, '上下级单位类型不能为空'];
  222. // if(empty($value['unit'])) return [false, '上下级单位名称不能为空'];
  223. // }
  224. // }
  225. // if(! empty($data['receipt_list'])){
  226. // foreach ($data['receipt_list'] as $value){
  227. // if(empty($value['type'])) return [false, '营收信息类型不能为空'];
  228. // if(empty($value['year'])) return [false, '营收年份不能为空'];
  229. //// $start_time = strtotime($value['start_time'] . "-01");
  230. //// $end_time = strtotime($value['end_time'] . "-01");
  231. //// if(! $start_time) return [false, '营收日期范围错误'];
  232. // if(empty($value['receipt'])) return [false, '营收数据不能为空'];
  233. // }
  234. // }
  235. // if(! empty($data['requirement_list'])){
  236. // foreach ($data['requirement_list'] as $value){
  237. // if(empty($value['type'])) return [false, '需求信息类型不能为空'];
  238. // if(empty($value['requirement'])) return [false, '需求信息名不能为空'];
  239. // if(empty($value['month_usage'])) return [false, '月用量不能为空'];
  240. // if(empty($value['reference_value'])) return [false, '参考价不能为空'];
  241. // }
  242. // }
  243. if($is_add){
  244. }else{
  245. if(empty($data['id'])) return [false,'ID不能为空'];
  246. $bool = Organization::where('id', $data['id'])
  247. ->where('del_time',0)
  248. ->exists();
  249. if(! $bool) return [false, '组织不存在或已被删除'];
  250. }
  251. return [true, $data];
  252. }
  253. public function fillData($data, $user, $search){
  254. if(empty($data['data'])) return $data;
  255. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  256. foreach ($data['data'] as $key => $value){
  257. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  258. $data['data'][$key]['founding_time'] = $value['founding_time'] ? date('Y-m-d',$value['founding_time']) : '';
  259. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  260. $data['data'][$key]['type_title'] = Organization::$type_name[$value['type']] ?? '';
  261. }
  262. return $data;
  263. }
  264. public function generateCode($crt_id)
  265. {
  266. return DB::transaction(function () use ($crt_id) {
  267. // 加行级锁 FOR UPDATE,避免并发重复
  268. $maxCode = Organization::where('crt_id', $crt_id)
  269. ->lockForUpdate()
  270. ->max('code');
  271. // 转数字
  272. $num = intval($maxCode);
  273. // +1
  274. $num++;
  275. // 小于 10000 → 保留 4 位前导零
  276. if ($num < 10000) {
  277. $code = str_pad($num, 4, '0', STR_PAD_LEFT);
  278. } else {
  279. $code = (string)$num; // 大于等于10000则直接用数字
  280. }
  281. return $code;
  282. });
  283. }
  284. public function getMap($id){
  285. if(empty($id)) return [];
  286. if(! is_array($id)) $id = [$id];
  287. return Organization::whereIn('id', $id)
  288. ->pluck('title', 'id')
  289. ->all();
  290. }
  291. }