Index.php 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use setasign\Fpdi\PdfReader\PdfReader;
  5. use think\Controller;
  6. use think\Db;
  7. use think\Request;
  8. /**
  9. * 达成大屏数据接口
  10. */
  11. class Index extends Controller{
  12. protected $noNeedLogin = ['*'];
  13. protected $noNeedRight = ['*'];
  14. /**首页*/
  15. public function index(){$this->success('成功');}
  16. /**第一屏*/
  17. //年度承接产量
  18. public function thisyear(){
  19. $date = date("Y");
  20. // 返回 JSON 格式数据
  21. return json([
  22. "status" => 0,
  23. "msg" => "",
  24. "data" => $date.'年度承接产量',
  25. ]);
  26. }
  27. public function lastyear(){
  28. $date = date("Y") - 1;
  29. // 返回 JSON 格式数据
  30. return json([
  31. "status" => 0,
  32. "msg" => "",
  33. "data" => $date.'年度承接产量',
  34. ]);
  35. }
  36. //获取近三年年份
  37. public function ymd_api(){
  38. $data = [
  39. ["name" => " ", "value" => strval(date('Y'))], // 将年份转换为字符串
  40. ["name" => " ", "value" => strval(date('Y') - 1)], // 将年份转换为字符串
  41. ["name" => " ", "value" => strval(date('Y') - 2)], // 将年份转换为字符串
  42. ];
  43. // 返回 JSON 格式数据
  44. return json([
  45. "status" => 0,
  46. "msg" => "",
  47. "data" => $data,
  48. ]);
  49. }
  50. /**
  51. *去年的年度承接产量【接口】
  52. *统计去年订单个数,订单数量,包装完工数量,完成率。
  53. */
  54. public function Lastcontractedoutput_api(){
  55. // 获取今年的年份
  56. $last_year = date("Y") -1;
  57. // 查询条件:Mod_rq为null且年份为今年的记录数量
  58. $count = \db('工单_基本资料')
  59. ->where('Sys_rq', 'between', ["$last_year-01-01 00:00:00", "$last_year-12-31 23:59:59"])
  60. ->whereNull('Mod_rq') // 修正:使用 whereNull 方法
  61. ->count();
  62. // 查询条件:Mod_rq为null且年份为今年的订单数量字段的总和
  63. $sum = \db('工单_基本资料')
  64. ->field("sum(`订单数量`) as total_sum") // 使用反引号包裹中文字段名,并设置别名
  65. ->where('Sys_rq', 'between', ["$last_year-01-01 00:00:00", "$last_year-12-31 23:59:59"])
  66. ->whereNull('Mod_rq')
  67. ->select();
  68. $total_sum = 0;
  69. $s_num_total = 0;
  70. $ratio = 0;
  71. if (!empty($sum)) {
  72. foreach ($sum as $value) {
  73. $total_sum = $value['total_sum']; // 使用别名获取结果
  74. if ($total_sum) {
  75. // 查询设备_产量计酬表中工序编号为7且mod_rq为null且年份为今年的s_num总和
  76. $s_num_result = \db('设备_产量计酬')
  77. ->field("sum(`s_num`) as s_num_total") // 计算 s_num 的总和
  78. ->where('工序编号', 7)
  79. ->whereNull('mod_rq')
  80. ->where('sys_rq', 'between', ["$last_year-01-01 00:00:00", "$last_year-12-31 23:59:59"])
  81. ->select();
  82. if (!empty($s_num_result)) {
  83. $s_num_total = $s_num_result[0]['s_num_total']; // 获取 s_num 的总和
  84. // 计算比例并四舍五入保留两位小数
  85. if ($total_sum != 0) { // 避免除数为0
  86. $ratio = round(($s_num_total / $total_sum) * 100) . '%';
  87. }
  88. }
  89. }
  90. }
  91. }
  92. // 构建返回数据
  93. // $data = [
  94. // ["name" => "总订单数", "value" => $count],
  95. // ["name" => "承接大货数", "value" => $total_sum],
  96. // ["name" => "完工数量", "value" => $s_num_total],
  97. // ["name" => "完成率", "value" => $ratio],
  98. // ];
  99. $data = [
  100. ["name" => " ", "value" => $count],
  101. ["name" => " ", "value" => $total_sum],
  102. ["name" => " ", "value" => $s_num_total],
  103. // ["name" => " ", "value" => $ratio],
  104. ];
  105. // 返回 JSON 格式数据
  106. return json([
  107. "status" => 0,
  108. "msg" => "",
  109. "data" => $data,
  110. ]);
  111. }
  112. /**
  113. *当年的年度承接产量【接口】
  114. *统计当年订单个数,订单数量,包装完工数量,完成率。
  115. */
  116. public function Annualcontractedoutput_api(){
  117. // 获取今年的年份
  118. $year = date("Y");
  119. // 查询条件:Mod_rq为null且年份为今年的记录数量
  120. $count = \db('工单_基本资料')
  121. ->where('Sys_rq', 'between', ["$year-01-01 00:00:00", "$year-12-31 23:59:59"])
  122. ->whereNull('Mod_rq') // 修正:使用 whereNull 方法
  123. ->count();
  124. // 查询条件:Mod_rq为null且年份为今年的订单数量字段的总和
  125. $sum = \db('工单_基本资料')
  126. ->field("sum(`订单数量`) as total_sum") // 使用反引号包裹中文字段名,并设置别名
  127. ->where('Sys_rq', 'between', ["$year-01-01 00:00:00", "$year-12-31 23:59:59"])
  128. ->whereNull('Mod_rq')
  129. ->select();
  130. $total_sum = 0;
  131. $s_num_total = 0;
  132. $ratio = 0;
  133. if (!empty($sum)) {
  134. foreach ($sum as $value) {
  135. $total_sum = $value['total_sum']; // 使用别名获取结果
  136. if ($total_sum) {
  137. // 查询设备_产量计酬表中工序编号为7且mod_rq为null且年份为今年的s_num总和
  138. $s_num_result = \db('设备_产量计酬')
  139. ->field("sum(`s_num`) as s_num_total") // 计算 s_num 的总和
  140. ->where('工序编号', 7)
  141. ->whereNull('mod_rq')
  142. ->where('sys_rq', 'between', ["$year-01-01 00:00:00", "$year-12-31 23:59:59"])
  143. ->select();
  144. if (!empty($s_num_result)) {
  145. $s_num_total = $s_num_result[0]['s_num_total']; // 获取 s_num 的总和
  146. // 计算比例并四舍五入保留两位小数
  147. if ($total_sum != 0) { // 避免除数为0
  148. $ratio = round(($s_num_total / $total_sum) * 100) . '%';
  149. }
  150. }
  151. }
  152. }
  153. }
  154. // 构建返回数据
  155. $data = [
  156. ["name" => " ", "value" => $count],
  157. ["name" => " ", "value" => $total_sum],
  158. ["name" => " ", "value" => $s_num_total],
  159. // ["name" => " ", "value" => $ratio],
  160. ];
  161. // 返回 JSON 格式数据
  162. return json([
  163. "status" => 0,
  164. "msg" => "",
  165. "data" => $data,
  166. ]);
  167. }
  168. public function dcompletion_api(){
  169. // 获取今年的年份
  170. $year = date("Y");
  171. // 查询条件:Mod_rq为null且年份为今年的记录数量
  172. $count = \db('工单_基本资料')
  173. ->where('Sys_rq', 'between', ["$year-01-01 00:00:00", "$year-12-31 23:59:59"])
  174. ->whereNull('Mod_rq') // 修正:使用 whereNull 方法
  175. ->count();
  176. // 查询条件:Mod_rq为null且年份为今年的订单数量字段的总和
  177. $sum = \db('工单_基本资料')
  178. ->field("sum(`订单数量`) as total_sum") // 使用反引号包裹中文字段名,并设置别名
  179. ->where('Sys_rq', 'between', ["$year-01-01 00:00:00", "$year-12-31 23:59:59"])
  180. ->whereNull('Mod_rq')
  181. ->select();
  182. $total_sum = 0;
  183. $s_num_total = 0;
  184. $ratio = 0;
  185. if (!empty($sum)) {
  186. foreach ($sum as $value) {
  187. $total_sum = $value['total_sum']; // 使用别名获取结果
  188. if ($total_sum) {
  189. // 查询设备_产量计酬表中工序编号为7且mod_rq为null且年份为今年的s_num总和
  190. $s_num_result = \db('设备_产量计酬')
  191. ->field("sum(`s_num`) as s_num_total") // 计算 s_num 的总和
  192. ->where('工序编号', 7)
  193. ->whereNull('mod_rq')
  194. ->where('sys_rq', 'between', ["$year-01-01 00:00:00", "$year-12-31 23:59:59"])
  195. ->select();
  196. if (!empty($s_num_result)) {
  197. $s_num_total = $s_num_result[0]['s_num_total']; // 获取 s_num 的总和
  198. // 计算比例并四舍五入取整数
  199. if ($total_sum != 0) { // 避免除数为0
  200. $ratio = ($s_num_total / $total_sum) * 100; // 四舍五入并取整数
  201. }
  202. }
  203. }
  204. }
  205. }
  206. // 返回 JSON 格式数据
  207. return json([
  208. "status" => 0,
  209. "hitSugarSelf" => true,
  210. "msg" => "",
  211. "data" => round($ratio), // 返回计算后的整数完成率
  212. ]);
  213. }
  214. public function qcompletion_api(){
  215. // 获取今年的年份
  216. $last_year = date("Y") - 1;
  217. // 查询条件:Mod_rq为null且年份为去年的记录数量
  218. $count = \db('工单_基本资料')
  219. ->where('Sys_rq', 'between', ["$last_year-01-01 00:00:00", "$last_year-12-31 23:59:59"])
  220. ->whereNull('Mod_rq') // 修正:使用 whereNull 方法
  221. ->count();
  222. // 查询条件:Mod_rq为null且年份为去年的订单数量字段的总和
  223. $sum = \db('工单_基本资料')
  224. ->field("sum(`订单数量`) as total_sum") // 使用反引号包裹中文字段名,并设置别名
  225. ->where('Sys_rq', 'between', ["$last_year-01-01 00:00:00", "$last_year-12-31 23:59:59"])
  226. ->whereNull('Mod_rq')
  227. ->select();
  228. $total_sum = 0;
  229. $s_num_total = 0;
  230. $ratio = 0;
  231. if (!empty($sum)) {
  232. foreach ($sum as $value) {
  233. $total_sum = $value['total_sum']; // 使用别名获取结果
  234. if ($total_sum) {
  235. // 查询设备_产量计酬表中工序编号为7且mod_rq为null且年份为去年的s_num总和
  236. $s_num_result = \db('设备_产量计酬')
  237. ->field("sum(`s_num`) as s_num_total") // 计算 s_num 的总和
  238. ->where('工序编号', 7)
  239. ->whereNull('mod_rq')
  240. ->where('sys_rq', 'between', ["$last_year-01-01 00:00:00", "$last_year-12-31 23:59:59"])
  241. ->select();
  242. if (!empty($s_num_result)) {
  243. $s_num_total = $s_num_result[0]['s_num_total']; // 获取 s_num 的总和
  244. // 计算比例并四舍五入保留两位小数
  245. if ($total_sum != 0) { // 避免除数为0
  246. $ratio = round(($s_num_total / $total_sum) * 100, 2); // 计算并保留两位小数
  247. }
  248. }
  249. }
  250. }
  251. }
  252. // 返回 JSON 格式数据
  253. return json([
  254. "status" => 0,
  255. "hitSugarSelf" => true,
  256. "msg" => "",
  257. "data" => $ratio,
  258. ]);
  259. }
  260. /**
  261. * 年度订单个数【接口】
  262. * 统计近三年的订单条数
  263. */
  264. public function orderlist_api(){
  265. // 获取当前日期的年份
  266. $currentYear = date('Y');
  267. // 获取三年前的年份
  268. $threeYearsAgo = $currentYear - 2;
  269. // 构建查询的年份范围
  270. $years = [$threeYearsAgo, $threeYearsAgo + 1, $currentYear];
  271. // 查询订单的年份和数量
  272. $orderData = db('工单_基本资料')
  273. ->field('YEAR(Sys_rq) AS year, COUNT(*) AS order_count')
  274. ->whereNull('Mod_rq')
  275. ->where('YEAR(Sys_rq)', 'between', [$threeYearsAgo, $currentYear])
  276. ->group('YEAR(Sys_rq)')
  277. ->select();
  278. // 将查询结果按年份分组存储
  279. $orderCountByYear = [];
  280. foreach ($orderData as $data) {
  281. $orderCountByYear[$data['year']] = $data['order_count'];
  282. }
  283. // 对于没有订单的年份,默认值为0
  284. foreach ($years as $year) {
  285. if (!isset($orderCountByYear[$year])) {
  286. $orderCountByYear[$year] = 0; // 如果该年没有数据,则设置为0
  287. }
  288. }
  289. // 准备 API 格式的响应数据
  290. $response = [
  291. 'status' => 0,
  292. 'msg' => '',
  293. 'data' => [
  294. 'categories' => $years, // 输出年份数组
  295. 'series' => [
  296. [
  297. 'name' => '年度订单个数',
  298. 'data' => array_map(function($year) use ($orderCountByYear) {
  299. return $orderCountByYear[$year];
  300. }, $years), // 根据年份返回对应的订单数量
  301. ]
  302. ]
  303. ]
  304. ];
  305. return json($response);
  306. }
  307. /**
  308. * 年度计划数量【接口】
  309. * 统计近三年的订单总数量合计
  310. */
  311. public function ordersumlist_api(){
  312. // 获取当前日期的年份
  313. $currentYear = date('Y');
  314. // 获取三年前的年份
  315. $threeYearsAgo = $currentYear - 2;
  316. // 构建查询的年份范围
  317. $years = [$threeYearsAgo, $threeYearsAgo + 1, $currentYear];
  318. // 查询订单的年份和数量的合计
  319. $orderData = db('工单_基本资料')
  320. ->field('YEAR(Sys_rq) AS year, SUM(订单数量) AS total_quantity') // 这里改为求“数量”的合计
  321. ->whereNull('Mod_rq')
  322. ->where('YEAR(Sys_rq)', 'between', [$threeYearsAgo, $currentYear])
  323. ->group('YEAR(Sys_rq)')
  324. ->select();
  325. // 将查询结果按年份分组存储
  326. $orderSumByYear = [];
  327. foreach ($orderData as $data) {
  328. $orderSumByYear[$data['year']] = $data['total_quantity'];
  329. }
  330. // 对于没有订单的年份,默认值为0
  331. foreach ($years as $year) {
  332. if (!isset($orderSumByYear[$year])) {
  333. $orderSumByYear[$year] = 0; // 如果该年没有数据,则设置为0
  334. }
  335. }
  336. // 准备 API 格式的响应数据
  337. $response = [
  338. 'status' => 0,
  339. 'msg' => '',
  340. 'data' => [
  341. 'categories' => $years, // 输出年份数组
  342. 'series' => [
  343. [
  344. 'name' => '年度计划数量',
  345. 'data' => array_map(function($year) use ($orderSumByYear) {
  346. return $orderSumByYear[$year];
  347. }, $years), // 根据年份返回对应的订单数量合计
  348. ]
  349. ]
  350. ]
  351. ];
  352. return json($response);
  353. }
  354. /**
  355. * 年度完工数量【接口】
  356. * 统计每个月完成包装的数量
  357. */
  358. public function Annualcompletionrate_api() {
  359. // 获取当前年份和上一年份
  360. $currentYear = date('Y');
  361. $lastYear = $currentYear - 1;
  362. // 获取当前年份和去年的数据
  363. $orderData = db('设备_产量计酬')
  364. ->field("DATE_FORMAT(sys_rq, '%Y-%m') AS sys_rq, SUM(s_num) AS 数量")
  365. ->where('工序名称', '包装')
  366. ->where('mod_rq',null)
  367. ->group('DATE_FORMAT(sys_rq, "%Y-%m")') // 按年月分组
  368. ->select();
  369. // 手动过滤数据,只保留当前年和去年的数据
  370. $filteredData = array_filter($orderData, function ($data) use ($currentYear, $lastYear) {
  371. $year = explode('-', $data['sys_rq'])[0]; // 提取年份
  372. return $year == $currentYear || $year == $lastYear;
  373. });
  374. // 初始化月份和年份数组
  375. $months = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];
  376. $responseData = [
  377. 'categories' => $months, // 月份数组
  378. 'series' => [
  379. ['name' => $lastYear . '年', 'data' => array_fill(0, 12, 0)], // 初始化为0
  380. ['name' => $currentYear . '年', 'data' => array_fill(0, 12, 0)] // 初始化为0
  381. ]
  382. ];
  383. // 将查询结果按年份和月份存储
  384. foreach ($filteredData as $data) {
  385. // 提取年和月
  386. $yearMonth = explode('-', $data['sys_rq']);
  387. $year = $yearMonth[0];
  388. $month = (int) $yearMonth[1] - 1; // 因为数组从0开始,所以要减去1
  389. // 根据年份更新对应的月份数据
  390. if ($year == $lastYear) {
  391. $responseData['series'][0]['data'][$month] = $data['数量'];
  392. } elseif ($year == $currentYear) {
  393. $responseData['series'][1]['data'][$month] = $data['数量'];
  394. }
  395. }
  396. // 构造最终的响应数据
  397. $response = [
  398. 'status' => 0,
  399. 'msg' => '',
  400. 'data' => $responseData
  401. ];
  402. return json($response);
  403. }
  404. /**
  405. * 年度完成率【接口】
  406. */
  407. public function Annualcompletionraterate_api() {
  408. // 获取当前年份和上一年
  409. $currentYear = date('Y');
  410. $lastYear = $currentYear - 1;
  411. // 初始化月份数组
  412. $months = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];
  413. // 初始化返回数据
  414. $responseData = [
  415. 'categories' => $months, // 月份
  416. 'series' => [
  417. ['name' => $lastYear . '年完成率', 'data' => array_fill(0, 12, 0)], // 上一年完成率数据
  418. ['name' => $currentYear . '年完成率', 'data' => array_fill(0, 12, 0)], // 当前年完成率数据
  419. ]
  420. ];
  421. // 1. 获取工单基本资料表每月的订单数量(近两年)
  422. $orderData = db('工单_基本资料')
  423. ->field("DATE_FORMAT(出库日期, '%Y-%m') AS 订单日期, SUM(订单数量) AS 订单数量")
  424. ->where('Mod_rq', null) // 筛选 Mod_rq 为 null
  425. ->group('DATE_FORMAT(出库日期, "%Y-%m")') // 按年月分组
  426. ->select();
  427. // 2. 获取设备产量计酬表每月的包装数量(s_num)(近两年)
  428. $productionData = db('设备_产量计酬')
  429. ->field("DATE_FORMAT(sys_rq, '%Y-%m') AS 报工日期, SUM(s_num) AS 完工数量")
  430. ->where('工序名称', '包装') // 筛选工序名称为包装
  431. ->where('mod_rq', null) // 筛选 mod_rq 为 null
  432. ->group('DATE_FORMAT(sys_rq, "%Y-%m")') // 按年月分组
  433. ->select();
  434. // 将查询结果转换为数组,方便后续计算
  435. $orderDataMap = [];
  436. foreach ($orderData as $data) {
  437. $orderDataMap[$data['订单日期']] = $data['订单数量'];
  438. }
  439. $productionDataMap = [];
  440. foreach ($productionData as $data) {
  441. $productionDataMap[$data['报工日期']] = $data['完工数量'];
  442. }
  443. // 3. 计算每月的完成率并四舍五入
  444. foreach ($months as $month) {
  445. // 拼接当前月和上一年的年月
  446. $currentMonth = $currentYear . '-' . $month;
  447. $lastMonth = $lastYear . '-' . $month;
  448. // 获取当前月和上一年的订单数量和完工数量
  449. $totalOrdersCurrentYear = isset($orderDataMap[$currentMonth]) ? $orderDataMap[$currentMonth] : 0;
  450. $totalSNumCurrentYear = isset($productionDataMap[$currentMonth]) ? $productionDataMap[$currentMonth] : 0;
  451. $totalOrdersLastYear = isset($orderDataMap[$lastMonth]) ? $orderDataMap[$lastMonth] : 0;
  452. $totalSNumLastYear = isset($productionDataMap[$lastMonth]) ? $productionDataMap[$lastMonth] : 0;
  453. // 计算完成率,避免除以零,并四舍五入
  454. // $completionRateCurrentYear = $totalOrdersCurrentYear > 0 ? round(($totalSNumCurrentYear / $totalOrdersCurrentYear) * 100) : 0;
  455. // $completionRateLastYear = $totalOrdersLastYear > 0 ? round(($totalSNumLastYear / $totalOrdersLastYear) * 100) : 0;
  456. // 计算完成率,避免除以零,并限制最大值为 100%
  457. $completionRateCurrentYear = $totalOrdersCurrentYear > 0 ? min(round(($totalSNumCurrentYear / $totalOrdersCurrentYear) * 100), 100) : 0;
  458. $completionRateLastYear = $totalOrdersLastYear > 0 ? min(round(($totalSNumLastYear / $totalOrdersLastYear) * 100), 100) : 0;
  459. // 存储每个月的完成率
  460. $responseData['series'][0]['data'][$month - 1] = $completionRateLastYear;
  461. $responseData['series'][1]['data'][$month - 1] = $completionRateCurrentYear;
  462. }
  463. // 返回响应数据
  464. $response = [
  465. 'status' => 0,
  466. 'msg' => '',
  467. 'data' => $responseData
  468. ];
  469. return json($response);
  470. }
  471. /**
  472. * 年度客户订单数量汇总【接口】
  473. */
  474. public function getOrderSummaryKhStyle()
  475. {
  476. // 获取当前年份
  477. $last_year = date("Y");
  478. // 查询工单_基本资料表,按客户编号分组并计算订单数量的总和
  479. $result = \db('工单_基本资料')
  480. ->field('客户编号, SUM(订单数量) as 总订单数量') // 按客户编号分组并计算订单数量的总和
  481. ->where('Sys_rq', '>=', "$last_year-01-01 00:00:00") // 用 >= 和 <= 来代替 between,提高查询效率
  482. ->where('Sys_rq', '<=', "$last_year-12-31 23:59:59")
  483. ->whereNull('Mod_rq') // 确保 'Mod_rq' 为 null
  484. ->group('客户编号') // 按客户编号分组
  485. ->order('总订单数量 desc') // 按总订单数量降序排序
  486. ->cache(true, 60) // 添加缓存来减少数据库查询,60秒缓存一次
  487. ->select();
  488. // 如果没有数据,返回空结果
  489. if (empty($result)) {
  490. return json([
  491. 'status' => 0,
  492. 'msg' => '没有数据',
  493. 'data' => [
  494. 'columns' => [],
  495. 'rows' => []
  496. ]
  497. ]);
  498. }
  499. // 定义列格式
  500. $columns = [
  501. ['name' => '客户编号', 'id' => 'khbh', 'width' => '30', 'autoWrap' => "true", 'textAlign' => 'left'],
  502. ['name' => '总订单数量', 'id' => '总订单数量', 'width' => '30', 'autoWrap' => "true", 'textAlign' => 'left']
  503. ];
  504. // 格式化结果
  505. $rows = array_map(function ($item) {
  506. return [
  507. 'khbh' => $item['客户编号'],
  508. '总订单数量' => $item['总订单数量']
  509. ];
  510. }, $result);
  511. // 返回 JSON 格式的结果
  512. return json([
  513. 'status' => 0,
  514. 'msg' => '',
  515. 'data' => [
  516. 'columns' => $columns,
  517. 'rows' => $rows
  518. ]
  519. ]);
  520. }
  521. /**
  522. * 年度款式上下装数量汇总【接口】
  523. */
  524. public function jacket() {
  525. // 获取当前年份
  526. $last_year = date("Y");
  527. // 查询数据库,统计款式订单数量
  528. $row = \db('工单_基本资料')
  529. ->field('款式, COALESCE(SUM(订单数量), 0) as 总订单数量') // 避免 NULL 值
  530. ->where('Sys_rq', 'between', ["$last_year-01-01 00:00:00", "$last_year-12-31 23:59:59"])
  531. ->whereNull('Mod_rq')
  532. ->group('款式') // 按款式分组
  533. ->order('总订单数量 desc') // 订单数量降序排列
  534. ->limit(20) // 取前 20 个热销款式
  535. ->select();
  536. // 定义表格列
  537. $result['columns'] = [
  538. ['name' => '分类', 'id' => 'fl', 'width' => '20', 'textAlign' => 'left'],
  539. ['name' => '款式', 'id' => 'ks', 'width' => '35', 'textAlign' => 'left'],
  540. ['name' => '总订单数量', 'id' => 'zds', 'width' => '25', 'textAlign' => 'left'],
  541. ];
  542. // 如果查询结果为空,返回空数据
  543. if (empty($row)) {
  544. return json([
  545. 'status' => 0,
  546. 'msg' => '没有数据',
  547. 'data' => $result
  548. ]);
  549. }
  550. // 分类关键词
  551. $upperKeywords = ['衣', '装', '夹', '衫', '甲', '外套']; // 上装
  552. $lowerKeywords = ['裤', '裙']; // 下装
  553. // 处理查询结果,添加分类信息
  554. foreach ($row as $k => $v) {
  555. $style = $v['款式'];
  556. $isUpper = $isLower = false; // 是否属于上装/下装
  557. // 判断款式分类
  558. if (preg_match('/(' . implode('|', $upperKeywords) . ')/u', $style)) {
  559. $isUpper = true;
  560. }
  561. if (preg_match('/(' . implode('|', $lowerKeywords) . ')/u', $style)) {
  562. $isLower = true;
  563. }
  564. // 归类
  565. if (strpos($style, '+') !== false) {
  566. $category = '套装';
  567. } elseif ($isUpper && $isLower) {
  568. $category = '全身';
  569. } elseif ($isUpper) {
  570. $category = '上装';
  571. } elseif ($isLower) {
  572. $category = '下装';
  573. } else {
  574. $category = '其他';
  575. }
  576. // 组装 `rows` 数据
  577. $result['rows'][$k]['ks'] = $style;
  578. $result['rows'][$k]['zds'] = $v['总订单数量'];
  579. $result['rows'][$k]['fl'] = $category;
  580. }
  581. $res['status'] = 0;
  582. $res['msg'] = '';
  583. $res['data'] = $result;
  584. return json($res);
  585. }
  586. /**第二屏*/
  587. /**
  588. * 每月各个小组生产报工走势【接口】
  589. * 统计每个工序小组的报工数据
  590. */
  591. public function Monthlyoutput_api() {
  592. $req = $this->request->param();
  593. if (isset($req['jtbh']) && !empty($req['jtbh'])){
  594. $jtbh = $req['jtbh'];
  595. } else {
  596. $this->error('参数错误');
  597. }
  598. $currentYear = date('Y'); // 当前年份
  599. $lastYear = $currentYear - 1; // 去年年份
  600. $months = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];
  601. $wheres['sczl_jtbh'] = ['like', '%' . $jtbh . '%']; // 查询上个月的数据
  602. // 查询数据库,按年份、月份和 jtbh 分组,获取近两年的数据
  603. $queryResult = \db('设备_产量计酬')
  604. ->field('
  605. YEAR(sys_rq) as year,
  606. DATE_FORMAT(sys_rq, "%m") as month,
  607. SUM(
  608. CASE
  609. WHEN 工序名称 IN ("裁剪", "车缝") THEN 数量
  610. ELSE s_num
  611. END
  612. ) as 报工数量,
  613. 工序名称,
  614. sczl_jtbh as jtbh
  615. ')
  616. ->where($wheres)
  617. ->whereNull('mod_rq')
  618. ->where('sys_rq', 'between', ["$lastYear-01-01 00:00:00", "$currentYear-12-31 23:59:59"])
  619. ->group('YEAR(sys_rq), DATE_FORMAT(sys_rq, "%m"), sczl_jtbh')
  620. ->select();
  621. // 初始化结果数组,分开存储2024年和2025年的数据
  622. $result = [
  623. "categories" => $months, // 月份
  624. "series" => [
  625. ["name" => "{$lastYear}年", "data" => array_fill(0, 12, 0)], // 2024年数据,初始化为0
  626. ["name" => "{$currentYear}年", "data" => array_fill(0, 12, 0)] // 2025年数据,初始化为0
  627. ]
  628. ];
  629. // 按年份分组数据
  630. $dataByYear = [
  631. $lastYear => array_fill(0, 12, 0), // 2024年的每月数据,初始化为0
  632. $currentYear => array_fill(0, 12, 0) // 2025年的每月数据,初始化为0
  633. ];
  634. // 将查询结果填充到对应的年份和月份
  635. foreach ($queryResult as $row) {
  636. $year = $row['year'];
  637. $month = (int)$row['month'] - 1; // 月份从0开始
  638. $sNumTotal = $row['报工数量'];
  639. // 合并相同年份和月份的数据
  640. if (isset($dataByYear[$year])) {
  641. $dataByYear[$year][$month] += $sNumTotal; // 这里是累加相同年月的数据
  642. }
  643. }
  644. // 将数据填充到结果中的 series 中
  645. foreach ($dataByYear as $year => $monthData) {
  646. foreach ($result['series'] as &$series) {
  647. if ($series['name'] == "{$year}年") {
  648. $series['data'] = $monthData;
  649. break;
  650. }
  651. }
  652. }
  653. // 返回结果
  654. return json([
  655. "status" => 0,
  656. "msg" => "",
  657. "data" => $result,
  658. ]);
  659. }
  660. /**第三屏*/
  661. /**
  662. * 计划中
  663. * */
  664. public function plan_api(){
  665. // 获取今年的年份
  666. $last_year = date("Y");
  667. // 查询条件:Mod_rq为null且年份为今年的订单状态字段的数量
  668. $data = \db('工单_基本资料')
  669. ->field('gd_statu as 订单状态, COUNT(*) as 订单数量') // 统计每个订单状态的数量
  670. ->where('Sys_rq', 'between', ["$last_year-01-01 00:00:00", "$last_year-12-31 23:59:59"])
  671. ->whereNull('Mod_rq')
  672. ->group('gd_statu') // 按订单状态分组
  673. ->select();
  674. // 如果查询结果为空,设置一个默认值
  675. if (empty($data)) {
  676. $formattedData[] = [
  677. 'name' => ' ',
  678. 'value' => 0
  679. ];
  680. } else {
  681. // 否则根据查询结果格式化数据
  682. $formattedData[] = [
  683. 'name' => ' ',
  684. 'value' => isset($data[0]['订单数量']) ? $data[0]['订单数量'] : 0
  685. ];
  686. }
  687. // 返回 JSON 格式数据
  688. return json([
  689. "status" => 0,
  690. "msg" => "",
  691. "data" => $formattedData,
  692. ]);
  693. }
  694. /**
  695. * 生产中
  696. * */
  697. public function Production_api(){
  698. // 获取今年的年份
  699. $last_year = date("Y");
  700. // 查询条件:Mod_rq为null且年份为今年的订单状态字段的数量
  701. $data = \db('工单_基本资料')
  702. ->field('gd_statu as 订单状态, COUNT(*) as 订单数量') // 统计每个订单状态的数量
  703. ->where('Sys_rq', 'between', ["$last_year-01-01 00:00:00", "$last_year-12-31 23:59:59"])
  704. ->whereNull('Mod_rq')
  705. ->group('gd_statu') // 按订单状态分组
  706. ->select();
  707. // 如果查询结果为空,设置一个默认值
  708. if (empty($data)) {
  709. $formattedData[] = [
  710. 'name' => ' ',
  711. 'value' => 0
  712. ];
  713. } else {
  714. // 否则根据查询结果格式化数据
  715. $formattedData[] = [
  716. 'name' => ' ',
  717. 'value' => isset($data[1]['订单数量']) ? $data[1]['订单数量'] : 0
  718. ];
  719. }
  720. // 返回 JSON 格式数据
  721. return json([
  722. "status" => 0,
  723. "msg" => "",
  724. "data" => $formattedData,
  725. ]);
  726. }
  727. /**
  728. * 已完工
  729. * */
  730. public function Completed_api(){
  731. // 获取今年的年份
  732. $last_year = date("Y");
  733. // 查询条件:Mod_rq为null且年份为今年的订单状态字段的数量
  734. $data = \db('工单_基本资料')
  735. ->field('gd_statu as 订单状态, COUNT(*) as 订单数量') // 统计每个订单状态的数量
  736. ->where('Sys_rq', 'between', ["$last_year-01-01 00:00:00", "$last_year-12-31 23:59:59"])
  737. ->whereNull('Mod_rq')
  738. ->group('gd_statu') // 按订单状态分组
  739. ->select();
  740. // 如果查询结果为空,设置一个默认值
  741. if (empty($data)) {
  742. $formattedData[] = [
  743. 'name' => ' ',
  744. 'value' => 0
  745. ];
  746. } else {
  747. // 否则根据查询结果格式化数据
  748. $formattedData[] = [
  749. 'name' => ' ',
  750. 'value' => isset($data[2]['订单数量']) ? $data[2]['订单数量'] : 0
  751. ];
  752. }
  753. // 返回 JSON 格式数据
  754. return json([
  755. "status" => 0,
  756. "msg" => "",
  757. "data" => $formattedData,
  758. ]);
  759. }
  760. /**
  761. * 统计当前小组正在生产某个工单
  762. * */
  763. public function group_api() {
  764. $last_year = date("Y");
  765. // 自定义小组排序规则
  766. $group_order = [
  767. '裁剪01组', '裁剪02组', '裁剪03组', '裁剪04组',
  768. '车缝01组', '车缝02组', '车缝03组', '车缝04组', '车缝05组', '车缝06组', '车缝07组', '车缝08组', '车缝09组', '车缝10组', '车缝11组', '车缝12组',
  769. '大办组', '美英组', '小芬组',
  770. '手工01组', '手工02组',
  771. '总检01组', '总检02组',
  772. '大烫01组',
  773. '包装01组', '包装02组'
  774. ];
  775. // 查询条件:按小组和订单编号分组,取最新一条记录
  776. $data = \db('设备_产量计酬')->alias('c')
  777. ->join('工单_基本资料 j', 'j.订单编号 = c.订单编号', 'LEFT')
  778. ->field([
  779. 'c.订单编号',
  780. 'j.客户编号',
  781. 'c.款号',
  782. 'c.尺码',
  783. Db::raw('CASE
  784. WHEN c.工序名称 IN ("裁剪", "车缝") THEN c.数量
  785. ELSE c.s_num
  786. END AS 数量'),
  787. 'c.工序名称',
  788. 'c.sczl_bh', // 小组名称
  789. 'c.sys_rq' // 时间字段
  790. ])
  791. ->group('c.订单编号, c.sczl_bh') // 按订单编号和小组分组,确保每个订单编号只出现一次
  792. ->order('c.sys_rq DESC') // 按时间倒序排序,确保取到最新的一条
  793. ->where('c.sys_rq', 'between', ["$last_year-01-01 00:00:00", "$last_year-12-31 23:59:59"]) // 时间过滤
  794. ->select();
  795. // 格式化数据,按小组取最新的一条工单
  796. $groupedData = [];
  797. foreach ($data as $item) {
  798. // 确保每个小组只有一条最新记录
  799. $groupedData[$item['sczl_bh']][] = [
  800. '订单编号' => $item['订单编号'],
  801. '客户编号' => $item['客户编号'],
  802. '款号' => $item['款号'],
  803. '尺码' => $item['尺码'],
  804. '数量' => $item['数量'],
  805. '工序名称' => $item['工序名称'],
  806. '小组' => $item['sczl_bh'],
  807. '时间' => $item['sys_rq'], // 记录时间
  808. ];
  809. }
  810. // 筛选每个小组的最新一条数据
  811. $latestData = [];
  812. foreach ($groupedData as $group => $groupItems) {
  813. // 只取最新一条记录
  814. $latestData[$group] = $groupItems[0]; // 因为数据是按时间降序排列的,所以取第一个就是最新的
  815. }
  816. // 对小组进行自定义排序
  817. uasort($latestData, function($a, $b) use ($group_order) {
  818. // 获取小组排序位置
  819. $a_index = array_search($a['小组'], $group_order);
  820. $b_index = array_search($b['小组'], $group_order);
  821. // 根据位置排序
  822. return $a_index - $b_index;
  823. });
  824. // 根据自定义列格式生成数据
  825. $result['columns'] = [
  826. // ['name' => '工序名称', 'id' => 'gxxm', 'width' => '10%', 'textAlign' => 'left'],
  827. ['name' => '小组', 'id' => 'xz', 'width' => '10', 'autoWrap' => "true",'textAlign' => 'left'],
  828. ['name' => '订单编号', 'id' => 'ddbh', 'width' => '14','autoWrap' => "true", 'textAlign' => 'left'],
  829. ['name' => '客户编号', 'id' => 'khbh', 'width' => '10','autoWrap' => "true", 'textAlign' => 'left'],
  830. ['name' => '款号', 'id' => 'kh', 'width' => '30', 'textAlign' => 'left'],
  831. ['name' => '尺码', 'id' => 'cm', 'width' => '7','autoWrap' => "true", 'textAlign' => 'left'],
  832. ['name' => '数量', 'id' => 'sl', 'width' => '7', 'autoWrap' => "true",'textAlign' => 'left'],
  833. ['name' => '时间', 'id' => 'sj', 'width' => '20','autoWrap' => "true", 'textAlign' => 'left'],
  834. ];
  835. // 将最新的数据填充到表格行中
  836. $rows = [];
  837. foreach ($latestData as $v) {
  838. $rows[] = [
  839. 'ddbh' => $v['订单编号'],
  840. 'khbh' => $v['客户编号'],
  841. 'kh' => $v['款号'],
  842. 'gxxm' => $v['工序名称'],
  843. 'xz' => $v['小组'],
  844. 'cm' => $v['尺码'],
  845. 'sl' => $v['数量'],
  846. 'sj' => $v['时间'],
  847. ];
  848. }
  849. // 如果没有数据,返回空行
  850. if (empty($rows)) {
  851. $rows[] = [
  852. 'ddbh' => '', 'khbh' => '', 'kh' => '', 'cm' => '', 'gxxm' => '', 'xz' => '', 'sl' => '', 'sj' => ''
  853. ];
  854. }
  855. // 返回最终结果
  856. $res['status'] = 0;
  857. $res['msg'] = '查询成功';
  858. $res['data'] = [
  859. 'columns' => $result['columns'],
  860. 'rows' => $rows
  861. ];
  862. return json($res);
  863. }
  864. /**
  865. * 在产订单工序详情【缓存】
  866. */
  867. public function getJhzByRedis(){
  868. //存缓存Redis
  869. $redis = redis();
  870. $redis_key = md5('getJhzByRedis');
  871. // 定义固定工序名称
  872. $processNames = ['裁剪', '车缝', '手工', '大烫', '总检', '包装'];
  873. // $startTime = date('Y-m-d', strtotime('-180 days'));
  874. $startTime = date('Y-m-01');
  875. $endTime = date('Y-m-d');
  876. $where['c.Sys_rq'] = ['between', [$startTime, $endTime]];
  877. $where['j.出库日期'] = ['<>', 'NULL'];
  878. $where['c.mod_rq'] = null;
  879. // 第一个查询:获取完工数量
  880. $list = \db('工单_基本资料')->alias('j')
  881. ->join('设备_产量计酬 c', 'j.订单编号 = c.订单编号', 'LEFT')
  882. ->field('c.订单编号, j.工单入仓数量, j.客户编号, j.生产款号, j.款式, c.工序名称,
  883. CASE
  884. WHEN 工序名称 IN ("裁剪", "车缝") THEN SUM(数量)
  885. ELSE SUM(s_num)
  886. END as 完工数量,
  887. j.gd_statu as 订单状态')
  888. ->where($where)
  889. ->group('c.订单编号, c.工序名称')
  890. ->order('c.订单编号 desc')
  891. ->having('完工数量 > 0')
  892. ->select();
  893. // 第二个查询:获取制单数
  894. $yjwhere['y.Mod_rq'] = null;
  895. $yjlist = \db('工单_基本资料')->alias('j')
  896. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'LEFT')
  897. ->field('y.订单编号, SUM(y.zdtotal) as 制单数')
  898. ->where($yjwhere)
  899. ->group('y.订单编号')
  900. ->order('y.订单编号 desc')
  901. ->select();
  902. // 将制单数结果转换为以订单编号为键的数组,方便后续合并
  903. $yjlistIndexed = [];
  904. foreach ($yjlist as $yj) {
  905. $yjlistIndexed[$yj['订单编号']] = $yj['制单数'];
  906. }
  907. // 合并数据
  908. $mergedData = [];
  909. foreach ($list as $order) {
  910. $orderId = $order['订单编号'];
  911. // 初始化订单数据,如果还未存在
  912. if (!isset($mergedData[$orderId])) {
  913. // 获取制单数和已完工数量
  914. $zdTotal = $yjlistIndexed[$orderId] ?? 0; // 制单数
  915. $completedTotal = $order['工单入仓数量']; // 已完工数量
  916. // 计算完成百分比
  917. $completionPercentage = $zdTotal > 0 ? round(($completedTotal / $zdTotal) * 100, 2) . '%' : '0%';
  918. // 初始化订单数据
  919. $mergedData[$orderId] = [
  920. '订单编号' => $orderId,
  921. '客户编号' => $order['客户编号'],
  922. '生产款号' => $order['生产款号'],
  923. '款式' => $order['款式'],
  924. '订单状态' => $order['订单状态'],
  925. '已完工数量' => $completedTotal,
  926. '制单数' => $zdTotal,
  927. '完成百分比' => $completionPercentage, // 新增字段:完成百分比
  928. ];
  929. // 初始化所有工序的完工数量为空
  930. foreach ($processNames as $process) {
  931. $mergedData[$orderId][$process] = '';
  932. }
  933. }
  934. // 更新对应的工序名称和完工数量
  935. if (in_array($order['工序名称'], $processNames)) {
  936. $mergedData[$orderId][$order['工序名称']] = $order['完工数量'];
  937. }
  938. }
  939. foreach ($mergedData as $key => $order) {
  940. // 判断裁剪是否为空
  941. if (empty($order['裁剪'])) {
  942. unset($mergedData[$key]); // 如果裁剪为空,删除该订单
  943. }
  944. }
  945. // 转换合并后的数组格式
  946. $result = array_values($mergedData);
  947. $redis->set($redis_key, json_encode($result));
  948. echo date("Y-m-d H:i:s").'----存进去了';
  949. // return $result;
  950. }
  951. /**
  952. * 在产订单工序详情【接口】
  953. */
  954. public function getJhzBy(){
  955. $redis = redis();
  956. $row = json_decode($redis->get(md5('getJhzByRedis')), true);
  957. // 列定义
  958. $result['columns'] = [
  959. ['name' => '订单编号', 'id' => 'ddbh', 'width' => '7', 'textAlign' => 'left'],
  960. ['name' => '客户编号', 'id' => 'khbh', 'width' => '6','autoWrap' => "true", 'textAlign' => 'left'],
  961. ['name' => '生产款号', 'id' => 'sckh', 'width' => '12', 'textAlign' => 'left'],
  962. ['name' => '款式', 'id' => 'ks', 'width' => '10', 'textAlign' => 'left'],
  963. ['name' => '订单状态', 'id' => 'ddzt', 'width' => '6', 'textAlign' => 'left'],
  964. ['name' => '制单总数', 'id' => 'zds', 'width' => '6', 'textAlign' => 'left'],
  965. ['name' => '裁剪已完成', 'id' => 'cj', 'width' => '6','autoWrap'=>"true", 'textAlign' => 'left'],
  966. ['name' => '车缝已完成', 'id' => 'cf', 'width' => '6','autoWrap'=>"true", 'textAlign' => 'left'],
  967. ['name' => '手工已完成', 'id' => 'sg', 'width' => '6','autoWrap'=>"true", 'textAlign' => 'left'],
  968. ['name' => '大烫已完成', 'id' => 'dt', 'width' => '6','autoWrap'=>"true", 'textAlign' => 'left'],
  969. ['name' => '总检已完成', 'id' => 'zj', 'width' => '6','autoWrap'=>"true", 'textAlign' => 'left'],
  970. ['name' => '包装已完成', 'id' => 'bz', 'width' => '6','autoWrap'=>"true", 'textAlign' => 'left'],
  971. ['name' => '已完成数量', 'id' => 'ywgsl', 'width' => '6','autoWrap'=>"true", 'textAlign' => 'left'],
  972. ['name' => '完成百分比', 'id' => 'bfb', 'width' => '6','autoWrap'=>"true", 'textAlign' => 'left'],
  973. ];
  974. if ($row) {
  975. foreach ($row as $k => $v) {
  976. $result['rows'][$k]['ddbh'] = $v['订单编号'];
  977. $result['rows'][$k]['khbh'] = $v['客户编号'];
  978. $result['rows'][$k]['sckh'] = $v['生产款号'];
  979. $result['rows'][$k]['ks'] = $v['款式'];
  980. $result['rows'][$k]['ddzt'] = $v['订单状态'];
  981. // 工序数量字段,根据之前的定义调整字段名
  982. $result['rows'][$k]['cj'] = $v['裁剪'] ?? '';
  983. $result['rows'][$k]['cf'] = $v['车缝'] ?? '';
  984. $result['rows'][$k]['sg'] = $v['手工'] ?? '';
  985. $result['rows'][$k]['dt'] = $v['大烫'] ?? '';
  986. $result['rows'][$k]['zj'] = $v['总检'] ?? '';
  987. $result['rows'][$k]['bz'] = $v['包装'] ?? '';
  988. // 其他数量字段
  989. $result['rows'][$k]['ywgsl'] = $v['已完工数量'] ?? ''; // 示例字段,需确保与数据一致
  990. $result['rows'][$k]['zds'] = $v['制单数'] ?? ''; // 对应制单数
  991. $result['rows'][$k]['bfb'] = $v['完成百分比'] ?? ''; // 对应制单数
  992. }
  993. } else {
  994. // 如果没有数据,初始化空行
  995. $result['rows'][0] = [
  996. 'ddbh' => '', 'khbh' => '', 'sckh' => '', 'ks' => '', 'ddzt' => '',
  997. 'cj' => '', 'cf' => '', 'sg' => '', 'dt' => '', 'zj' => '', 'bz' => '',
  998. 'ywgsl' => '', 'zds' => ''
  999. ];
  1000. }
  1001. // echo "<pre>";print_r($result);echo "<pre>";die;
  1002. $res['status'] = 0;
  1003. $res['msg'] = '';
  1004. $res['data'] = $result;
  1005. return json($res);
  1006. }
  1007. /**
  1008. *当日班组报工产量【缓存】
  1009. * 建议缓存时间,5--10分钟自动更新一次
  1010. * 统计当天的各个小组报工完成数量
  1011. */
  1012. public function GroupProductionRedis() {
  1013. // 存缓存到 Redis
  1014. $redis = redis();
  1015. $redis_key = md5('GroupProductionRedis');
  1016. $today = date('Y-m-d'); // 获取今天的日期
  1017. $sevenDaysAgo = date('Y-m-d', strtotime('-6 days')); // 计算6天前的日期(包含今天共7天)
  1018. // 自定义需要显示的生产工序顺序
  1019. $customGroups = ['裁剪', '车缝', '手工', '大烫', '总检', '包装'];
  1020. // 获取所有设备资料,并按自定义工序顺序过滤
  1021. $Machine = db('设备_基本资料')
  1022. ->field('生产工序, 设备编组')
  1023. ->whereIn('生产工序', $customGroups)
  1024. ->select();
  1025. // 查询当天设备产量数据
  1026. $res = db('设备_产量计酬')->alias('c')
  1027. ->field([
  1028. 'c.sczl_bh',
  1029. // 使用 CASE 语句动态选择字段:裁剪和车缝使用数量,其他工序使用 s_num
  1030. Db::raw('CASE
  1031. WHEN c.工序名称 IN ("裁剪", "车缝") THEN c.数量
  1032. ELSE s_num
  1033. END AS 数量'),
  1034. 'c.工序名称',
  1035. 'c.sczl_jtbh',
  1036. 'c.sys_rq'
  1037. ])
  1038. ->where(function($query) {
  1039. // 过滤 mod_rq 为 NULL 或 空字符串的记录
  1040. $query->where('c.mod_rq', null)->whereOr('c.mod_rq', '');
  1041. })
  1042. ->where('c.sys_rq', '>=', $sevenDaysAgo . ' 00:00:00') // 从今天00:00开始
  1043. ->where('c.sys_rq', '<=', $today . ' 23:59:59') // 到今天23:59结束
  1044. ->select();
  1045. // 将合并后的数据存储到 Redis 中
  1046. $redis->set($redis_key, json_encode($res));
  1047. echo date("Y-m-d H:i:s") . ' 存进去了';
  1048. }
  1049. //当日报工数量
  1050. public function GroupMachineCFyi()
  1051. {
  1052. $redis = redis(); // 连接 Redis
  1053. $row = json_decode($redis->get(md5('GroupProductionRedis')), true); // 从 Redis 获取数据
  1054. // 获取请求参数中的组别代码
  1055. $req = $this->request->param();
  1056. if (isset($req['jtbh']) && !empty($req['jtbh'])) {
  1057. $arr = $req['jtbh'];
  1058. } else {
  1059. $this->error('参数错误'); // 如果没有传递有效的组别代码,返回错误
  1060. }
  1061. // 获取当前日期
  1062. $today = date('Y-m-d');
  1063. $toYear = date('Y'); // 当前年份
  1064. // 获取七天前的日期
  1065. $sevenDaysAgo = date('Y-m-d', strtotime('-6 days'));
  1066. // 初始化分类和数据系列
  1067. $categories = []; // 存储七天的日期
  1068. $dataSeries = []; // 存储不同工序的数量汇总
  1069. // 获取近七天的日期数组(只取日期部分)
  1070. for ($i = 6; $i >= 0; $i--) {
  1071. $categories[] = date('d', strtotime("-{$i} days")); // 只取日期部分
  1072. }
  1073. // 遍历 Redis 获取的数据,筛选出符合条件的数据
  1074. foreach ($row as $data) {
  1075. // 检查组别代码是否匹配
  1076. if ($data['sczl_jtbh'] === $arr) {
  1077. $date = date('Y-m-d', strtotime($data['sys_rq'])); // 获取数据的日期
  1078. if ($date >= $sevenDaysAgo && $date <= $today) {
  1079. // 计算该日期在七天日期数组中的索引
  1080. $dateIndex = array_search(date('d', strtotime($date)), $categories);
  1081. if ($dateIndex !== false) {
  1082. // 根据工序名称汇总
  1083. $processName = $data['工序名称'];
  1084. // 如果工序名称还没在 $dataSeries 中创建,先初始化
  1085. if (!isset($dataSeries[$processName])) {
  1086. $dataSeries[$processName] = array_fill(0, 7, 0); // 初始化该工序的数据
  1087. }
  1088. // 累加数量
  1089. $dataSeries[$processName][$dateIndex] += $data['数量']; // 使用工序名称作为键名
  1090. }
  1091. }
  1092. }
  1093. }
  1094. // 如果没有任何数据,确保工序数据为默认值
  1095. if (empty($dataSeries)) {
  1096. // 如果没有数据,初始化默认工序并设置数据为0
  1097. $dataSeries = [
  1098. '默认工序' => array_fill(0, 7, 0)
  1099. ];
  1100. }
  1101. // 构建返回的数据格式
  1102. $seriesData = [];
  1103. foreach ($dataSeries as $processName => $values) {
  1104. $seriesData[] = [
  1105. 'name' => $processName, // 工序名称作为名称
  1106. 'data' => $values // 当前工序的数量数据
  1107. ];
  1108. }
  1109. $list = [
  1110. 'categories' => $categories, // 七天的日期
  1111. 'series' => $seriesData // 不同工序的数据
  1112. ];
  1113. // 返回 JSON 格式的结果
  1114. $res = [
  1115. 'status' => 0,
  1116. 'msg' => '',
  1117. 'data' => $list
  1118. ];
  1119. return json($res); // 返回最终的 JSON 数据
  1120. }
  1121. //下面接口已写好-----暂时未用到
  1122. //板房车缝当日报工数量
  1123. public function GroupMachinebfCFyi()
  1124. {
  1125. $redis = redis();
  1126. $row = json_decode($redis->get(md5('GroupProductionRedis')), true);
  1127. // 初始化小组名称和数量
  1128. $categories = [];
  1129. $dataSeries = [];
  1130. // 遍历数据,找到所有工序名称为“车缝”的小组,且只包含“大办组”、“小芬组”、“美英组”
  1131. foreach ($row as $data) {
  1132. if ($data['工序名称'] === '车缝' && in_array($data['组别'], ['大办组', '小芬组', '美英组'])) {
  1133. $categories[] = $data['组别']; // 添加组别到分类中
  1134. $dataSeries[$data['组别']] = $data['数量']; // 更新小组数量
  1135. }
  1136. }
  1137. // 构建输出数据格式
  1138. $list = [
  1139. 'categories' => $categories,
  1140. 'series' => [
  1141. [
  1142. 'name' => '完成',
  1143. 'data' => array_values($dataSeries) // 获取数量数组
  1144. ]
  1145. ]
  1146. ];
  1147. $res = [
  1148. 'status' => 0,
  1149. 'msg' => '',
  1150. 'data' => $list
  1151. ];
  1152. // 返回 JSON 格式的结果
  1153. return json($res);
  1154. }
  1155. //裁剪当日报工数量
  1156. public function GroupMachineCJyi()
  1157. {
  1158. $redis = redis();
  1159. $row = json_decode($redis->get(md5('GroupProductionRedis')), true);
  1160. // 初始化小组名称和数量
  1161. $categories = [];
  1162. $dataSeries = [];
  1163. // 遍历数据,找到所有工序名称为“车缝”的小组
  1164. foreach ($row as $data) {
  1165. if ($data['工序名称'] !== '车缝') {
  1166. $categories[] = $data['组别']; // 添加组别到分类中
  1167. $dataSeries[$data['组别']] = $data['数量']; // 更新小组数量
  1168. }
  1169. }
  1170. // 构建输出数据格式
  1171. $list = [
  1172. 'categories' => $categories,
  1173. 'series' => [
  1174. [
  1175. 'name' => '完成',
  1176. 'data' => array_values($dataSeries) // 获取数量数组
  1177. ]
  1178. ]
  1179. ];
  1180. $res = [
  1181. 'status' => 0,
  1182. 'msg' => '',
  1183. 'data' => $list
  1184. ];
  1185. // 返回 JSON 格式的结果
  1186. return json($res);
  1187. }
  1188. //手工当日报工数量
  1189. public function GroupMachineSGyi()
  1190. {
  1191. $redis = redis();
  1192. $row = json_decode($redis->get(md5('GroupProductionRedis')), true);
  1193. // 初始化小组名称和数量
  1194. $categories = [];
  1195. $dataSeries = [];
  1196. // 遍历数据,找到所有工序名称为“车缝”的小组
  1197. foreach ($row as $data) {
  1198. if ($data['工序名称'] === '手工') {
  1199. $categories[] = $data['组别']; // 添加组别到分类中
  1200. $dataSeries[$data['组别']] = $data['数量']; // 更新小组数量
  1201. }
  1202. }
  1203. // 构建输出数据格式
  1204. $list = [
  1205. 'categories' => $categories,
  1206. 'series' => [
  1207. [
  1208. 'name' => '完成',
  1209. 'data' => array_values($dataSeries) // 获取数量数组
  1210. ]
  1211. ]
  1212. ];
  1213. $res = [
  1214. 'status' => 0,
  1215. 'msg' => '',
  1216. 'data' => $list
  1217. ];
  1218. // 返回 JSON 格式的结果
  1219. return json($res);
  1220. }
  1221. //大烫当日报工数量
  1222. public function GroupMachineDTyi()
  1223. {
  1224. $redis = redis();
  1225. $row = json_decode($redis->get(md5('GroupProductionRedis')), true);
  1226. // 初始化小组名称和数量
  1227. $categories = [];
  1228. $dataSeries = [];
  1229. // 遍历数据,找到所有工序名称为“车缝”的小组
  1230. foreach ($row as $data) {
  1231. if ($data['工序名称'] === '大烫') {
  1232. $categories[] = $data['组别']; // 添加组别到分类中
  1233. $dataSeries[$data['组别']] = $data['数量']; // 更新小组数量
  1234. }
  1235. }
  1236. // 构建输出数据格式
  1237. $list = [
  1238. 'categories' => $categories,
  1239. 'series' => [
  1240. [
  1241. 'name' => '完成',
  1242. 'data' => array_values($dataSeries) // 获取数量数组
  1243. ]
  1244. ]
  1245. ];
  1246. $res = [
  1247. 'status' => 0,
  1248. 'msg' => '',
  1249. 'data' => $list
  1250. ];
  1251. // 返回 JSON 格式的结果
  1252. return json($res);
  1253. }
  1254. //总检当日报工数量
  1255. public function GroupMachineZJyi()
  1256. {
  1257. $redis = redis();
  1258. $row = json_decode($redis->get(md5('GroupProductionRedis')), true);
  1259. // 初始化小组名称和数量
  1260. $categories = [];
  1261. $dataSeries = [];
  1262. // 遍历数据,找到所有工序名称为“车缝”的小组
  1263. foreach ($row as $data) {
  1264. if ($data['工序名称'] === '总检') {
  1265. $categories[] = $data['组别']; // 添加组别到分类中
  1266. $dataSeries[$data['组别']] = $data['数量']; // 更新小组数量
  1267. }
  1268. }
  1269. // 构建输出数据格式
  1270. $list = [
  1271. 'categories' => $categories,
  1272. 'series' => [
  1273. [
  1274. 'name' => '完成',
  1275. 'data' => array_values($dataSeries) // 获取数量数组
  1276. ]
  1277. ]
  1278. ];
  1279. $res = [
  1280. 'status' => 0,
  1281. 'msg' => '',
  1282. 'data' => $list
  1283. ];
  1284. // 返回 JSON 格式的结果
  1285. return json($res);
  1286. }
  1287. //包装当日报工数量
  1288. public function GroupMachineBZyi()
  1289. {
  1290. $redis = redis();
  1291. $row = json_decode($redis->get(md5('GroupProductionRedis')), true);
  1292. // 初始化小组名称和数量
  1293. $categories = [];
  1294. $dataSeries = [];
  1295. // 遍历数据,找到所有工序名称为“车缝”的小组
  1296. foreach ($row as $data) {
  1297. if ($data['工序名称'] === '包装') {
  1298. $categories[] = $data['组别']; // 添加组别到分类中
  1299. $dataSeries[$data['组别']] = $data['数量']; // 更新小组数量
  1300. }
  1301. }
  1302. // 构建输出数据格式
  1303. $list = [
  1304. 'categories' => $categories,
  1305. 'series' => [
  1306. [
  1307. 'name' => '完成',
  1308. 'data' => array_values($dataSeries) // 获取数量数组
  1309. ]
  1310. ]
  1311. ];
  1312. $res = [
  1313. 'status' => 0,
  1314. 'msg' => '',
  1315. 'data' => $list
  1316. ];
  1317. // 返回 JSON 格式的结果
  1318. return json($res);
  1319. }
  1320. //自动生成面料以及调用---》GPT
  1321. public function GtpAiOrder()
  1322. {
  1323. if (!$this->request->isGet()) {
  1324. $this->error('请求错误');
  1325. }
  1326. $order = $this->request->param('订单编号');
  1327. // 判断是否有指定的订单号
  1328. if (!empty($order)) {
  1329. // 查询单个订单的最大编号
  1330. $maxOrder = \db('工单_基本资料')
  1331. ->where('订单编号', 'like', "{$order}%")
  1332. ->order('订单编号', 'desc')
  1333. ->limit(1)
  1334. ->value('订单编号');
  1335. // 查询该订单的基本资料
  1336. $list = \db('工单_基本资料')
  1337. ->where('订单编号', 'like', "{$order}%")
  1338. ->order('订单编号', 'desc')
  1339. ->limit(1)
  1340. ->find();
  1341. // 如果面料数据为空,提示错误
  1342. if (empty($list['面料'])) {
  1343. $this->error('面料数据为空无法定义BOM');
  1344. }
  1345. // 处理订单编号
  1346. $numericPart = substr($maxOrder, 2);
  1347. $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT);
  1348. $param['订单编号'] = $order . $newNumericPart;
  1349. // 假设massage是从数据库获取的数据
  1350. $massage = empty($list['粘衬']) || $list['粘衬'] == '无' ? $list['面料'] : $list['面料'] . ',粘衬:' . $list['粘衬'];
  1351. $materialCategories = [];
  1352. $pattern = '/(\S+?):([^,]+)/'; // 匹配 类别:物料 格式
  1353. preg_match_all($pattern, $massage, $matches, PREG_SET_ORDER);
  1354. foreach ($matches as $match) {
  1355. $category = $match[1]; // 分类名称
  1356. $materials = explode('+', $match[2]); // 如果物料名称中有‘+’,则分开处理多个物料
  1357. // 将分类和对应的物料添加到数组中
  1358. foreach ($materials as $material) {
  1359. $materialCategories[$category][] = trim($material); // 去除物料两边的空格
  1360. }
  1361. }
  1362. $mianliao = $this->Gpt($massage);
  1363. $data = [];
  1364. foreach ($mianliao as $value) {
  1365. if (!empty($value) && $value !== '粘衬') { // 排除空值和粘衬
  1366. $category = '';
  1367. // 查找物料对应的分类
  1368. foreach ($materialCategories as $cat => $materials) {
  1369. if (in_array($value, $materials)) {
  1370. $category = $cat;
  1371. break;
  1372. }
  1373. }
  1374. // 如果找到分类,将数据存入BOM
  1375. $data[] = [
  1376. 'BOM_工单编号' => $list['订单编号'],
  1377. 'BOM_物料名称' => $value,
  1378. 'BOM_desc' => '',
  1379. '物料分类' => $category ? $category : '',
  1380. 'Sys_rq' => date('Y-m-d H:i:s'),
  1381. 'Sys_ID' => '超级管理员'
  1382. ];
  1383. }
  1384. }
  1385. foreach ($data as &$item) {
  1386. if (empty($item['物料分类'])) {
  1387. $item['物料分类'] = '';
  1388. }
  1389. // 去除所有非汉字字符
  1390. $item['物料分类'] = preg_replace('/[^\p{Han}]/u', '', $item['物料分类']);
  1391. }
  1392. //// 批量更新 BOM_desc,如果对应的 BOM_desc 没有填充就更新
  1393. // foreach ($data as $item) {
  1394. // if (!empty($item['物料分类'])) {
  1395. // \db('工单_bom资料')->insertAll($data);
  1396. //
  1397. //// \db('工单_面料资料')
  1398. //// ->where('BOM_工单编号', $item['BOM_工单编号'])
  1399. //// ->where('BOM_物料名称', $item['BOM_物料名称'])
  1400. //// ->update(['BOM_desc' => preg_replace('/[^\w\s]/u', '', $item['BOM_desc'])]);
  1401. //
  1402. // }
  1403. // }
  1404. // $this->success('成功',$order);
  1405. } else {
  1406. // 如果没有指定订单号,批量查询订单号并处理
  1407. $has_bom = \db('工单_bom资料')->alias('a')->field('a.BOM_工单编号')->group('a.BOM_工单编号')->select();
  1408. $all_orders = \db('工单_基本资料')->alias('a')->field('a.订单编号')->where('a.面料', '<>', '')->group('a.订单编号')->select();
  1409. // 提取有BOM资料的订单号
  1410. $has_bom_orders = array_column($has_bom, 'BOM_工单编号');
  1411. // 筛选出没有对应BOM资料的订单号
  1412. $no_bom_orders = array_filter($all_orders, function ($order) use ($has_bom_orders) {
  1413. return !in_array($order['订单编号'], $has_bom_orders);
  1414. });
  1415. // 遍历没有BOM资料的订单
  1416. foreach ($no_bom_orders as $orderData) {
  1417. // 获取该订单号的最大订单编号
  1418. $maxOrder = \db('工单_基本资料')
  1419. ->where('订单编号', 'like', "{$orderData['订单编号']}%")
  1420. ->order('订单编号', 'desc')
  1421. ->limit(1)
  1422. ->value('订单编号');
  1423. // 获取该订单号的具体数据
  1424. $list = \db('工单_基本资料')
  1425. ->where('订单编号', 'like', "{$orderData['订单编号']}%")
  1426. ->order('订单编号', 'desc')
  1427. ->limit(1)
  1428. ->find();
  1429. if (empty($list['面料'])) {
  1430. $this->error("订单 {$orderData['订单编号']} 面料数据为空,无法定义BOM");
  1431. }
  1432. // 处理订单编号
  1433. $numericPart = substr($maxOrder, 2);
  1434. $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT);
  1435. $param['订单编号'] = $order . $newNumericPart;
  1436. // 处理物料信息
  1437. $massage = empty($list['粘衬']) || $list['粘衬'] == '无' ? $list['面料'] : $list['面料'] . ',粘衬:' . $list['粘衬'];
  1438. $mianliao = $this->Gpt($massage);
  1439. // 插入物料数据
  1440. $data = [];
  1441. foreach ($mianliao as $key => $value) {
  1442. if (!empty($value) && $value !== '粘衬') { // 排除空值和粘衬
  1443. $data[] = [
  1444. 'BOM_工单编号' => $list['订单编号'],
  1445. 'BOM_物料名称' => $value,
  1446. 'BOM_desc' => '',
  1447. '物料分类' => '',
  1448. 'Sys_rq' => date('Y-m-d H:i:s'),
  1449. 'Sys_ID' => '超级管理员'
  1450. ];
  1451. }
  1452. }
  1453. // 批量插入BOM资料
  1454. if (!empty($data)) {
  1455. \db('工单_bom资料')->insertAll($data);
  1456. }
  1457. }
  1458. $this->success('成功');
  1459. }
  1460. }
  1461. //GPT
  1462. public function Gpt($massage)
  1463. {
  1464. // 设置 API 密钥
  1465. $apiKey = 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY'; // 替换为您的 API 密钥
  1466. // 要发送给 GPT 的消息
  1467. $messages = [
  1468. [
  1469. 'role' => 'user',
  1470. 'content' => '你好,帮我按照:面料1、面料2、面料3……来整理归纳下面的面料信息,我只需要面料信息,不需要其他:'.$massage
  1471. ]
  1472. ];
  1473. // 创建请求数据
  1474. $data = [
  1475. 'model' => 'gpt-3.5-turbo', // 使用的模型
  1476. 'messages' => $messages,
  1477. 'max_tokens' => 100, // 设置最大 token 数
  1478. ];
  1479. // 初始化 cURL
  1480. $ch = curl_init('https://niubi.zeabur.app/v1/chat/completions');
  1481. // 设置 cURL 选项
  1482. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1483. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  1484. 'Content-Type: application/json',
  1485. 'Authorization: Bearer ' . $apiKey,
  1486. ]);
  1487. curl_setopt($ch, CURLOPT_POST, true);
  1488. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  1489. curl_setopt($ch, CURLOPT_CAINFO, ROOT_PATH . '/public/uploads/cacert.pem');
  1490. // 执行请求
  1491. $response = curl_exec($ch);
  1492. // 检查错误
  1493. if (curl_errno($ch)) {
  1494. echo 'Error:' . curl_error($ch);
  1495. }
  1496. // 关闭 cURL
  1497. curl_close($ch);
  1498. // 解析和输出响应
  1499. $responseData = json_decode($response, true);
  1500. // 获取 GPT 的回复
  1501. if (isset($responseData['choices'][0]['message']['content'])) {
  1502. //获取返回内容
  1503. $gptReply = $responseData['choices'][0]['message']['content'];
  1504. // halt($gptReply);
  1505. //返回面料信息
  1506. $gptArray = explode('面料',$gptReply);
  1507. array_shift($gptArray);
  1508. foreach ($gptArray as $key=>$value){
  1509. $gptArray[$key] = preg_replace('/\s+/', '', substr($value,4));
  1510. }
  1511. return $gptArray;
  1512. } else {
  1513. echo "未能获取 GPT 的回复。";
  1514. }
  1515. }
  1516. }