Product.php 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use \think\Request;
  5. use \think\Db;
  6. use think\Cache;
  7. use function EasyWeChat\Kernel\Support\rsa_public_encrypt;
  8. use function fast\e;
  9. /**
  10. * 产品管理接口
  11. */
  12. class Product extends Api
  13. {
  14. protected $noNeedLogin = ['*'];
  15. protected $noNeedRight = ['*'];
  16. /**
  17. * 首页
  18. *
  19. */
  20. public function index()
  21. {
  22. $this->success('请求成功');
  23. }
  24. /**
  25. * 获取产品资料
  26. *
  27. * @ApiMethod GET
  28. *@param string custom_code
  29. *@param string limit
  30. *@param string page
  31. */
  32. public function getProduct(){
  33. if (Request::instance()->isGet() == false){
  34. $this->error('非法请求');
  35. }
  36. $params = Request::instance()->param();
  37. $limit = $params['limit'];
  38. $yjno = 10;
  39. if (!isset($limit)){
  40. $limit = 15;
  41. }
  42. if (!isset($params['page'])){
  43. $pages = 0;
  44. }else{
  45. $pages = ((int)$params['page'] -1 ) * (int)$limit;
  46. }
  47. $total = 0;
  48. if (isset($params['custom_code']) && !empty($params['custom_code'])){
  49. $customCode = $params['custom_code'];
  50. $sql = "SELECT rtrim(客户编号) as 客户编号,rtrim(客户名称) as 客户名称,客户料号,rtrim(产品编号) as 产品编号,rtrim(产品名称) as 产品名称,版本号,成品规格,
  51. rtrim(计量单位) as 计量单位,rtrim(产品类别) as 产品类别,生产类别,产品备注,投产日期,状态,U8UID,rtrim(Sys_id) as Sys_id,Sys_rq,Mod_rq,UniqID
  52. FROM `产品_基本资料` WHERE `客户编号` = '{$customCode}' ORDER BY CASE WHEN `状态` IS NULL THEN 0 ELSE 1 END,
  53. `客户编号`ASC,`状态` ASC,`产品编号` DESC LIMIT {$limit} OFFSET {$pages}";
  54. $total = db('产品_基本资料')->where('客户编号',$customCode)->count();
  55. }else{
  56. if (isset($params['search']) && !empty($params['search'])){
  57. $search = $params['search'];
  58. $sql = "SELECT rtrim(客户编号) as 客户编号,rtrim(客户名称) as 客户名称,客户料号,rtrim(产品编号) as 产品编号,rtrim(产品名称) as 产品名称,版本号,成品规格,
  59. rtrim(计量单位) as 计量单位,rtrim(产品类别) as 产品类别,生产类别,产品备注,投产日期,状态,U8UID,rtrim(Sys_id) as Sys_id,Sys_rq,Mod_rq,UniqID
  60. FROM `产品_基本资料` WHERE `产品名称` LIKE '%{$search}%' OR `产品编号` LIKE '%{$search}%' ORDER BY CASE WHEN `状态` IS NULL THEN 0 ELSE 1 END,
  61. `客户编号`ASC,`状态` ASC,`产品编号` DESC LIMIT {$limit} OFFSET {$pages}";
  62. $total = db('产品_基本资料')->where('产品名称','like','%'.$search.'%')->count();
  63. }else{
  64. $sql = "SELECT rtrim(客户编号) as 客户编号,rtrim(客户名称) as 客户名称,客户料号,rtrim(产品编号) as 产品编号,rtrim(产品名称) as 产品名称,版本号,成品规格,
  65. rtrim(计量单位) as 计量单位,rtrim(产品类别) as 产品类别,生产类别,产品备注,投产日期,状态,U8UID,rtrim(Sys_id) as Sys_id,Sys_rq,Mod_rq,UniqID
  66. FROM `产品_基本资料` ORDER BY CASE WHEN `状态` IS NULL THEN 0 ELSE 1 END,
  67. `客户编号`ASC,`状态` ASC,`产品编号` DESC LIMIT {$limit} OFFSET {$pages}";
  68. $total = db('产品_基本资料')->count();
  69. }
  70. }
  71. $list = Db::query($sql);
  72. foreach ($list as $key=>$value){
  73. $code = trim($value['产品编号']);
  74. $gd_sql = "SELECT `接单日期` FROM `工单_基本资料` WHERE `成品代号` = '{$code}' ORDER BY Uniqid DESC";
  75. $gdRes = Db::query($gd_sql);
  76. $list[$key]['receiveDate'] = '';
  77. if (!empty($gdRes)){
  78. $list[$key]['receiveDate'] = $gdRes[0]['接单日期'];
  79. }
  80. if (isset($params['sort'])){
  81. $gy_sql = "SELECT * FROM `产品_工艺资料` WHERE Gy0_cpdh = '{$code}' AND Gy0_yjno >= '{$yjno}'";
  82. $gyRes = Db::query($gy_sql);
  83. }else{
  84. $gy_sql = "SELECT * FROM `产品_工艺资料` WHERE Gy0_cpdh = '{$code}'";
  85. $gyRes = Db::query($gy_sql);
  86. }
  87. $list[$key]['gyData'] = '';
  88. if (empty($gyRes)){
  89. $list[$key]['gyData'] = '缺';
  90. }
  91. if (isset($params['sort'])){
  92. $yj_sql = "SELECT COUNT(*) as total FROM `产品_印件资料` WHERE `yj_cpdh` = '{$code}' AND yj_yjno >= '{$yjno}'";
  93. $yjRes = Db::query($yj_sql);
  94. }else{
  95. $yj_sql = "SELECT COUNT(*) as total FROM `产品_印件资料` WHERE `yj_cpdh` = '{$code}'";
  96. $yjRes = Db::query($yj_sql);
  97. }
  98. $list[$key]['yjData'] = '无';
  99. if ($yjRes[0]['total'] > 0){
  100. $list[$key]['yjData'] = $yjRes[0]['total'];
  101. }
  102. }
  103. $data['data'] = $list;
  104. $data['total'] = $total;
  105. $this->success('请求成功',$data);
  106. }
  107. /**
  108. * 获取产品基础数据
  109. *
  110. * @ApiMethod POST
  111. *@param string product_code
  112. */
  113. public function getProductData(){
  114. if (Request::instance()->isGet() == false){
  115. $this->error('非法请求');
  116. }
  117. $params = Request::instance()->param();
  118. $code = $params['product_code'];
  119. if (!isset($code)){
  120. $this->error('参数不能为空');
  121. }
  122. if (isset($params['sort'])){
  123. $PriWhere['yj_yjno'] = ['>=',10];
  124. $proWhere['Gy0_yjno'] = ['>=',10];
  125. }else{
  126. $PriWhere['yj_yjno'] = ['<',10];
  127. $proWhere['Gy0_yjno'] = ['<',10];
  128. }
  129. $num = config('product_code_digit');
  130. //工艺资料
  131. $option['a.Gy0_cpdh'] = $code;
  132. $gy_field = 'rtrim(a.Gy0_方案) as 方案,a.Gy0_yjno,a.Gy0_gxh,rtrim(a.gy0_gxmc) as gy0_gxmc,rtrim(a.Add_gxmc) as add_gxmc,a.Gy0_Ks,a.Gy0_ls,rtrim(a.工序备注) as 备注,
  133. a.工价系数,a.损耗系数,a.Gy0_Ms,a.人工检_正品板,a.人工检_次品板,a.人工检_废检,a.机检_正品板,a.机检_次品板,a.机检_废检,rtrim(a.Gy0_sbmc) as Gy0_sbmc,rtrim(a.Sys_id) as Sys_id,
  134. a.Sys_rq,a.Mod_rq,b.sys_rate0 as 基础损耗,b.sys_rate1 as 损耗率,a.UniqID';
  135. $gyRes = db('产品_工艺资料')->alias('a')
  136. ->join('dic_lzsh b','a.Gy0_shdh = b.sys_bh','left')
  137. ->where($option)->where($proWhere)->field($gy_field)->order('a.Gy0_yjno asc,a.Gy0_gxh asc')->select();
  138. //印件资料
  139. $where['yj_cpdh'] = $code;
  140. $field = 'yj_yjno,rtrim(yj_yjdh) as yj_yjdh,yj_yjmc,rtrim(yj_zzdh) as yj_zzdh,rtrim(yj_zzmc) as yj_zzmc,rtrim(yj_tlgg) as yj_tlgg,
  141. rtrim(yj_klgg) as yj_klgg,yj_ks,yj_ls,rtrim(yj_desc) as yj_desc,rtrim(sys_id) as sys_id,sys_rq,mod_rq,UniqId';
  142. $yjRes = db('产品_印件资料')->where($where)->where($PriWhere)->field($field)->select();
  143. //印版资料
  144. $yb_field = 'rtrim(a.YB_方案) as YB_方案,a.YB_Yjno,rtrim(a.存货编码) as 存货编码,a.考核印数,rtrim(a.Sys_id) as Sys_id,a.Mod_rq,
  145. rtrim(b.物料名称) as 印版名称,rtrim(c.名称) as 印版类别,a.UniqID,rtrim(d.供方批号) as 供方批号,e.Yb_领用机台 as 领用机台,sum(e.Yb_印数) as 印数,d.UniqID';
  146. $query = db('产品_印版资料')->alias('a')
  147. ->join('物料_存货编码 b', 'a.存货编码 = b.物料代码', 'left')
  148. ->join('物料_存货结构 c',
  149. 'LEFT(a.存货编码, CASE WHEN a.存货编码 REGEXP \'[a-zA-Z]\' THEN 5 ELSE 4 END) = c.编号',
  150. 'left'
  151. )
  152. ->join('产品_印版库 d', 'a.存货编码 = d.存货编码', 'left')
  153. ->join('工单_印版领用记录 e', 'a.存货编码 = e.Yb_存货编码 and d.供方批号 = e.Yb_供方批号', 'left')
  154. ->where('e.Yb_退还日期', null)
  155. ->order('a.YB_Yjno,a.存货编码')
  156. ->group('a.YB_Yjno,a.存货编码,d.UniqID');
  157. // 动态处理$code参数类型
  158. if (is_array($code)) {
  159. $query->whereIn('a.YB_Cpdh', $code);
  160. } else {
  161. $query->where('a.YB_Cpdh', $code);
  162. }
  163. // 添加其他过滤条件
  164. if (!empty($filter) && is_array($filter)) {
  165. foreach ($filter as $field => $value) {
  166. $query->where($field, $value);
  167. }
  168. }
  169. $ybRes = $query->field($yb_field)->select();
  170. //技术附件
  171. $jsRes = db('产品_技术附件')
  172. ->where('关联产品','like','%'.$code.'%')
  173. ->select();
  174. foreach ($jsRes as $key=>&$value){
  175. if(mb_detect_encoding($value['附件内容'])!='ASCII'){
  176. $value['附件内容'] = '';
  177. }
  178. }
  179. $list = [];
  180. $list['yjData'] = $yjRes;
  181. $list['gyData'] = $gyRes;
  182. $list['ybData'] = $ybRes;
  183. $list['jsData'] = $jsRes;
  184. $this->success('请求成功',$list);
  185. }
  186. /**
  187. * 4.获取单个工艺数据(排产参数调整)
  188. *
  189. * @ApiMethod GET
  190. *@param string product_code
  191. */
  192. public function getProductGy(){
  193. if (Request::instance()->isGet() == false){
  194. $this->error('非法请求');
  195. }
  196. $params = Request::instance()->param();
  197. $code = $params['product_code'];
  198. if (!isset($code)){
  199. $this->error('参数不能为空');
  200. }
  201. $option['a.Gy0_cpdh'] = $code;
  202. if (!empty($params['plan'])){
  203. $option['a.Gy0_方案'] = $params['plan'];
  204. }
  205. $gy_field = 'rtrim(a.Gy0_方案) as programme,a.Gy0_yjno,a.Gy0_gxh,rtrim(a.gy0_gxmc) as gy0_gxmc,a.A类产能 as A_power,rtrim(a.Gy0_shdh) as Gy0_shdh,rtrim(a.Gy0_sbbh) as Gy0_sbbh,
  206. a.工价系数 as difficulty_coe,a.损耗系数 as loss_coe,a.Gy0_Ms as ms_coe,a.Gy0_Ks,a.Gy0_ls,rtrim(a.Gy0_site) as Gy0_site,rtrim(a.Add_gxmc) as Add_gxmc,a.UniqID,a.Gy0_辅助工时,
  207. rtrim(a.工序备注) as remark,a.人工检_正品板 as artificial_zp,a.人工检_次品板 as artificial_cp,a.人工检_废检 as artificial_fj,a.机检_正品板 as machine_zp,a.机检_次品板 as machine_cp,
  208. a.机检_废检 as machine_fj,rtrim(b.客户名称) as custom_name,rtrim(b.产品名称) as product_name,a.UniqId';
  209. $gyRes = db('产品_工艺资料')->alias('a')
  210. ->join('产品_基本资料 b','a.Gy0_cpdh = b.产品编号','left')
  211. ->where($option)->field($gy_field)->order('a.Gy0_yjno asc,a.Gy0_gxh asc')->select();
  212. $this->success('请求成功',$gyRes);
  213. }
  214. /**
  215. * 预览修改工艺参数(仅返回影响的工单列表,不执行修改)
  216. *
  217. * @ApiMethod POST
  218. * @params array data
  219. */
  220. public function editGyPreview(){
  221. if (Request::instance()->isPost() == false){
  222. $this->error('非法请求');
  223. }
  224. $params = Request::instance()->post();
  225. if (!isset($params) || !isset($params[0]['UniqID'])){
  226. $this->error('参数不能为空');
  227. }
  228. $affected = [];
  229. foreach ($params as $value) {
  230. $newSbbh = isset($value['machine']) ? trim((string)$value['machine']) : '';
  231. if ($newSbbh === '') continue;
  232. $gyInfo = \db('产品_工艺资料')->where('UniqID', $value['UniqID'])->field('Gy0_cpdh, Gy0_yjno, Gy0_gxh, Gy0_sbbh')->find();
  233. if (empty($gyInfo)) continue;
  234. $oldSbbh = trim($gyInfo['Gy0_sbbh']);
  235. if ($oldSbbh === $newSbbh) continue; // 没改机台跳过
  236. $cpdh = trim($gyInfo['Gy0_cpdh']);
  237. $yjno = trim($gyInfo['Gy0_yjno']);
  238. $gxh = trim($gyInfo['Gy0_gxh']);
  239. if ($cpdh === '' || $yjno === '' || $gxh === '') continue;
  240. $rows = \db('工单_工艺资料')
  241. ->alias('w')
  242. ->join('工单_基本资料 o', 'w.Gy0_gdbh = o.Gd_gdbh', 'left')
  243. ->where('w.Gy0_sj1', '1900-01-01 00:00:00')
  244. ->where('w.Gy0_yjno', $yjno)
  245. ->where('w.Gy0_gxh', $gxh)
  246. ->whereIn('w.Gy0_gdbh', function($query) use ($cpdh) {
  247. $query->table('工单_基本资料')->where('Gd_cpdh', $cpdh)->field('Gd_gdbh');
  248. })
  249. ->field('w.Gy0_gdbh as gdbh, w.Gy0_yjno as yjno, w.Gy0_gxh as gxh, rtrim(w.Gy0_sbbh) as old_sbbh, rtrim(o.Gd_cpdh) as cpdh')
  250. ->select();
  251. foreach ($rows as $r) {
  252. $affected[] = [
  253. 'gdbh' => $r['gdbh'],
  254. 'yjno' => $r['yjno'],
  255. 'gxh' => $r['gxh'],
  256. 'old_sbbh' => $r['old_sbbh'],
  257. 'new_sbbh' => $newSbbh,
  258. 'cpdh' => $r['cpdh'],
  259. ];
  260. }
  261. }
  262. $this->success('预览成功', $affected);
  263. }
  264. /**
  265. * 修改工艺参数
  266. *
  267. * @ApiMethod
  268. * @params array data
  269. */
  270. public function editGy(){
  271. if (Request::instance()->isPost() == false){
  272. $this->error('非法请求');
  273. }
  274. $params = Request::instance()->post();
  275. if (!isset($params) || !isset($params[0]['UniqID'])){
  276. $this->error('参数不能为空');
  277. }
  278. $operator = $this->getProductLogOperator($params);
  279. if ($operator === '' && isset($params[0])) {
  280. $operator = $this->getProductLogOperator($params[0]);
  281. }
  282. $i = 0;
  283. $productLogs = [];
  284. foreach ($params as $key=>$value){
  285. $data = [];
  286. if (!empty($value['A_power'])){
  287. $data['A类产能'] = $value['A_power'];
  288. }
  289. if (!empty($value['shdh'])){
  290. $data['Gy0_shdh'] = $value['shdh'];
  291. }
  292. if (!empty($value['machine'])){
  293. $data['Gy0_sbbh'] = $value['machine'];
  294. }
  295. if (!empty($value['time'])){
  296. $data['Gy0_辅助工时'] = $value['time'];
  297. }
  298. if (!empty($value['difficulty_coe'])){
  299. $data['工价系数'] = $value['difficulty_coe'];
  300. }
  301. if (!empty($value['loss_coe'])){
  302. $data['损耗系数'] = $value['loss_coe'];
  303. }
  304. if (!empty($value['ms_coe'])){
  305. $data['Gy0_Ms'] = $value['ms_coe'];
  306. }
  307. if (!empty($value['Gy0_site'])){
  308. $data['Gy0_site'] = $value['Gy0_site'];
  309. }
  310. $oldGy = \db('产品_工艺资料')->where('UniqID', $value['UniqID'])->find();
  311. // 如果修改了设备编号 → 同步未开工工单_工艺资料
  312. $newSbbh = isset($value['machine']) ? trim((string)$value['machine']) : '';
  313. $syncSql = '';
  314. if ($newSbbh !== '') {
  315. // 取产品_工艺资料的 Gy0_cpdh / Gy0_yjno / Gy0_gxh
  316. $gyInfo = $oldGy;
  317. if (!empty($gyInfo)) {
  318. $cpdh = trim($gyInfo['Gy0_cpdh']);
  319. $yjno = trim($gyInfo['Gy0_yjno']);
  320. $gxh = trim($gyInfo['Gy0_gxh']);
  321. if ($cpdh !== '' && $yjno !== '' && $gxh !== '') {
  322. $syncSql = \db('工单_工艺资料')
  323. ->where('Gy0_sj1', '1900-01-01 00:00:00')
  324. ->where('Gy0_yjno', $yjno)
  325. ->where('Gy0_gxh', $gxh)
  326. ->whereIn('Gy0_gdbh', function($query) use ($cpdh) {
  327. $query->table('工单_基本资料')->where('Gd_cpdh', $cpdh)->field('Gd_gdbh');
  328. })
  329. ->fetchSql(true)
  330. ->update(['Gy0_sbbh' => $newSbbh]);
  331. }
  332. }
  333. }
  334. $sql = db('产品_工艺资料')->where('UniqID',$value['UniqID'])->fetchSql(true)->update($data);
  335. $res = Db::query($sql);
  336. if ($res !== false){
  337. $i++;
  338. if (!empty($oldGy) && !empty($data)) {
  339. $productLogs = array_merge($productLogs, $this->buildProductFieldChangeLogs($oldGy, $data, [
  340. '产品编号' => isset($oldGy['Gy0_cpdh']) ? trim($oldGy['Gy0_cpdh']) : '',
  341. '印件号' => isset($oldGy['Gy0_yjno']) ? $oldGy['Gy0_yjno'] : '',
  342. '工序号' => isset($oldGy['Gy0_gxh']) ? $oldGy['Gy0_gxh'] : '',
  343. '方案' => isset($oldGy['Gy0_方案']) ? $oldGy['Gy0_方案'] : '',
  344. '业务表' => '产品_工艺资料',
  345. 'RecordUniqid' => $value['UniqID'],
  346. 'sys_id' => $operator,
  347. ]));
  348. }
  349. }
  350. // 执行级联同步(不影响主更新的成功判定)
  351. if ($syncSql !== '') {
  352. Db::execute($syncSql);
  353. $productLogs[] = [
  354. '产品编号' => !empty($oldGy['Gy0_cpdh']) ? trim($oldGy['Gy0_cpdh']) : '',
  355. '印件号' => isset($oldGy['Gy0_yjno']) ? $oldGy['Gy0_yjno'] : '',
  356. '工序号' => isset($oldGy['Gy0_gxh']) ? $oldGy['Gy0_gxh'] : '',
  357. '方案' => isset($oldGy['Gy0_方案']) ? $oldGy['Gy0_方案'] : '',
  358. '业务表' => '工单_工艺资料',
  359. 'RecordUniqid' => $value['UniqID'],
  360. '操作类型' => '修改',
  361. '字段名' => 'Gy0_sbbh',
  362. '修改后' => $newSbbh,
  363. 'sys_id' => $operator,
  364. '备注' => '同步未开工工单机台',
  365. ];
  366. }
  367. }
  368. $this->saveProductOperateLog($productLogs);
  369. if ($i !== 0){
  370. $this->success('更新成功');
  371. }else{
  372. $this->error('更新失败');
  373. }
  374. }
  375. /**
  376. * 修改产品信息
  377. *
  378. * @ApiMethod POST
  379. *
  380. * @params object data
  381. */
  382. public function editProduct(){
  383. if (Request::instance()->isPost() == false){
  384. $this->error('非法请求');
  385. }
  386. $params = Request::instance()->post();
  387. if (empty($params)){
  388. $this->error('参数不能为空');
  389. }
  390. if (!isset($params['印品代号'])){
  391. $this->error('印品代号不能为空');
  392. }
  393. $code = $params['印品代号'];
  394. unset($params['印品代号']);
  395. $operator = $this->getProductLogOperator($params);
  396. unset($params['sys_id']);
  397. $oldProduct = db('产品_基本资料')->where('产品编号',$code)->find();
  398. $sql = db('产品_基本资料')->where('产品编号',$code)->fetchSql(true)->update($params);
  399. $res = Db::query($sql);
  400. if ($res !== false){
  401. if (!empty($oldProduct)) {
  402. $this->saveProductOperateLog($this->buildProductFieldChangeLogs($oldProduct, $params, [
  403. '产品编号' => $code,
  404. '产品名称' => isset($oldProduct['产品名称']) ? $oldProduct['产品名称'] : '',
  405. '业务表' => '产品_基本资料',
  406. 'RecordUniqid' => isset($oldProduct['UniqID']) ? $oldProduct['UniqID'] : '',
  407. 'sys_id' => $operator,
  408. ]));
  409. }
  410. $this->success('更新成功');
  411. }else{
  412. $this->error('更新失败');
  413. }
  414. }
  415. /**
  416. * 设置产品状态
  417. *
  418. * @ApiMethod POST
  419. * @params string status
  420. * @params string code
  421. */
  422. public function setProductStatus(){
  423. if (Request::instance()->isPost() == false){
  424. $this->error('非法请求');
  425. }
  426. $params = Request::instance()->post();
  427. if (empty($params['code']) || empty($params['status'])){
  428. $this->error('参数不能为空');
  429. }
  430. $code = $params['code'];
  431. $status = '';
  432. if ($params['status'] == 2){
  433. $status = '停产';
  434. }
  435. $operator = $this->getProductLogOperator($params);
  436. $oldProduct = db('产品_基本资料')->where('产品编号',$code)->field('UniqID,产品名称,状态')->find();
  437. $sql = db('产品_基本资料')->where('产品编号',$code)->fetchSql(true)->setField('状态',$status);
  438. $res = Db::query($sql);
  439. if ($res !== false){
  440. $this->saveProductOperateLog([[
  441. '产品编号' => $code,
  442. '产品名称' => !empty($oldProduct['产品名称']) ? $oldProduct['产品名称'] : '',
  443. '业务表' => '产品_基本资料',
  444. 'RecordUniqid' => !empty($oldProduct['UniqID']) ? $oldProduct['UniqID'] : '',
  445. '操作类型' => '停产',
  446. '字段名' => '状态',
  447. '修改前' => !empty($oldProduct['状态']) ? $oldProduct['状态'] : '',
  448. '修改后' => $status,
  449. 'sys_id' => $operator,
  450. ]]);
  451. $this->success('更新成功');
  452. }else{
  453. $this->error('更新失败');
  454. }
  455. }
  456. /**
  457. * 获取产品工艺数量
  458. *
  459. * @ApiMethod GET
  460. * @params string code
  461. */
  462. public function getGyTotal(){
  463. if (Request::instance()->isGet() == false){
  464. $this->error('非法请求');
  465. }
  466. $params = Request::instance()->param();
  467. $code = $params['code'];
  468. if (!isset($code)){
  469. $this->error('参数不能为空');
  470. }
  471. $res = db('产品_工艺资料')->where('Gy0_cpdh',$code)->distinct(true)->column('rtrim(Gy0_方案) as gy_plan');
  472. $data['gy'] = $res;
  473. $product = db('产品_基本资料')->where('产品编号',$code)->find();
  474. $data['yj'] = db('产品_印件资料')->where('yj_cpdh',$code)->column('yj_yjno');
  475. $data['name'] = rtrim($product['产品名称']);
  476. $this->success('请求成功',$data);
  477. }
  478. /**
  479. * 复制产品工艺信息
  480. *
  481. * @ApiMethod POST
  482. * @params string from_code
  483. * @params string from_pro
  484. * @params string to_code
  485. * @params string to_pro
  486. * @params int is_copy_gy
  487. */
  488. public function copyProductGy()
  489. {
  490. $request = Request::instance();
  491. if (!$request->isPost()) {
  492. $this->error('非法请求');
  493. }
  494. $params = $request->post();
  495. if (empty($params['from_code']) || empty($params['to_code'])) {
  496. $this->error('参数不能为空');
  497. }
  498. $where = [];
  499. if (isset($params['type']) && $params['type'] == 1) {
  500. $where = ['Gy0_gxh' => ['>', 20]];
  501. }
  502. $sys_id = $params['sys_id'];
  503. $mod_rq = date('Y-m-d H:i:s', time());
  504. // 定义数据处理策略(统一添加 Sys_id 和 Mod_rq 字段)
  505. $strategies = [
  506. 'gy' => [
  507. 'flag' => 'is_copy_gy',
  508. 'table' => '产品_工艺资料',
  509. 'source_field' => 'Gy0_cpdh',
  510. 'uniq_field' => 'UniqID',
  511. 'extra_check' => function () use ($params) {
  512. if (empty($params['to_pro'])) {
  513. $this->error('工艺方案不能为空');
  514. }
  515. },
  516. 'data_mapper' => function ($item, $seq) use ($params, $sys_id, $mod_rq) {
  517. return [
  518. 'Gy0_方案' => $params['to_pro'],
  519. 'Gy0_cpdh' => $params['to_code'],
  520. 'UniqID' => $seq,
  521. 'Sys_id' => $sys_id, // 新增
  522. 'Mod_rq' => $mod_rq // 新增
  523. ];
  524. }
  525. ],
  526. 'yj' => [
  527. 'flag' => 'is_copy_yj',
  528. 'table' => '产品_印件资料',
  529. 'source_field' => 'yj_cpdh',
  530. 'uniq_field' => 'UniqId',
  531. 'data_mapper' => function ($item, $seq) use ($params, $sys_id, $mod_rq) {
  532. return [
  533. 'yj_cpdh' => $params['to_code'],
  534. 'UniqId' => $seq,
  535. 'sys_id' => $sys_id, // 新增
  536. 'mod_rq' => $mod_rq // 新增
  537. ];
  538. }
  539. ],
  540. 'yb' => [
  541. 'flag' => 'is_copy_yb',
  542. 'table' => '产品_印版资料',
  543. 'source_field' => 'YB_Cpdh',
  544. 'uniq_field' => 'UniqID',
  545. 'data_mapper' => function ($item, $seq) use ($params, $sys_id, $mod_rq) {
  546. return [
  547. 'YB_Cpdh' => $params['to_code'],
  548. 'UniqID' => $seq,
  549. 'Sys_id' => $sys_id, // 新增
  550. 'Mod_rq' => $mod_rq // 新增
  551. ];
  552. }
  553. ]
  554. ];
  555. $processed = false;
  556. foreach ($strategies as $key => $strategy) {
  557. if (($params[$strategy['flag']] ?? 0) != 1) continue;
  558. $processed = true;
  559. if (isset($strategy['extra_check'])) {
  560. call_user_func($strategy['extra_check']);
  561. }
  562. // 构建查询条件
  563. $query = db($strategy['table'])->where($strategy['source_field'], $params['from_code']);
  564. if ($key === 'gy') {
  565. $query->where($where)->where('Gy0_方案', $params['from_pro']);
  566. }
  567. $data = $query->select();
  568. if (empty($data)) {
  569. $this->error("参考产品无{$strategy['table']}数据");
  570. }
  571. // 生成序列号
  572. $maxId = db($strategy['table'])
  573. ->order("{$strategy['uniq_field']} desc")
  574. ->value($strategy['uniq_field']) ?: 0;
  575. // 处理数据(自动覆盖原记录的 Sys_id 和 Mod_rq)
  576. $processedData = [];
  577. $currentSeq = 1;
  578. foreach ($data as $item) {
  579. unset($item[$strategy['uniq_field']]);
  580. $mapped = array_merge(
  581. $item,
  582. call_user_func($strategy['data_mapper'], $item, $maxId + $currentSeq)
  583. );
  584. $processedData[] = $mapped;
  585. $currentSeq++;
  586. }
  587. // 事务处理
  588. db()->startTrans();
  589. try {
  590. // db($strategy['table'])
  591. // ->where($strategy['source_field'], $params['to_code'])
  592. // ->delete();
  593. if (!db($strategy['table'])->insertAll($processedData)) {
  594. throw new \Exception("Insert failed");
  595. }
  596. db()->commit();
  597. $this->saveProductOperateLog([[
  598. '产品编号' => $params['to_code'],
  599. '方案' => isset($params['to_pro']) ? $params['to_pro'] : '',
  600. '业务表' => $strategy['table'],
  601. '操作类型' => '复制',
  602. 'sys_id' => $sys_id,
  603. '备注' => '从'.$params['from_code'].'复制'.count($processedData).'条',
  604. ]]);
  605. } catch (\Exception $e) {
  606. db()->rollback();
  607. $this->error("复制{$strategy['table']}数据失败: " . $e->getMessage());
  608. }
  609. }
  610. if (!$processed) {
  611. $this->error('请至少选择一个复制项目');
  612. }
  613. $this->success('数据复制成功');
  614. }
  615. /**
  616. * 工艺方案更名
  617. *
  618. * @ApiMethod POST
  619. * @params string code
  620. * @params string name
  621. */
  622. public function editGyName(){
  623. if (Request::instance()->isPost() == false){
  624. $this->error('非法请求');
  625. }
  626. $params = Request::instance()->post();
  627. if (empty($params['code']) || empty($params['old_name']) || empty($params['new_name'])){
  628. $this->error('参数不能为空');
  629. }
  630. $where['Gy0_cpdh'] = $params['code'];
  631. $where['Gy0_方案'] = $params['old_name'];
  632. $sql = db('产品_工艺资料')->where($where)->fetchSql(true)->setField('Gy0_方案',$params['new_name']);
  633. $res = Db::query($sql);
  634. if ($res !== false){
  635. $this->saveProductOperateLog([[
  636. '产品编号' => $params['code'],
  637. '方案' => $params['new_name'],
  638. '业务表' => '产品_工艺资料',
  639. '操作类型' => '修改',
  640. '字段名' => 'Gy0_方案',
  641. '修改前' => $params['old_name'],
  642. '修改后' => $params['new_name'],
  643. 'sys_id' => $this->getProductLogOperator($params),
  644. '备注' => '工艺方案更名',
  645. ]]);
  646. $this->success('更新成功');
  647. }else{
  648. $this->error('更新失败');
  649. }
  650. }
  651. /**
  652. * 工艺方案附加
  653. *
  654. * @ApiMethod POST
  655. * @params object data
  656. */
  657. public function editGyNo(){
  658. if (Request::instance()->isPost() == false){
  659. $this->error('非法请求');
  660. }
  661. $params = Request::instance()->post();
  662. if (empty($params) || !isset($params[0]['UniqID'])){
  663. $this->error('参数不能为空');
  664. }
  665. $operator = $this->pickProductLogOperator($params);
  666. $i = 0;
  667. $productLogs = [];
  668. foreach ($params as $key=>$value){
  669. $data = [];
  670. if (!empty($value['Gy0_yjno'])){
  671. $data['Gy0_yjno'] = $value['Gy0_yjno'];
  672. }
  673. if (!empty($value['Gy0_gxh'])){
  674. $data['Gy0_gxh'] = $value['Gy0_gxh'];
  675. }
  676. if (!empty($value['Gy0_Ks'])){
  677. $data['Gy0_Ks'] = $value['Gy0_Ks'];
  678. }
  679. if (!empty($value['Gy0_ls'])){
  680. $data['Gy0_ls'] = $value['Gy0_ls'];
  681. }
  682. $oldGy = \db('产品_工艺资料')->where('UniqID',$value['UniqID'])->find();
  683. $sql = db('产品_工艺资料')->where('UniqID',$value['UniqID'])->fetchSql(true)->update($data);
  684. $res = Db::query($sql);
  685. if ($res !== false){
  686. $i++;
  687. if (!empty($oldGy)) {
  688. $productLogs = array_merge($productLogs, $this->buildProductFieldChangeLogs($oldGy, $data, $this->gyLogBase($oldGy, $value['UniqID'], $operator)));
  689. }
  690. }
  691. }
  692. $this->saveProductOperateLog($productLogs);
  693. if ($i !== 0){
  694. $this->success('更新成功');
  695. }else{
  696. $this->error('更新失败');
  697. }
  698. }
  699. /**
  700. * 计损色数修正
  701. *
  702. * @ApiMethod POST
  703. * @params object data
  704. */
  705. public function editGyMs(){
  706. if (Request::instance()->isPost() == false){
  707. $this->error('非法请求');
  708. }
  709. $params = Request::instance()->post();
  710. if (empty($params) || !isset($params[0]['UniqID'])){
  711. $this->error('参数不能为空');
  712. }
  713. $operator = $this->pickProductLogOperator($params);
  714. $i = 0;
  715. $productLogs = [];
  716. foreach ($params as $key=>$value){
  717. $data = [];
  718. if (!empty($value['Gy0_Ms'])){
  719. $data['Gy0_Ms'] = $value['Gy0_Ms'];
  720. }
  721. if (!empty($value['Gy0_Ks'])){
  722. $data['Gy0_Ks'] = $value['Gy0_Ks'];
  723. }
  724. if (!empty($value['Gy0_ls'])){
  725. $data['Gy0_ls'] = $value['Gy0_ls'];
  726. }
  727. $data['Add_gxmc'] = $value['Add_gxmc'];
  728. $data['工序备注'] = $value['remark'];
  729. $oldGy = \db('产品_工艺资料')->where('UniqID',$value['UniqID'])->find();
  730. $sql = db('产品_工艺资料')->where('UniqID',$value['UniqID'])->fetchSql(true)->update($data);
  731. $res = Db::query($sql);
  732. if ($res !== false){
  733. $i++;
  734. if (!empty($oldGy)) {
  735. $productLogs = array_merge($productLogs, $this->buildProductFieldChangeLogs($oldGy, $data, $this->gyLogBase($oldGy, $value['UniqID'], $operator)));
  736. }
  737. }
  738. }
  739. $this->saveProductOperateLog($productLogs);
  740. if ($i !== 0){
  741. $this->success('更新成功');
  742. }else{
  743. $this->error('更新失败');
  744. }
  745. }
  746. /**
  747. * 产品质检废品系数调整
  748. *
  749. * @ApiMethod POST
  750. * @params object data
  751. */
  752. public function editGyWaste(){
  753. if (Request::instance()->isPost() == false){
  754. $this->error('非法请求');
  755. }
  756. $params = Request::instance()->post();
  757. if (empty($params) || !isset($params[0]['UniqID'])){
  758. $this->error('参数不能为空');
  759. }
  760. $operator = $this->pickProductLogOperator($params);
  761. $i = 0;
  762. $productLogs = [];
  763. foreach ($params as $key=>$value){
  764. $data = [];
  765. if (!empty($value['artificial_zp'])){
  766. $data['人工检_正品板'] = $value['artificial_zp'];
  767. }
  768. if (!empty($value['artificial_cp'])){
  769. $data['人工检_次品板'] = $value['artificial_cp'];
  770. }
  771. if (!empty($value['artificial_fj'])){
  772. $data['人工检_废检'] = $value['artificial_fj'];
  773. }
  774. if (!empty($value['machine_zp'])){
  775. $data['机检_正品板'] = $value['machine_zp'];
  776. }
  777. if (!empty($value['machine_cp'])){
  778. $data['机检_次品板'] = $value['machine_cp'];
  779. }
  780. if (!empty($value['machine_fj'])){
  781. $data['机检_废检'] = $value['machine_fj'];
  782. }
  783. $oldGy = \db('产品_工艺资料')->where('UniqID',$value['UniqID'])->find();
  784. $sql = db('产品_工艺资料')->where('UniqID',$value['UniqID'])->fetchSql(true)->update($data);
  785. $res = Db::query($sql);
  786. if ($res !== false){
  787. $i++;
  788. if (!empty($oldGy)) {
  789. $productLogs = array_merge($productLogs, $this->buildProductFieldChangeLogs($oldGy, $data, $this->gyLogBase($oldGy, $value['UniqID'], $operator)));
  790. }
  791. }
  792. }
  793. $this->saveProductOperateLog($productLogs);
  794. if ($i !== 0){
  795. $this->success('更新成功');
  796. }else{
  797. $this->error('更新失败');
  798. }
  799. }
  800. /**
  801. * 获取产品印件资料
  802. * @ApiMethod GET
  803. * @params string UniqId
  804. */
  805. public function getProductYjInfo(){
  806. if (Request::instance()->isGet() == false){
  807. $this->error('非法请求');
  808. }
  809. $params = Request::instance()->param();
  810. if (empty($params['UniqId']) || empty($params['UniqId'])){
  811. $this->error('参数错误');
  812. }
  813. $field = "yj_yjno,rtrim(yj_yjdh) as yj_yjdh,rtrim(yj_yjmc) as yj_yjmc,rtrim(yj_zzdh) as yj_zzdh,rtrim(yj_zzmc) as yj_zzmc,rtrim(yj_zzdh1) as yj_zzdh1,rtrim(yj_zzdh2) as yj_zzdh2,
  814. rtrim(yj_zzdh3) as yj_zzdh3,rtrim(yj_zzdh4) as yj_zzdh4,rtrim(yj_zzmc1) as yj_zzmc1,rtrim(yj_zzmc2) as yj_zzmc2,rtrim(yj_zzmc3) as yj_zzmc3,
  815. rtrim(yj_zzmc4) as yj_zzmc4,rtrim(yj_tlgg) as yj_tlgg,rtrim(yj_klgg) as yj_klgg,yj_ks,yj_ls,KgToPages,rtrim(yj_desc) as yj_desc,UniqId";
  816. $list = \db('产品_印件资料')->where('UniqId',$params['UniqId'])->field($field)->select();
  817. $this->success('请求成功',$list);
  818. }
  819. /**
  820. * 修改产品印件资料
  821. * @ApiMethod POST
  822. * @params array data
  823. */
  824. public function editProductYjInfo(){
  825. if (Request::instance()->isPost() == false){
  826. $this->error('非法请求');
  827. }
  828. $params = Request::instance()->post();
  829. if (empty($params) || !isset($params['UniqId'])){
  830. $this->error('参数不能为空');
  831. }
  832. $UniqId = $params['UniqId'];
  833. $operator = $this->getProductLogOperator($params);
  834. unset($params['UniqId']);
  835. $oldYj = \db('产品_印件资料')->where('UniqId',$UniqId)->find();
  836. $res = \db('产品_印件资料')->where('UniqId',$UniqId)->update($params);
  837. if ($res !== false){
  838. if (!empty($oldYj)) {
  839. $this->saveProductOperateLog($this->buildProductFieldChangeLogs($oldYj, $params, [
  840. '产品编号' => isset($oldYj['yj_cpdh']) ? $oldYj['yj_cpdh'] : '',
  841. '产品名称' => isset($oldYj['yj_yjmc']) ? $oldYj['yj_yjmc'] : '',
  842. '印件号' => isset($params['yj_yjno']) ? $params['yj_yjno'] : (isset($oldYj['yj_yjno']) ? $oldYj['yj_yjno'] : ''),
  843. '业务表' => '产品_印件资料',
  844. 'RecordUniqid' => $UniqId,
  845. 'sys_id' => $operator,
  846. ]));
  847. }
  848. $this->success('更新成功');
  849. }else{
  850. $this->error('更新失败');
  851. }
  852. }
  853. /**
  854. * 新增产品印件资料
  855. * @ApiMethod POST
  856. * @params array data
  857. */
  858. public function addProductYjInfo(){
  859. if (Request::instance()->isPost() == false){
  860. $this->error('非法请求');
  861. }
  862. $params = Request::instance()->post();
  863. $UniqId = \db('产品_印件资料')->order('UniqId desc')->value('UniqId');
  864. if ($UniqId < 2000000){
  865. $UniqId = 2000000;
  866. }else{
  867. $UniqId = $UniqId + 1;
  868. }
  869. $params['UniqId'] = $UniqId;
  870. $params['sys_rq'] = date('Y-m-d H:i:s');
  871. $operator = $this->getProductLogOperator($params);
  872. $res = \db('产品_印件资料')->insert($params);
  873. if ($res !== false){
  874. $this->saveProductOperateLog([[
  875. '产品编号' => isset($params['yj_cpdh']) ? $params['yj_cpdh'] : '',
  876. '产品名称' => isset($params['yj_yjmc']) ? $params['yj_yjmc'] : '',
  877. '印件号' => isset($params['yj_yjno']) ? $params['yj_yjno'] : '',
  878. '业务表' => '产品_印件资料',
  879. 'RecordUniqid' => $UniqId,
  880. '操作类型' => '新增',
  881. 'sys_id' => $operator,
  882. ]]);
  883. $this->success('新增成功');
  884. }else{
  885. $this->error('新增失败');
  886. }
  887. }
  888. /**
  889. * 获取印件代码及名称
  890. * @ApiMethod GET
  891. *
  892. */
  893. public function getProductYjList(){
  894. if (Request::instance()->isGet() == false){
  895. $this->error('非法请求');
  896. }
  897. $params = Request::instance()->get();
  898. $search = $params['search'];
  899. // $num = config('product_code_digit');
  900. $num = 5;
  901. if (!empty($search)){
  902. $sql = "SELECT DISTINCT
  903. rtrim(a.`物料代码`) AS `物料代码`,
  904. rtrim(a.`物料名称`) AS `物料名称`,
  905. rtrim(b.`客户编号`) AS `客户编号`,
  906. rtrim(b.`客户名称`) AS `客户名称`
  907. FROM
  908. `物料_存货编码` a
  909. JOIN `产品_基本资料` b ON LEFT(a.`物料代码`,
  910. CASE
  911. WHEN a.`物料代码` REGEXP '[a-zA-Z]' THEN 5
  912. ELSE 4
  913. END
  914. ) = LEFT(b.`客户编号`,
  915. CASE
  916. WHEN a.`物料代码` REGEXP '[a-zA-Z]' THEN 5
  917. ELSE 4
  918. END
  919. )
  920. WHERE
  921. (a.`物料名称` LIKE '%{$search}%' OR a.`物料代码` LIKE '%{$search}%')
  922. GROUP BY
  923. a.`物料代码`
  924. ORDER BY
  925. a.`物料代码` ASC ";
  926. }else{
  927. $sql = "SELECT DISTINCT
  928. rtrim(a.`物料代码`) AS `物料代码`,
  929. rtrim(a.`物料名称`) AS `物料名称`,
  930. rtrim(b.`客户编号`) AS `客户编号`,
  931. rtrim(b.`客户名称`) AS `客户名称`
  932. FROM
  933. `物料_存货编码` a
  934. JOIN `产品_基本资料` b ON LEFT(a.`物料代码`,
  935. CASE
  936. WHEN a.`物料代码` REGEXP '[a-zA-Z]' THEN 5
  937. ELSE 4
  938. END
  939. ) = LEFT(b.`客户编号`,
  940. CASE
  941. WHEN a.`物料代码` REGEXP '[a-zA-Z]' THEN 5
  942. ELSE 4
  943. END
  944. )
  945. ORDER BY
  946. a.`物料代码` ASC";
  947. }
  948. $data = Db::query($sql);
  949. // 初始化一个关联数组,用于存储相同客户编号的数据
  950. $groupedData = [];
  951. foreach ($data as $row) {
  952. $customerCode = substr($row['物料代码'],0,$num).substr($row['客户编号'],2,2).'/'.$row['客户名称'];
  953. $materialCodePrefix = substr($row['物料代码'], 0, $num);
  954. if ($materialCodePrefix == 'Y1401' ){
  955. $materialCodePrefix = $materialCodePrefix.'/糊盒类产品(含贴码)';
  956. }else{
  957. $materialCodePrefix = $materialCodePrefix.'/直接领用产品';
  958. }
  959. // 如果关联数组中不存在该物料代码前四位的键,则创建一个空数组
  960. if (!isset($groupedData[$materialCodePrefix])) {
  961. $groupedData[$materialCodePrefix] = [];
  962. }
  963. // 如果物料代码前四位数组中不存在该客户编号的键,则创建一个空数组
  964. if (!isset($groupedData[$materialCodePrefix][$customerCode])) {
  965. $groupedData[$materialCodePrefix][$customerCode] = [];
  966. }
  967. // 去除客户编号和客户名称
  968. unset($row['客户编号']);
  969. unset($row['客户名称']);
  970. // 将当前行的数据添加到相应的物料代码前四位和客户编号的数组中
  971. $groupedData[$materialCodePrefix][$customerCode][] = $row;
  972. }
  973. $this->success('请求成功',$groupedData);
  974. }
  975. /**
  976. * 获取纸张代号及名称
  977. * @ApiMethod GET
  978. * @params string search
  979. */
  980. public function getProductZzList(){
  981. if (Request::instance()->isGet() == false){
  982. $this->error('非法请求');
  983. }
  984. $params = Request::instance()->get();
  985. $search = $params['search'];
  986. $num = config('product_code_digit');
  987. if (!empty($search)){
  988. $sql = "SELECT rtrim(a.`物料代码`) as 物料代码,rtrim(a.`物料名称`) as 物料名称,rtrim(a.`规格`) as 规格,rtrim(b.编号) as oneCode,rtrim(b.名称) as oneName,
  989. rtrim(c.`编号`) as twoCode,rtrim(c.`名称`) as twoName,rtrim(d.`编号`) as thrCode,rtrim(d.`名称`) as thrName
  990. FROM `物料_存货编码` a
  991. LEFT JOIN `物料_存货结构` b ON LEFT(a.物料代码,$num-2) = b.编号
  992. LEFT JOIN `物料_存货结构` c ON LEFT(a.物料代码,$num) = c.编号
  993. LEFT JOIN `物料_存货结构` d ON LEFT(a.物料代码,$num+2) = d.编号
  994. WHERE (a.物料名称 LIKE '%{$search}%' or a.物料代码 LIKE '%{$search}%') AND (
  995. a.物料代码 LIKE '00%'
  996. OR a.物料代码 LIKE '01%'
  997. OR a.物料代码 LIKE '04%'
  998. OR a.物料代码 LIKE '03%'
  999. OR a.物料代码 LIKE '14%'
  1000. OR a.物料代码 LIKE 'Y00%'
  1001. OR a.物料代码 LIKE 'Y01%'
  1002. OR a.物料代码 LIKE 'Y04%'
  1003. OR a.物料代码 LIKE 'J03%'
  1004. OR a.物料代码 LIKE 'Y14%')";
  1005. $data = Db::query($sql);
  1006. }else{
  1007. $sql = "SELECT rtrim(a.`物料代码`) as 物料代码,rtrim(a.`物料名称`) as 物料名称,rtrim(a.`规格`) as 规格,rtrim(b.编号) as oneCode,rtrim(b.名称) as oneName,
  1008. rtrim(c.`编号`) as twoCode,rtrim(c.`名称`) as twoName,rtrim(d.`编号`) as thrCode,rtrim(d.`名称`) as thrName
  1009. FROM `物料_存货编码` a
  1010. LEFT JOIN `物料_存货结构` b ON LEFT(a.物料代码,$num-2) = b.编号
  1011. LEFT JOIN `物料_存货结构` c ON LEFT(a.物料代码,$num) = c.编号
  1012. LEFT JOIN `物料_存货结构` d ON LEFT(a.物料代码,$num+2) = d.编号
  1013. WHERE a.物料代码 LIKE 'Y00%' or a.物料代码 LIKE 'Y01%' or a.物料代码 LIKE 'Y04%' or a.物料代码 LIKE 'J03%' or a.物料代码 LIKE 'Y14%' OR
  1014. a.物料代码 LIKE '00%' or a.物料代码 LIKE '01%' or a.物料代码 LIKE '04%' or a.物料代码 LIKE '03%' or a.物料代码 LIKE '14%'";
  1015. $data = Db::query($sql);
  1016. }
  1017. $mergedArray = [];
  1018. foreach ($data as $item) {
  1019. $oneCode = $item['oneCode'];
  1020. $twoCode = $item['twoCode'];
  1021. $thrCode = $item['thrCode'];
  1022. $oneName = $item['oneName'];
  1023. $twoName = $item['twoName'];
  1024. $thrName = $item['thrName'];
  1025. // Create a unique key using the combination of oneCode, twoCode, and thrCode
  1026. $oneKey = "{$oneCode}/{$oneName}";
  1027. $twoKey = "{$twoCode}/{$twoName}";
  1028. $thrKey = "{$thrCode}/{$thrName}";
  1029. // Initialize arrays if not already set
  1030. if (!isset($mergedArray[$oneKey])) {
  1031. $mergedArray[$oneKey] = [];
  1032. }
  1033. if (!isset($mergedArray[$oneKey][$twoKey])) {
  1034. $mergedArray[$oneKey][$twoKey] = [];
  1035. }
  1036. // Append items to the arrays
  1037. $mergedArray[$oneKey][$twoKey][$thrKey][] = $item;
  1038. }
  1039. $this->success('请求成功',$mergedArray);
  1040. }
  1041. /**
  1042. *3.6工艺资料-获取产品工艺资料
  1043. */
  1044. public function getProductGyInfo(){
  1045. if (Request::instance()->isGet() == false){
  1046. $this->error('非法请求');
  1047. }
  1048. $params = Request::instance()->param();
  1049. if (empty($params['UniqID']) || empty($params['UniqID'])){
  1050. $this->error('参数错误');
  1051. }
  1052. $field = "rtrim(a.Gy0_方案) as Gy0_方案,a.Gy0_yjno,a.Gy0_gxh,a.Gy0_Ks,a.Gy0_ls,rtrim(a.Gy0_site) as Gy0_site,rtrim(a.gy0_gxmc) as gy0_gxmc,rtrim(a.Add_gxmc) as Add_gxmc,a.Gy0_Ms,
  1053. rtrim(a.Gy0_sbbh) as Gy0_sbbh,rtrim(a.Gy0_shdh) as Gy0_shdh,rtrim(b.sys_mc) as sys_mc,b.sys_rate0,b.sys_rate1,a.工价系数,a.损耗系数,rtrim(a.工序备注) as 工序备注,
  1054. rtrim(a.质量要求) as 质量要求,rtrim(a.质量隐患) as 质量隐患,a.UniqID";
  1055. $data = \db('产品_工艺资料')->alias('a')
  1056. ->join('dic_lzsh b','a.Gy0_shdh = b.sys_bh','left')
  1057. ->where('a.UniqID',$params['UniqID'])
  1058. ->field($field)
  1059. ->find();
  1060. $this->success('请求成功',$data);
  1061. }
  1062. /**
  1063. * 3.7工艺资料-获取车间及工艺名称
  1064. * @ApiMethod GET
  1065. *
  1066. */
  1067. public function getDepartName(){
  1068. if (Request::instance()->isGet() == false){
  1069. $this->error('非法请求');
  1070. }
  1071. $param = $this->request->param();
  1072. $data = \db('erp_常用字典')->where('分类','印刷工艺')->order('编号 asc')->column('名称');
  1073. $resultArray = [];
  1074. foreach ($data as $item) {
  1075. $parts = explode('_', $item);
  1076. // 使用第一个部分作为一级键,第二个部分作为二级键
  1077. $firstKey = $parts[0];
  1078. $secondKey = $parts[1];
  1079. $thirdValue = $parts[2];
  1080. $resultArray[$firstKey][$secondKey][] = $thirdValue;
  1081. }
  1082. // if (isset($param['sort'])){
  1083. // $result['标准工艺']['智能车间'] = $resultArray['标准工艺']['智能车间'];
  1084. // }else{
  1085. // unset($resultArray['标准工艺']['智能车间']);
  1086. $result = $resultArray;
  1087. // }
  1088. $this->success('请求成功',$result);
  1089. }
  1090. /**
  1091. * 3.8工艺资料-新增产品工艺
  1092. */
  1093. public function addProductGyInfo(){
  1094. if (Request::instance()->isPost() == false){
  1095. $this->error('非法请求');
  1096. }
  1097. $params = Request::instance()->post();
  1098. $UniqId = \db('产品_工艺资料')->order('UniqID desc')->value('UniqID');
  1099. if ($UniqId < 2000000){
  1100. $UniqId = 2000000;
  1101. }else{
  1102. $UniqId = $UniqId + 1;
  1103. }
  1104. $params['UniqID'] = $UniqId;
  1105. $params['Sys_rq'] = date('Y-m-d H:i:s');
  1106. $operator = $this->getProductLogOperator($params);
  1107. $sql = \db('产品_工艺资料')->fetchSql(true)->insert($params);
  1108. $res = Db::query($sql);
  1109. if ($res !== false){
  1110. $this->saveProductOperateLog([[
  1111. '产品编号' => isset($params['Gy0_cpdh']) ? $params['Gy0_cpdh'] : '',
  1112. '印件号' => isset($params['Gy0_yjno']) ? $params['Gy0_yjno'] : '',
  1113. '工序号' => isset($params['Gy0_gxh']) ? $params['Gy0_gxh'] : '',
  1114. '方案' => isset($params['Gy0_方案']) ? $params['Gy0_方案'] : '',
  1115. '业务表' => '产品_工艺资料',
  1116. 'RecordUniqid' => $UniqId,
  1117. '操作类型' => '新增',
  1118. 'sys_id' => $operator,
  1119. ]]);
  1120. $this->success('新增成功');
  1121. }else{
  1122. $this->error('新增失败');
  1123. }
  1124. }
  1125. /**
  1126. * 3.9印版资料-获取产品印版资料
  1127. * @ApiMethod GET
  1128. */
  1129. public function getProductYbInfo(){
  1130. if (Request::instance()->isGet() == false){
  1131. $this->error('非法请求');
  1132. }
  1133. $params = Request::instance()->param();
  1134. if (empty($params['UniqID']) || empty($params['UniqID'])){
  1135. $this->error('参数错误');
  1136. }
  1137. $field = "rtrim(a.YB_方案) as YB_方案,a.YB_Yjno,a.YB_gxh,rtrim(a.存货编码) as 存货编码,rtrim(a.印版名称) as 印版名称,a.UniqID,rtrim(b.gy0_gxmc) as gy0_gxmc,
  1138. rtrim(b.Add_gxmc) as Add_gxmc,rtrim(c.物料名称) as 物料名称,rtrim(a.YB_cpdh) as YB_cpdh";
  1139. $data = \db('产品_印版资料')->alias('a')
  1140. ->join('产品_工艺资料 b','a.YB_cpdh = b.Gy0_cpdh and a.YB_Yjno = b.Gy0_yjno and a.YB_gxh = b.Gy0_gxh','left')
  1141. ->join('物料_存货编码 c','a.存货编码 = c.物料代码','left')
  1142. ->where('a.UniqID',$params['UniqID'])->field($field)->order('存货编码')->find();
  1143. if (empty($data)){
  1144. $this->error('未查询到产品印版资料');
  1145. }
  1146. $where['Gy0_site'] = [
  1147. ['like','胶印%'],
  1148. ['like','烫模%'],
  1149. 'or'
  1150. ];
  1151. $option['Gy0_cpdh'] = $data['YB_cpdh'];
  1152. $option['Gy0_gxh'] = ['<',10];
  1153. $option['Gy0_sbbh'] = ['neq',''];
  1154. $gyData = \db('产品_工艺资料')->where($option)->where($where)
  1155. ->field('rtrim(Gy0_方案) as Gy0_方案,Gy0_yjno,Gy0_gxh,rtrim(gy0_gxmc) as gy0_gxmc,rtrim(Add_gxmc) as Add_gxmc')
  1156. ->order('Gy0_yjno,Gy0_gxh')->select();
  1157. $list = [];
  1158. foreach ($gyData as $key=>$value){
  1159. $yjno = $value['Gy0_yjno'] > 10 ? $value['Gy0_yjno'] : '0'.$value['Gy0_yjno'];
  1160. $gxh = $value['Gy0_gxh'] > 10 ? $value['Gy0_gxh'] : '0'.$value['Gy0_gxh'];
  1161. $list[$key]['gy'] = $value['Gy0_方案'].'-->'.$yjno.'-'.$gxh.' '.$value['gy0_gxmc'];
  1162. $list[$key]['gxmc'] = $value['Add_gxmc'];
  1163. }
  1164. $data['gy_data'] = $list;
  1165. $this->success('请求成功',$data);
  1166. }
  1167. /**
  1168. * 3.10印版资料-修改产品印版资料
  1169. * @ApiMethod POST
  1170. * @params string UniqId
  1171. */
  1172. public function editProductYbInfo(){
  1173. if (Request::instance()->isPost() == false){
  1174. $this->error('非法请求');
  1175. }
  1176. $params = Request::instance()->post();
  1177. if (empty($params) || !isset($params['UniqId'])){
  1178. $this->error('参数不能为空');
  1179. }
  1180. $UniqId = $params['UniqId'];
  1181. $operator = $this->getProductLogOperator($params);
  1182. unset($params['UniqId']);
  1183. $oldYb = \db('产品_印版资料')->where('UniqId',$UniqId)->find();
  1184. $sql = \db('产品_印版资料')->where('UniqId',$UniqId)->fetchSql(true)->update($params);
  1185. $res = Db::query($sql);
  1186. if ($res !== false){
  1187. if (!empty($oldYb)) {
  1188. $this->saveProductOperateLog($this->buildProductFieldChangeLogs($oldYb, $params, [
  1189. '产品编号' => isset($oldYb['YB_Cpdh']) ? $oldYb['YB_Cpdh'] : '',
  1190. '印件号' => isset($oldYb['YB_Yjno']) ? $oldYb['YB_Yjno'] : '',
  1191. '工序号' => isset($oldYb['YB_gxh']) ? $oldYb['YB_gxh'] : '',
  1192. '方案' => isset($oldYb['YB_方案']) ? $oldYb['YB_方案'] : '',
  1193. '业务表' => '产品_印版资料',
  1194. 'RecordUniqid' => $UniqId,
  1195. 'sys_id' => $operator,
  1196. ]));
  1197. }
  1198. $this->success('更新成功');
  1199. }else{
  1200. $this->error('更新失败');
  1201. }
  1202. }
  1203. /**
  1204. * 印版资料-获取物料名称
  1205. * @ApiMethod GET
  1206. *
  1207. */
  1208. public function getProductYbMaterialList(){
  1209. if (Request::instance()->isGet() == false){
  1210. $this->error('非法请求');
  1211. }
  1212. $sql = "SELECT rtrim(`编号`) as 编号, rtrim(`名称`) as 名称 FROM `物料_存货结构` WHERE `编号` IN ('0502','0503','0510','0511','0512','0513','0514','0520','0521','0523','0524','0525')";
  1213. $data = Db::query($sql);
  1214. $this->success('请求成功',$data);
  1215. }
  1216. /**
  1217. *3.12印版资料-获取详细存货名称
  1218. * @ApiMethod GET
  1219. * @params string code
  1220. */
  1221. public function getProductYbMaterialDetail(){
  1222. if (Request::instance()->isGet() == false){
  1223. $this->error('非法请求');
  1224. }
  1225. $params = Request::instance()->get();
  1226. if (empty($params) || !isset($params['code'])){
  1227. $this->error('参数不能为空');
  1228. }
  1229. $code = $params['code'];
  1230. $search = $params['search'];
  1231. $num = config('product_code_digit');
  1232. if (!empty($search)){
  1233. $sql = "SELECT rtrim(a.`物料代码`) as 物料代码,rtrim(a.`物料名称`) as 物料名称,rtrim(a.`规格`) as 规格,
  1234. rtrim(c.`编号`) as twoCode,rtrim(c.`名称`) as twoName,rtrim(d.`编号`) as thrCode,rtrim(d.`名称`) as thrName
  1235. FROM `物料_存货编码` a
  1236. LEFT JOIN `物料_存货结构` c ON LEFT(a.物料代码,$num) = c.编号
  1237. LEFT JOIN `物料_存货结构` d ON LEFT(a.物料代码,$num+2) = d.编号
  1238. WHERE a.物料代码 LIKE '{$code}%' AND a.物料名称 LIKE '%{$search}%'";
  1239. }else{
  1240. $sql = "SELECT rtrim(a.`物料代码`) as 物料代码,rtrim(a.`物料名称`) as 物料名称,rtrim(a.`规格`) as 规格,
  1241. rtrim(c.`编号`) as twoCode,rtrim(c.`名称`) as twoName,rtrim(d.`编号`) as thrCode,rtrim(d.`名称`) as thrName
  1242. FROM `物料_存货编码` a
  1243. LEFT JOIN `物料_存货结构` c ON LEFT(a.物料代码,$num) = c.编号
  1244. LEFT JOIN `物料_存货结构` d ON LEFT(a.物料代码,$num+2) = d.编号
  1245. WHERE a.物料代码 LIKE '{$code}%'";
  1246. }
  1247. $data = Db::query($sql);
  1248. $mergedArray = [];
  1249. foreach ($data as $item) {
  1250. $oneCode = '05';
  1251. $twoCode = $item['twoCode'];
  1252. $thrCode = $item['thrCode'];
  1253. $oneName = '版材';
  1254. $twoName = $item['twoName'];
  1255. $thrName = $item['thrName'];
  1256. $oneKey = "{$oneCode}/{$oneName}";
  1257. $twoKey = "{$twoCode}/{$twoName}";
  1258. $thrKey = "{$thrCode}/{$thrName}";
  1259. if (!isset($mergedArray[$oneKey])) {
  1260. $mergedArray[$oneKey] = [];
  1261. }
  1262. if (!isset($mergedArray[$oneKey][$twoKey])) {
  1263. $mergedArray[$oneKey][$twoKey] = [];
  1264. }
  1265. $mergedArray[$oneKey][$twoKey][$thrKey][] = $item;
  1266. }
  1267. $this->success('请求成功',$mergedArray);
  1268. }
  1269. /**
  1270. * 3.13印版资料-新增产品印版资料
  1271. * @ApiMethod POST
  1272. * @params array data
  1273. */
  1274. public function addProductYbInfo(){
  1275. if (Request::instance()->isPost() == false){
  1276. $this->error('非法请求');
  1277. }
  1278. $params = Request::instance()->post();
  1279. $UniqId = \db('产品_印版资料')->order('UniqID desc')->value('UniqID');
  1280. if ($UniqId < 2000000){
  1281. $UniqId = 2000000;
  1282. }else{
  1283. $UniqId = $UniqId + 1;
  1284. }
  1285. $params['UniqID'] = $UniqId;
  1286. $params['考核印数'] = '0.00';
  1287. $params['Sys_rq'] = date('Y-m-d H:i:s');
  1288. $operator = $this->getProductLogOperator($params);
  1289. $sql = \db('产品_印版资料')->fetchSql(true)->insert($params);
  1290. $res = Db::query($sql);
  1291. if ($res !== false){
  1292. $this->saveProductOperateLog([[
  1293. '产品编号' => isset($params['YB_Cpdh']) ? $params['YB_Cpdh'] : '',
  1294. '印件号' => isset($params['YB_Yjno']) ? $params['YB_Yjno'] : '',
  1295. '工序号' => isset($params['YB_gxh']) ? $params['YB_gxh'] : '',
  1296. '方案' => isset($params['YB_方案']) ? $params['YB_方案'] : '',
  1297. '业务表' => '产品_印版资料',
  1298. 'RecordUniqid' => $UniqId,
  1299. '操作类型' => '新增',
  1300. 'sys_id' => $operator,
  1301. ]]);
  1302. $this->success('新增成功');
  1303. }else{
  1304. $this->error('新增失败');
  1305. }
  1306. }
  1307. /**
  1308. * 3.14工艺资料-获取损耗代号
  1309. * @ApiMethod GET
  1310. * @params code
  1311. */
  1312. public function getLossCode(){
  1313. if (Request::instance()->isGet() == false){
  1314. $this->error('非法请求');
  1315. }
  1316. $params = Request::instance()->get();
  1317. if (empty($params) || !isset($params['code'])){
  1318. $this->error('参数不能为空');
  1319. }
  1320. $code = $params['code'];
  1321. $where['sys_bh|sys_mc'] = array('like','%'.$code.'%');
  1322. $data = \db('dic_lzsh')
  1323. ->where($where)
  1324. ->field('rtrim(sys_bh) as sys_bh, rtrim(sys_mc) as sys_mc,sys_rate0,sys_rate1')
  1325. ->select();
  1326. $this->success('请求成功',$data);
  1327. }
  1328. /**
  1329. * 产品工艺资料删除
  1330. * @return void
  1331. * @throws \think\Exception
  1332. * @throws \think\exception\PDOException
  1333. */
  1334. public function ProcessDetailDel()
  1335. {
  1336. // 1. 请求方法检查 - 删除操作建议使用POST方法
  1337. if (!$this->request->isGet()) {
  1338. $this->error('请求方法错误');
  1339. }
  1340. // 2. 参数获取与验证
  1341. $param = $this->request->param();
  1342. if (empty($param['UniqId'])) {
  1343. $this->error('参数错误: 缺少UniqId参数');
  1344. }
  1345. // 3. 参数处理
  1346. $uniqIds = explode(',', $param['UniqId']);
  1347. $uniqIds = array_filter(array_map('trim', $uniqIds)); // 过滤空值并去除空格
  1348. if (empty($uniqIds)) {
  1349. $this->error('参数错误: UniqId不能为空');
  1350. }
  1351. // 4. 获取当前用户ID
  1352. $userId = $param['sys_id'] ?? '';
  1353. if (empty($userId)) {
  1354. $this->error('参数错误: 缺少用户标识');
  1355. }
  1356. // 5. 查询要删除的工艺详情
  1357. $processDetails = Db::name('产品_工艺资料')
  1358. ->where('UniqID', 'in', $uniqIds)
  1359. ->field('UniqID, Gy0_cpdh, Gy0_yjno, Gy0_gxh, Gy0_方案')
  1360. ->select();
  1361. if (empty($processDetails)) {
  1362. $this->error('未找到要删除的工艺详情');
  1363. }
  1364. $logArray = [];
  1365. foreach ($processDetails as $item) {
  1366. $logArray[] = [
  1367. '产品编号' => $item['Gy0_cpdh'] ?? '',
  1368. '印件号' => $item['Gy0_yjno'] ?? '',
  1369. '工序号' => $item['Gy0_gxh'] ?? '',
  1370. '方案' => $item['Gy0_方案'] ?? '',
  1371. '业务表' => '产品_工艺资料',
  1372. 'RecordUniqid' => $item['UniqID'] ?? '',
  1373. '操作类型' => '删除',
  1374. '修改前' => '存在',
  1375. '修改后' => '已删除',
  1376. 'sys_id' => $userId,
  1377. '备注' => '产品工艺删除',
  1378. ];
  1379. }
  1380. // 7. 执行删除操作
  1381. $deleteResult = Db::name('产品_工艺资料')
  1382. ->where('UniqID', 'in', $uniqIds)
  1383. ->delete();
  1384. if ($deleteResult === false) {
  1385. $this->error('删除工艺详情失败');
  1386. }
  1387. $this->saveProductOperateLog($logArray);
  1388. // 9. 返回成功
  1389. $this->success('成功删除 ' . $deleteResult . ' 条工艺详情');
  1390. }
  1391. /**
  1392. * 产品印件资料删除
  1393. * @return void
  1394. * @throws \think\Exception
  1395. * @throws \think\exception\PDOException
  1396. */
  1397. public function PrintDetailDel()
  1398. {
  1399. if ($this->request->isGet() === false){
  1400. $this->error('请求错误');
  1401. }
  1402. $param = $this->request->param();
  1403. if (isset($param['UniqId']) === false){
  1404. $this->error('参数错误');
  1405. }
  1406. $printId = explode(',',$param['UniqId']);
  1407. $printRows = \db('产品_印件资料')->where('UniqID','in',$printId)->select();
  1408. $res = \db('产品_印件资料')->where('UniqID','in',$printId)->delete();
  1409. if ($res){
  1410. $logs = [];
  1411. foreach ($printRows as $row) {
  1412. $logs[] = [
  1413. '产品编号' => isset($row['yj_cpdh']) ? $row['yj_cpdh'] : '',
  1414. '产品名称' => isset($row['yj_yjmc']) ? $row['yj_yjmc'] : '',
  1415. '印件号' => isset($row['yj_yjno']) ? $row['yj_yjno'] : '',
  1416. '业务表' => '产品_印件资料',
  1417. 'RecordUniqid' => isset($row['UniqId']) ? $row['UniqId'] : (isset($row['UniqID']) ? $row['UniqID'] : ''),
  1418. '操作类型' => '删除',
  1419. '修改前' => isset($row['yj_yjmc']) ? trim((string)$row['yj_yjmc']) : '',
  1420. 'sys_id' => $this->getProductLogOperator($param),
  1421. ];
  1422. }
  1423. $this->saveProductOperateLog($logs);
  1424. $this->success('删除成功');
  1425. }else{
  1426. $this->error('删除失败');
  1427. }
  1428. }
  1429. /**
  1430. * 新增产品
  1431. * @ApiMethod POST
  1432. *
  1433. */
  1434. public function addProduct(){
  1435. if (Request::instance()->isPost() == false){
  1436. $this->error('非法请求');
  1437. }
  1438. $params = Request::instance()->post();
  1439. if (empty($params)){
  1440. $this->error('参数不能为空');
  1441. }
  1442. $uniqId = \db('产品_基本资料')->order('UniqID desc')->value('UniqID');
  1443. if (empty($uniqId)){
  1444. $params['UniqID'] = 1;
  1445. }else{
  1446. $params['UniqID'] = $uniqId + 1;
  1447. }
  1448. $params['Sys_rq'] = date('Y-m-d H:i:s');
  1449. $operator = $this->getProductLogOperator($params);
  1450. $sql = \db('产品_基本资料')->fetchSql(true)->insert($params);
  1451. $res = Db::query($sql);
  1452. if ($res !== false){
  1453. $this->saveProductOperateLog([[
  1454. '产品编号' => isset($params['产品编号']) ? $params['产品编号'] : '',
  1455. '产品名称' => isset($params['产品名称']) ? $params['产品名称'] : '',
  1456. '业务表' => '产品_基本资料',
  1457. 'RecordUniqid' => $params['UniqID'],
  1458. '操作类型' => '新增',
  1459. 'sys_id' => $operator,
  1460. ]]);
  1461. $this->success('新增成功');
  1462. }else{
  1463. $this->error('新增失败');
  1464. }
  1465. }
  1466. /**
  1467. * 产品资料删除
  1468. * @return void
  1469. * @throws \think\exception\PDOException
  1470. */
  1471. public function ProductDel()
  1472. {
  1473. if ($this->request->isGet() === false){
  1474. $this->error('请求错误');
  1475. }
  1476. $param = $this->request->param();
  1477. if (isset($param['UniqId']) === false){
  1478. $this->error('参数错误');
  1479. }
  1480. $WorkOrderId = explode(',',$param['UniqId']);
  1481. $operator = $this->getProductLogOperator($param);
  1482. $i = 0;
  1483. $logs = [];
  1484. foreach ($WorkOrderId as $key=>$value){
  1485. $productRow = \db('产品_基本资料')
  1486. ->where('UniqID',$value)
  1487. ->field('rtrim(产品编号) as 产品编号,rtrim(产品名称) as 产品名称')
  1488. ->find();
  1489. $product = empty($productRow['产品编号']) ? '' : $productRow['产品编号'];
  1490. \db()->startTrans();
  1491. try {
  1492. \db('产品_基本资料')->where('UniqID',$value)->delete();
  1493. if (!empty(\db('产品_工艺资料')->where('Gy0_cpdh',$product)->find())){
  1494. \db('产品_工艺资料')->where('Gy0_cpdh',$product)->delete();
  1495. }
  1496. if (!empty(\db('产品_印件资料')->where('yj_cpdh',$product)->find())){
  1497. \db('产品_印件资料')->where('yj_cpdh',$product)->delete();
  1498. }
  1499. \db()->commit();
  1500. $logs[] = [
  1501. '产品编号' => $product,
  1502. '产品名称' => empty($productRow['产品名称']) ? '' : $productRow['产品名称'],
  1503. '业务表' => '产品_基本资料',
  1504. 'RecordUniqid' => $value,
  1505. '操作类型' => '删除',
  1506. 'sys_id' => $operator,
  1507. '备注' => '同时删除该产品工艺资料和印件资料',
  1508. ];
  1509. }catch (\Exception $e){
  1510. \db()->rollback();
  1511. $i++;
  1512. }
  1513. }
  1514. $this->saveProductOperateLog($logs);
  1515. if ($i === 0){
  1516. $this->success('删除成功');
  1517. }else{
  1518. $this->error('删除失败');
  1519. }
  1520. }
  1521. /**
  1522. * 修改产品工艺资料
  1523. * @ApiMethod POST
  1524. *
  1525. */
  1526. public function productEdit()
  1527. {
  1528. // 1. 请求方法检查
  1529. if (!Request::instance()->isPost()) {
  1530. $this->error('非法请求');
  1531. }
  1532. // 2. 参数获取与验证
  1533. $params = Request::instance()->post();
  1534. if (empty($params)) {
  1535. $this->error('参数不能为空');
  1536. }
  1537. // 3. 必需参数验证
  1538. if (empty($params['UniqID'])) {
  1539. $this->error('产品唯一标识不能为空');
  1540. }
  1541. $uniqId = trim($params['UniqID']);
  1542. // 4. 查询原始数据
  1543. $processDetail = Db::name('产品_工艺资料')
  1544. ->where('UniqID', $uniqId)
  1545. ->find();
  1546. if (empty($processDetail)) {
  1547. $this->error('找不到对应的产品工艺资料');
  1548. }
  1549. // 5. 准备更新数据
  1550. $updateData = [];
  1551. $logArray = [];
  1552. $currentTime = date('Y-m-d H:i:s');
  1553. $modifyUser = $params['Sys_id'] ?? '';
  1554. // 6. 构建更新数据和日志记录
  1555. foreach ($params as $key => $value) {
  1556. // 跳过不需要更新的字段
  1557. if (in_array($key, ['UniqID', 'Sys_id'])) {
  1558. continue;
  1559. }
  1560. // 只处理表中存在的字段
  1561. if (array_key_exists($key, $processDetail)) {
  1562. $oldValue = $processDetail[$key] ?? '';
  1563. $newValue = trim($value);
  1564. // 检查值是否发生变化
  1565. if ($oldValue != $newValue) {
  1566. $updateData[$key] = $newValue;
  1567. $logArray[] = [
  1568. '产品编号' => $processDetail['Gy0_cpdh'] ?? '',
  1569. '印件号' => $processDetail['Gy0_yjno'] ?? '',
  1570. '工序号' => $processDetail['Gy0_gxh'] ?? '',
  1571. '方案' => $processDetail['Gy0_方案'] ?? '',
  1572. '业务表' => '产品_工艺资料',
  1573. 'RecordUniqid' => $uniqId,
  1574. '操作类型' => '修改',
  1575. '字段名' => $key,
  1576. '修改前' => $oldValue,
  1577. '修改后' => $newValue,
  1578. 'sys_id' => $modifyUser,
  1579. '备注' => '产品工艺调整',
  1580. ];
  1581. }
  1582. }
  1583. }
  1584. // 7. 如果没有需要更新的字段
  1585. if (empty($updateData)) {
  1586. $this->success('没有检测到数据变化');
  1587. }
  1588. // 8. 添加更新时间
  1589. $updateData['Mod_rq'] = $currentTime;
  1590. // 9. 更新产品工艺资料
  1591. // $updateResult = Db::name('产品_工艺资料')
  1592. // ->where('UniqID', $uniqId)
  1593. // ->update($updateData);
  1594. $sql = \db('产品_工艺资料')->where('UniqID',$uniqId)->fetchSql(true)->update($params);
  1595. $updateResult = Db::query($sql);
  1596. // 10. 检查更新结果
  1597. // ThinkPHP的update方法成功时返回影响的行数(>=0),失败返回false
  1598. if ($updateResult === false) {
  1599. $this->error('更新产品工艺资料失败');
  1600. }
  1601. $this->saveProductOperateLog($logArray);
  1602. // 12. 返回成功
  1603. $this->success('修改成功');
  1604. }
  1605. /**
  1606. * 获取联数
  1607. * @return void
  1608. * @throws \think\db\exception\DataNotFoundException
  1609. * @throws \think\db\exception\ModelNotFoundException
  1610. * @throws \think\exception\DbException
  1611. */
  1612. public function getCouplet()
  1613. {
  1614. if ($this->request->isGet() === false){
  1615. $this->error('请求错误');
  1616. }
  1617. $param = $this->request->param();
  1618. if (empty($param['product']) || empty($param['yjno'])){
  1619. $this->error('参数错误');
  1620. }
  1621. $list = \db('产品_印件资料')
  1622. ->where('yj_cpdh',$param['product'])
  1623. ->where('yj_yjno',$param['yjno'])
  1624. ->field('rtrim(yj_ks) as 开数,rtrim(yj_ls) as 联数')
  1625. ->find();
  1626. $process = \db('产品_工艺资料')
  1627. ->where('Gy0_cpdh',$param['product'])
  1628. ->where('Gy0_yjno',$param['yjno'])
  1629. ->order('Gy0_gxh desc')
  1630. ->value('Gy0_gxh');
  1631. $list['工序号'] = $process + 1;
  1632. $this->success('成功',$list);
  1633. }
  1634. /**
  1635. * 产品工艺排序
  1636. * @return void
  1637. * @throws \think\Exception
  1638. * @throws \think\exception\PDOException
  1639. */
  1640. public function ProductYgSort()
  1641. {
  1642. // 验证请求方法
  1643. if (!$this->request->isGet()) {
  1644. $this->error('请求错误');
  1645. }
  1646. // 获取并验证参数
  1647. $param = $this->request->param();
  1648. if (empty($param) || !isset($param['cpdh'])) {
  1649. $this->error('参数错误');
  1650. }
  1651. if (!isset($param['yjno']) && empty($param['yjno'])) {
  1652. $this->error('请选择印件');
  1653. }
  1654. // 构建查询条件
  1655. $where = ['Gy0_cpdh' => $param['cpdh']];
  1656. $where['Gy0_yjno'] = $param['yjno'];
  1657. // 获取待排序的数据
  1658. $list = \db('产品_工艺资料')
  1659. ->where($where)
  1660. ->order('Gy0_gxh,Sys_rq desc,Mod_rq desc')
  1661. ->column('Gy0_gxh', 'UniqID');
  1662. $operator = $this->getProductLogOperator($param);
  1663. $logs = [];
  1664. // 更新排序字段
  1665. $i = 1;
  1666. foreach ($list as $key => $value) {
  1667. if ((int)$value<20){
  1668. $result = \db('产品_工艺资料')
  1669. ->where('UniqID', $key)
  1670. ->update(['Gy0_gxh' => $i]);
  1671. if ($result !== false) {
  1672. if ((string)$value !== (string)$i) {
  1673. $logs[] = [
  1674. '产品编号' => $param['cpdh'],
  1675. '印件号' => $param['yjno'],
  1676. '工序号' => $i,
  1677. '业务表' => '产品_工艺资料',
  1678. 'RecordUniqid' => $key,
  1679. '操作类型' => '排序',
  1680. '字段名' => 'Gy0_gxh',
  1681. '修改前' => $value,
  1682. '修改后' => $i,
  1683. 'sys_id' => $operator,
  1684. ];
  1685. }
  1686. $i++;
  1687. } else {
  1688. \think\facade\Log::error("更新排序失败,UniqID: {$key}");
  1689. }
  1690. }
  1691. }
  1692. $this->saveProductOperateLog($logs);
  1693. $this->success('成功');
  1694. }
  1695. /**
  1696. * 批量参数里取操作人,列表请求可能把 sys_id 放在首条或外层
  1697. */
  1698. protected function pickProductLogOperator($params)
  1699. {
  1700. $operator = $this->getProductLogOperator($params);
  1701. if ($operator === '' && isset($params[0]) && is_array($params[0])) {
  1702. $operator = $this->getProductLogOperator($params[0]);
  1703. }
  1704. return $operator;
  1705. }
  1706. /**
  1707. * 读取操作人,兼容 sys_id / Sys_id
  1708. */
  1709. protected function getProductLogOperator($params)
  1710. {
  1711. if (!is_array($params)) {
  1712. return '';
  1713. }
  1714. if (isset($params['sys_id']) && $params['sys_id'] !== '') {
  1715. return trim((string)$params['sys_id']);
  1716. }
  1717. if (isset($params['Sys_id']) && $params['Sys_id'] !== '') {
  1718. return trim((string)$params['Sys_id']);
  1719. }
  1720. return '';
  1721. }
  1722. /**
  1723. * 工艺资料日志的公共字段
  1724. */
  1725. protected function gyLogBase(array $old, $uniqId, $operator)
  1726. {
  1727. return [
  1728. '产品编号' => isset($old['Gy0_cpdh']) ? trim((string)$old['Gy0_cpdh']) : '',
  1729. '印件号' => isset($old['Gy0_yjno']) ? $old['Gy0_yjno'] : '',
  1730. '工序号' => isset($old['Gy0_gxh']) ? $old['Gy0_gxh'] : '',
  1731. '方案' => isset($old['Gy0_方案']) ? $old['Gy0_方案'] : '',
  1732. '业务表' => '产品_工艺资料',
  1733. 'RecordUniqid' => $uniqId,
  1734. 'sys_id' => $operator,
  1735. ];
  1736. }
  1737. /**
  1738. * 按字段对比生成修改日志
  1739. */
  1740. protected function buildProductFieldChangeLogs(array $old, array $new, array $base)
  1741. {
  1742. $skip = ['sys_id', 'Sys_id', 'sys_rq', 'Sys_rq', 'mod_rq', 'Mod_rq', 'UniqId', 'UniqID'];
  1743. $logs = [];
  1744. foreach ($new as $field => $newValue) {
  1745. if (in_array($field, $skip, true) || !array_key_exists($field, $old)) {
  1746. continue;
  1747. }
  1748. if (trim((string)$old[$field]) === trim((string)$newValue)) {
  1749. continue;
  1750. }
  1751. $logs[] = array_merge($base, [
  1752. '操作类型' => isset($base['操作类型']) ? $base['操作类型'] : '修改',
  1753. '字段名' => $field,
  1754. '修改前' => trim((string)$old[$field]),
  1755. '修改后' => trim((string)$newValue),
  1756. ]);
  1757. }
  1758. return $logs;
  1759. }
  1760. /**
  1761. * 写入产品操作日志,失败不影响主操作
  1762. */
  1763. protected function saveProductOperateLog(array $rows)
  1764. {
  1765. if (empty($rows)) {
  1766. return;
  1767. }
  1768. $now = date('Y-m-d H:i:s');
  1769. $logs = [];
  1770. foreach ($rows as $row) {
  1771. $logs[] = [
  1772. '产品编号' => isset($row['产品编号']) ? $row['产品编号'] : '',
  1773. '产品名称' => isset($row['产品名称']) ? $row['产品名称'] : '',
  1774. '印件号' => isset($row['印件号']) ? $row['印件号'] : '',
  1775. '工序号' => isset($row['工序号']) ? $row['工序号'] : '',
  1776. '方案' => isset($row['方案']) ? $row['方案'] : '',
  1777. '业务表' => isset($row['业务表']) ? $row['业务表'] : '',
  1778. 'RecordUniqid' => isset($row['RecordUniqid']) ? $row['RecordUniqid'] : '',
  1779. '操作类型' => isset($row['操作类型']) ? $row['操作类型'] : '',
  1780. '字段名' => isset($row['字段名']) ? $row['字段名'] : '',
  1781. '修改前' => isset($row['修改前']) ? $row['修改前'] : '',
  1782. '修改后' => isset($row['修改后']) ? $row['修改后'] : '',
  1783. 'sys_id' => isset($row['sys_id']) ? $row['sys_id'] : '',
  1784. 'sys_rq' => $now,
  1785. '备注' => isset($row['备注']) ? $row['备注'] : '',
  1786. ];
  1787. }
  1788. try {
  1789. \db('产品_操作日志')->insertAll($logs);
  1790. } catch (\Exception $e) {
  1791. \think\Log::write('记录产品操作日志失败: ' . $e->getMessage());
  1792. }
  1793. }
  1794. }