FyySqlServerService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Orders;
  4. use App\Model\SaleOrdersProduct;
  5. use Illuminate\Support\Facades\Config;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Redis;
  8. class FyySqlServerService extends Service
  9. {
  10. public $db = null;
  11. public $error = null;
  12. public function __construct()
  13. {
  14. $fyy_array = Redis::get('fyy_sql_server');
  15. if(empty($fyy_array)) {
  16. $this->error = 'SQLSERVER设置不存在!';
  17. return;
  18. }
  19. $fyy_array = json_decode($fyy_array,true);
  20. if(! $this->db){
  21. $config = [
  22. 'driver' => 'sqlsrv',
  23. 'host' => $fyy_array['sqlserver_host'] ?? '',
  24. 'port' => $fyy_array['sqlserver_port'] ?? '',
  25. 'database' => $fyy_array['sqlserver_database'] ?? '',
  26. 'username' => env('SQLSRV_USERNAME'),
  27. 'password' => env('SQLSRV_PASSWORD'),
  28. ];
  29. // 进行数据库连接
  30. Config::set('database.connections.sqlsrvs', $config);
  31. try {
  32. $pdo = DB::connection('sqlsrvs')->getPdo();
  33. if ($pdo instanceof \PDO) {
  34. // 连接成功的逻辑代码
  35. $this->db = DB::connection('sqlsrvs');
  36. } else {
  37. $this->error = '连接失败!';
  38. return;
  39. }
  40. } catch (\Throwable $e) {
  41. $this->error = $e->getMessage();
  42. }
  43. }
  44. }
  45. private function fakeData(){
  46. $return = [
  47. [
  48. 'out_order_no' => '001',
  49. 'out_order_no_time' => 1685928995,
  50. 'customer_no' => '201603012',
  51. 'customer_name' => '巴大胡',
  52. 'table_header_mark' => '表头备注',
  53. 'product_no' => '产品编码001',
  54. 'product_title' => '产品名称001',
  55. 'product_size' => '产品规格30c',
  56. 'product_unit' => '产品单位米',
  57. 'order_quantity' => 100,
  58. 'product_quantity_on_hand' => 50,
  59. 'technology_material' => '工艺材质',
  60. 'technology_name' => '工艺名称:同命',
  61. 'wood_name' => '木皮名称:梭梭树皮',
  62. 'process_mark' => '加工备注',
  63. 'table_body_mark' => '表体备注',
  64. 'out_crt_man' => '外部人员一',
  65. 'out_checker_man' => '外部人员二',
  66. 'out_checker_time' => 1685928995,
  67. ],
  68. [
  69. 'out_order_no' => '001',
  70. 'out_order_no_time' => 1685928995,
  71. 'customer_no' => '201603012',
  72. 'customer_name' => '巴大胡',
  73. 'table_header_mark' => '表头备注',
  74. 'product_no' => '产品编码002',
  75. 'product_title' => '产品名称002',
  76. 'product_size' => '产品规格30d',
  77. 'product_unit' => '产品单位米',
  78. 'order_quantity' => 100,
  79. 'product_quantity_on_hand' => 50,
  80. 'technology_material' => '工艺材质',
  81. 'technology_name' => '工艺名称:同命',
  82. 'wood_name' => '木皮名称:梭梭树皮',
  83. 'process_mark' => '加工备注',
  84. 'table_body_mark' => '表体备注',
  85. 'out_crt_man' => '外部人员一',
  86. 'out_checker_man' => '外部人员二',
  87. 'out_checker_time' => 1685928995,
  88. ],
  89. [
  90. 'out_order_no' => '002',
  91. 'out_order_no_time' => 1685928995,
  92. 'customer_no' => '201603012',
  93. 'customer_name' => '巴大胡',
  94. 'table_header_mark' => '表头备注',
  95. 'product_no' => '产品编码002',
  96. 'product_title' => '产品名称002',
  97. 'product_size' => '产品规格30d',
  98. 'product_unit' => '产品单位米',
  99. 'order_quantity' => 100,
  100. 'product_quantity_on_hand' => 50,
  101. 'technology_material' => '工艺材质',
  102. 'technology_name' => '工艺名称:同命',
  103. 'wood_name' => '木皮名称:梭梭树皮',
  104. 'process_mark' => '加工备注',
  105. 'table_body_mark' => '表体备注',
  106. 'out_crt_man' => '外部人员一',
  107. 'out_checker_man' => '外部人员二',
  108. 'out_checker_time' => 1685928995,
  109. ],
  110. ];
  111. return $return;
  112. }
  113. public function is_same_month($timestamp1,$timestamp2){
  114. date_default_timezone_set("PRC");
  115. // 格式化时间戳为年份和月份
  116. $year1 = date('Y', $timestamp1);
  117. $month1 = date('m', $timestamp1);
  118. $year2 = date('Y', $timestamp2);
  119. $month2 = date('m', $timestamp2);
  120. if ($year1 === $year2 && $month1 === $month2) {
  121. return true;
  122. } else {
  123. return false;
  124. }
  125. }
  126. //获取数据(点击引入)
  127. public function getDataFromSqlServer($data){
  128. if(! empty($this->error)) return [false,$this->error,''];
  129. if(empty($data['out_order_no_time'][0]) || empty($data['out_order_no_time'][1])) return [false,'制单日期不能为空!',''];
  130. $bool = $this->is_same_month($data['out_order_no_time'][0],$data['out_order_no_time'][1]);
  131. if(! $bool) return [false,'制单日期必须同月!',''];
  132. //查询产品主表副表数据
  133. date_default_timezone_set("PRC");
  134. $start = date('Y-m-d H:i:s.000',$data['out_order_no_time'][0]);
  135. $end = date('Y-m-d H:i:s.000',$data['out_order_no_time'][1]);
  136. $model = $this->db->table('SO_SOMain as a')
  137. ->leftJoin('SO_SODetails as b','b.cSOCode','a.cSOCode')
  138. ->whereBetween('a.dDate',[$start, $end])
  139. ->whereNotNull('a.cVerifier')
  140. ->select('a.cSOCode as out_order_no','a.dDate as out_order_no_time','a.cCusCode as customer_no','a.cCusName as customer_name','a.cMemo as table_header_mark','a.cMaker as out_crt_man','a.cVerifier as out_checker_man','a.dverifydate as out_checker_time','b.cInvCode as product_no','b.iQuantity as order_quantity','b.cDefine28 as technology_material','b.cFree1 as technology_name','b.cFree2 as wood_name','b.cDefine30 as process_mark','b.cMemo as table_body_mark');
  141. if(! empty($data['out_order_no'])) $model->where('a.cSOCode', 'LIKE', '%'.$data['out_order_no'].'%');
  142. if(! empty($data['customer_no'])) $model->where('a.cCusCode', 'LIKE', '%'.$data['customer_no'].'%');
  143. if(! empty($data['customer_name'])) $model->where('a.cCusName', 'LIKE', '%'.$data['customer_name'].'%');
  144. if(! empty($data['table_header_mark'])) $model->where('a.cMemo', 'LIKE', '%'.$data['table_header_mark'].'%');
  145. if(! empty($data['out_crt_man'])) $model->where('a.cMaker', 'LIKE', '%'.$data['out_crt_man'].'%');
  146. if(! empty($data['out_checker_man'])) $model->where('a.cVerifier', 'LIKE', '%'.$data['out_checker_man'].'%');
  147. if(! empty($data['out_checker_time'][0]) && ! empty($data['out_checker_time'][1])) {
  148. $start1 = date('Y-m-d H:i:s.000',$data['out_checker_time'][0]);
  149. $end1 = date('Y-m-d H:i:s.000',$data['out_checker_time'][1]);
  150. $model->whereBetween('a.dverifydate',[$start1, $end1]);
  151. }
  152. $result = $model->get()->toArray();
  153. if(empty($result)) return [false,'暂无数据,更新结束!',''];
  154. list($status,$msg) = $this->orderRule($result);
  155. if(! $status) return [false,$msg,''];
  156. //查询附带的一些信息(比较少)
  157. $product_no = array_column($result,'product_no');
  158. $messageOne = $this->db->table('Inventory as a')
  159. ->join('ComputationUnit as b',function($join){
  160. $join->on('a.cGroupCode','=','b.cGroupCode')
  161. ->on('a.cComUnitCode','=','b.cComUnitCode');
  162. }, null,null,'left')
  163. ->whereIn('a.cInvCode',$product_no)
  164. ->select('a.cInvCode as product_no','a.cInvName as product_title','a.cInvStd as product_size','b.cComUnitName as product_unit')
  165. ->get()->toArray();
  166. $messageMap = array_column($messageOne,null,'product_no');
  167. //现存量查询开始 ---组织查询条件
  168. $args = '';
  169. foreach ($result as $value){
  170. $product = $value->product_no;
  171. $technology_name = $value->technology_name ?? '';
  172. $wood_name = $value->wood_name ?? '';
  173. $args .= "(a.cInvCode = '{$product}' and a.cFree1 = '{$technology_name}' and a.cFree2 = '{$wood_name}') OR ";
  174. }
  175. $args = rtrim($args,'OR ');
  176. $messageTwo = $this->db->table('CurrentStock as a')
  177. ->leftJoin('Warehouse as b','b.cWhCode','a.cWhCode')
  178. ->whereRaw("($args)")
  179. ->where('a.iQuantity','>',0)
  180. ->select('a.iQuantity as product_quantity_on_hand','a.cInvCode as product_no','a.cFree1 as technology_name','a.cFree2 as wood_name','b.cWhName as warehouse_name')
  181. ->get()->toArray();
  182. if(! empty($messageTwo)){
  183. foreach ($messageTwo as $key => $value){
  184. $messageTwo[$key] = (array)$value;
  185. }
  186. }
  187. //现存量查询结束
  188. foreach ($result as $key => $value){
  189. $result[$key]->technology_material = $value->technology_material ?? '';
  190. $result[$key]->technology_name = $value->technology_name ?? '';
  191. $result[$key]->wood_name = $value->wood_name ?? '';
  192. $result[$key]->process_mark = $value->process_mark ?? '';
  193. $result[$key]->table_body_mark = $value->table_body_mark ?? '';
  194. $result[$key]->table_header_mark = $value->table_header_mark ?? '';
  195. $keys = $value->product_no . $value->technology_name . $value->wood_name;
  196. $result[$key]->out_order_no_time = $value->out_order_no_time ? strtotime($value->out_order_no_time) : 0;
  197. $result[$key]->out_checker_time = $value->out_checker_time ? strtotime($value->out_checker_time) : 0;
  198. $result[$key]->product_title = $messageMap[$value->product_no]->product_title ?? '';
  199. $result[$key]->product_size = $messageMap[$value->product_no]->product_size ?? '';
  200. $result[$key]->product_unit = $messageMap[$value->product_no]->product_unit ?? '';
  201. $result[$key] = (array)$value;
  202. }
  203. return [true,$result,$messageTwo];
  204. }
  205. public function orderRule($data){
  206. $result = Orders::where('del_time',0)
  207. ->whereIn('out_order_no',array_column($data,'out_order_no'))
  208. ->select('out_order_no')
  209. ->get()->toArray();
  210. if(! empty($result)) return [false,'查询区间内销售订单号已存在'];
  211. return [true,''];
  212. }
  213. //获取数据(刷新现存量)
  214. public function getDataFromSqlServerForOnHand($data){
  215. if(! empty($this->error)) return [false,$this->error,''];
  216. if(empty($data['id'])) return [false,'数据不能为空!',''];
  217. $product = SaleOrdersProduct::whereIn('id',$data['id'])
  218. ->select('product_no','technology_name','wood_name')
  219. ->get()->toArray();
  220. //现存量查询开始 ---组织查询条件
  221. $args = '';
  222. foreach ($product as $value){
  223. $args .= "(a.cInvCode = '{$value['product_no']}' and a.cFree1 = '{$value['technology_name']}' and a.cFree2 = '{$value['wood_name']}') OR ";
  224. }
  225. $args = rtrim($args,'OR ');
  226. $message = $this->db->table('CurrentStock as a')
  227. ->leftJoin('Warehouse as b','b.cWhCode','a.cWhCode')
  228. ->whereRaw("($args)")
  229. ->where('a.iQuantity','>',0)
  230. ->select('a.iQuantity as product_quantity_on_hand','a.cInvCode as product_no','a.cFree1 as technology_name','a.cFree2 as wood_name','b.cWhName as warehouse_name')
  231. ->get()->toArray();
  232. if(! empty($message)){
  233. foreach ($message as $key => $value){
  234. $message[$key] = (array)$value;
  235. }
  236. }
  237. //现存量查询结束
  238. return [true,$message,$product];
  239. }
  240. }