OrganizationService.php 13 KB

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