Product.php 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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. $i = 0;
  279. foreach ($params as $key=>$value){
  280. $data = [];
  281. if (!empty($value['A_power'])){
  282. $data['A类产能'] = $value['A_power'];
  283. }
  284. if (!empty($value['shdh'])){
  285. $data['Gy0_shdh'] = $value['shdh'];
  286. }
  287. if (!empty($value['machine'])){
  288. $data['Gy0_sbbh'] = $value['machine'];
  289. }
  290. if (!empty($value['time'])){
  291. $data['Gy0_辅助工时'] = $value['time'];
  292. }
  293. if (!empty($value['difficulty_coe'])){
  294. $data['工价系数'] = $value['difficulty_coe'];
  295. }
  296. if (!empty($value['loss_coe'])){
  297. $data['损耗系数'] = $value['loss_coe'];
  298. }
  299. if (!empty($value['ms_coe'])){
  300. $data['Gy0_Ms'] = $value['ms_coe'];
  301. }
  302. if (!empty($value['Gy0_site'])){
  303. $data['Gy0_site'] = $value['Gy0_site'];
  304. }
  305. // 如果修改了设备编号 → 同步未开工工单_工艺资料
  306. $newSbbh = isset($value['machine']) ? trim((string)$value['machine']) : '';
  307. $syncSql = '';
  308. if ($newSbbh !== '') {
  309. // 取产品_工艺资料的 Gy0_cpdh / Gy0_yjno / Gy0_gxh
  310. $gyInfo = \db('产品_工艺资料')->where('UniqID', $value['UniqID'])->field('Gy0_cpdh, Gy0_yjno, Gy0_gxh')->find();
  311. if (!empty($gyInfo)) {
  312. $cpdh = trim($gyInfo['Gy0_cpdh']);
  313. $yjno = trim($gyInfo['Gy0_yjno']);
  314. $gxh = trim($gyInfo['Gy0_gxh']);
  315. if ($cpdh !== '' && $yjno !== '' && $gxh !== '') {
  316. $syncSql = \db('工单_工艺资料')
  317. ->where('Gy0_sj1', '1900-01-01 00:00:00')
  318. ->where('Gy0_yjno', $yjno)
  319. ->where('Gy0_gxh', $gxh)
  320. ->whereIn('Gy0_gdbh', function($query) use ($cpdh) {
  321. $query->table('工单_基本资料')->where('Gd_cpdh', $cpdh)->field('Gd_gdbh');
  322. })
  323. ->fetchSql(true)
  324. ->update(['Gy0_sbbh' => $newSbbh]);
  325. }
  326. }
  327. }
  328. $sql = db('产品_工艺资料')->where('UniqID',$value['UniqID'])->fetchSql(true)->update($data);
  329. $res = Db::query($sql);
  330. if ($res !== false){
  331. $i++;
  332. }
  333. // 执行级联同步(不影响主更新的成功判定)
  334. if ($syncSql !== '') {
  335. Db::execute($syncSql);
  336. }
  337. }
  338. if ($i !== 0){
  339. $this->success('更新成功');
  340. }else{
  341. $this->error('更新失败');
  342. }
  343. }
  344. /**
  345. * 修改产品信息
  346. *
  347. * @ApiMethod POST
  348. *
  349. * @params object data
  350. */
  351. public function editProduct(){
  352. if (Request::instance()->isPost() == false){
  353. $this->error('非法请求');
  354. }
  355. $params = Request::instance()->post();
  356. if (empty($params)){
  357. $this->error('参数不能为空');
  358. }
  359. if (!isset($params['印品代号'])){
  360. $this->error('印品代号不能为空');
  361. }
  362. $code = $params['印品代号'];
  363. unset($params['印品代号']);
  364. $sql = db('产品_基本资料')->where('产品编号',$code)->fetchSql(true)->update($params);
  365. $res = Db::query($sql);
  366. if ($res !== false){
  367. $this->success('更新成功');
  368. }else{
  369. $this->error('更新失败');
  370. }
  371. }
  372. /**
  373. * 设置产品状态
  374. *
  375. * @ApiMethod POST
  376. * @params string status
  377. * @params string code
  378. */
  379. public function setProductStatus(){
  380. if (Request::instance()->isPost() == false){
  381. $this->error('非法请求');
  382. }
  383. $params = Request::instance()->post();
  384. if (empty($params['code']) || empty($params['status'])){
  385. $this->error('参数不能为空');
  386. }
  387. $code = $params['code'];
  388. $status = '';
  389. if ($params['status'] == 2){
  390. $status = '停产';
  391. }
  392. $sql = db('产品_基本资料')->where('产品编号',$code)->fetchSql(true)->setField('状态',$status);
  393. $res = Db::query($sql);
  394. if ($res !== false){
  395. $this->success('更新成功');
  396. }else{
  397. $this->error('更新失败');
  398. }
  399. }
  400. /**
  401. * 获取产品工艺数量
  402. *
  403. * @ApiMethod GET
  404. * @params string code
  405. */
  406. public function getGyTotal(){
  407. if (Request::instance()->isGet() == false){
  408. $this->error('非法请求');
  409. }
  410. $params = Request::instance()->param();
  411. $code = $params['code'];
  412. if (!isset($code)){
  413. $this->error('参数不能为空');
  414. }
  415. $res = db('产品_工艺资料')->where('Gy0_cpdh',$code)->distinct(true)->column('rtrim(Gy0_方案) as gy_plan');
  416. $data['gy'] = $res;
  417. $product = db('产品_基本资料')->where('产品编号',$code)->find();
  418. $data['yj'] = db('产品_印件资料')->where('yj_cpdh',$code)->column('yj_yjno');
  419. $data['name'] = rtrim($product['产品名称']);
  420. $this->success('请求成功',$data);
  421. }
  422. /**
  423. * 复制产品工艺信息
  424. *
  425. * @ApiMethod POST
  426. * @params string from_code
  427. * @params string from_pro
  428. * @params string to_code
  429. * @params string to_pro
  430. * @params int is_copy_gy
  431. */
  432. public function copyProductGy()
  433. {
  434. $request = Request::instance();
  435. if (!$request->isPost()) {
  436. $this->error('非法请求');
  437. }
  438. $params = $request->post();
  439. if (empty($params['from_code']) || empty($params['to_code'])) {
  440. $this->error('参数不能为空');
  441. }
  442. $where = [];
  443. if (isset($params['type']) && $params['type'] == 1) {
  444. $where = ['Gy0_gxh' => ['>', 20]];
  445. }
  446. $sys_id = $params['sys_id'];
  447. $mod_rq = date('Y-m-d H:i:s', time());
  448. // 定义数据处理策略(统一添加 Sys_id 和 Mod_rq 字段)
  449. $strategies = [
  450. 'gy' => [
  451. 'flag' => 'is_copy_gy',
  452. 'table' => '产品_工艺资料',
  453. 'source_field' => 'Gy0_cpdh',
  454. 'uniq_field' => 'UniqID',
  455. 'extra_check' => function () use ($params) {
  456. if (empty($params['to_pro'])) {
  457. $this->error('工艺方案不能为空');
  458. }
  459. },
  460. 'data_mapper' => function ($item, $seq) use ($params, $sys_id, $mod_rq) {
  461. return [
  462. 'Gy0_方案' => $params['to_pro'],
  463. 'Gy0_cpdh' => $params['to_code'],
  464. 'UniqID' => $seq,
  465. 'Sys_id' => $sys_id, // 新增
  466. 'Mod_rq' => $mod_rq // 新增
  467. ];
  468. }
  469. ],
  470. 'yj' => [
  471. 'flag' => 'is_copy_yj',
  472. 'table' => '产品_印件资料',
  473. 'source_field' => 'yj_cpdh',
  474. 'uniq_field' => 'UniqId',
  475. 'data_mapper' => function ($item, $seq) use ($params, $sys_id, $mod_rq) {
  476. return [
  477. 'yj_cpdh' => $params['to_code'],
  478. 'UniqId' => $seq,
  479. 'sys_id' => $sys_id, // 新增
  480. 'mod_rq' => $mod_rq // 新增
  481. ];
  482. }
  483. ],
  484. 'yb' => [
  485. 'flag' => 'is_copy_yb',
  486. 'table' => '产品_印版资料',
  487. 'source_field' => 'YB_Cpdh',
  488. 'uniq_field' => 'UniqID',
  489. 'data_mapper' => function ($item, $seq) use ($params, $sys_id, $mod_rq) {
  490. return [
  491. 'YB_Cpdh' => $params['to_code'],
  492. 'UniqID' => $seq,
  493. 'Sys_id' => $sys_id, // 新增
  494. 'Mod_rq' => $mod_rq // 新增
  495. ];
  496. }
  497. ]
  498. ];
  499. $processed = false;
  500. foreach ($strategies as $key => $strategy) {
  501. if (($params[$strategy['flag']] ?? 0) != 1) continue;
  502. $processed = true;
  503. if (isset($strategy['extra_check'])) {
  504. call_user_func($strategy['extra_check']);
  505. }
  506. // 构建查询条件
  507. $query = db($strategy['table'])->where($strategy['source_field'], $params['from_code']);
  508. if ($key === 'gy') {
  509. $query->where($where)->where('Gy0_方案', $params['from_pro']);
  510. }
  511. $data = $query->select();
  512. if (empty($data)) {
  513. $this->error("参考产品无{$strategy['table']}数据");
  514. }
  515. // 生成序列号
  516. $maxId = db($strategy['table'])
  517. ->order("{$strategy['uniq_field']} desc")
  518. ->value($strategy['uniq_field']) ?: 0;
  519. // 处理数据(自动覆盖原记录的 Sys_id 和 Mod_rq)
  520. $processedData = [];
  521. $currentSeq = 1;
  522. foreach ($data as $item) {
  523. unset($item[$strategy['uniq_field']]);
  524. $mapped = array_merge(
  525. $item,
  526. call_user_func($strategy['data_mapper'], $item, $maxId + $currentSeq)
  527. );
  528. $processedData[] = $mapped;
  529. $currentSeq++;
  530. }
  531. // 事务处理
  532. db()->startTrans();
  533. try {
  534. // db($strategy['table'])
  535. // ->where($strategy['source_field'], $params['to_code'])
  536. // ->delete();
  537. if (!db($strategy['table'])->insertAll($processedData)) {
  538. throw new \Exception("Insert failed");
  539. }
  540. db()->commit();
  541. } catch (\Exception $e) {
  542. db()->rollback();
  543. $this->error("复制{$strategy['table']}数据失败: " . $e->getMessage());
  544. }
  545. }
  546. if (!$processed) {
  547. $this->error('请至少选择一个复制项目');
  548. }
  549. $this->success('数据复制成功');
  550. }
  551. /**
  552. * 工艺方案更名
  553. *
  554. * @ApiMethod POST
  555. * @params string code
  556. * @params string name
  557. */
  558. public function editGyName(){
  559. if (Request::instance()->isPost() == false){
  560. $this->error('非法请求');
  561. }
  562. $params = Request::instance()->post();
  563. if (empty($params['code']) || empty($params['old_name']) || empty($params['new_name'])){
  564. $this->error('参数不能为空');
  565. }
  566. $where['Gy0_cpdh'] = $params['code'];
  567. $where['Gy0_方案'] = $params['old_name'];
  568. $sql = db('产品_工艺资料')->where($where)->fetchSql(true)->setField('Gy0_方案',$params['new_name']);
  569. $res = Db::query($sql);
  570. if ($res !== false){
  571. $this->success('更新成功');
  572. }else{
  573. $this->error('更新失败');
  574. }
  575. }
  576. /**
  577. * 工艺方案附加
  578. *
  579. * @ApiMethod POST
  580. * @params object data
  581. */
  582. public function editGyNo(){
  583. if (Request::instance()->isPost() == false){
  584. $this->error('非法请求');
  585. }
  586. $params = Request::instance()->post();
  587. if (empty($params) || !isset($params[0]['UniqID'])){
  588. $this->error('参数不能为空');
  589. }
  590. $i = 0;
  591. foreach ($params as $key=>$value){
  592. $data = [];
  593. if (!empty($value['Gy0_yjno'])){
  594. $data['Gy0_yjno'] = $value['Gy0_yjno'];
  595. }
  596. if (!empty($value['Gy0_gxh'])){
  597. $data['Gy0_gxh'] = $value['Gy0_gxh'];
  598. }
  599. if (!empty($value['Gy0_Ks'])){
  600. $data['Gy0_Ks'] = $value['Gy0_Ks'];
  601. }
  602. if (!empty($value['Gy0_ls'])){
  603. $data['Gy0_ls'] = $value['Gy0_ls'];
  604. }
  605. $sql = db('产品_工艺资料')->where('UniqID',$value['UniqID'])->fetchSql(true)->update($data);
  606. $res = Db::query($sql);
  607. if ($res !== false){
  608. $i++;
  609. }
  610. }
  611. if ($i !== 0){
  612. $this->success('更新成功');
  613. }else{
  614. $this->error('更新失败');
  615. }
  616. }
  617. /**
  618. * 计损色数修正
  619. *
  620. * @ApiMethod POST
  621. * @params object data
  622. */
  623. public function editGyMs(){
  624. if (Request::instance()->isPost() == false){
  625. $this->error('非法请求');
  626. }
  627. $params = Request::instance()->post();
  628. if (empty($params) || !isset($params[0]['UniqID'])){
  629. $this->error('参数不能为空');
  630. }
  631. $i = 0;
  632. foreach ($params as $key=>$value){
  633. $data = [];
  634. if (!empty($value['Gy0_Ms'])){
  635. $data['Gy0_Ms'] = $value['Gy0_Ms'];
  636. }
  637. if (!empty($value['Gy0_Ks'])){
  638. $data['Gy0_Ks'] = $value['Gy0_Ks'];
  639. }
  640. if (!empty($value['Gy0_ls'])){
  641. $data['Gy0_ls'] = $value['Gy0_ls'];
  642. }
  643. $data['Add_gxmc'] = $value['Add_gxmc'];
  644. $data['工序备注'] = $value['remark'];
  645. $sql = db('产品_工艺资料')->where('UniqID',$value['UniqID'])->fetchSql(true)->update($data);
  646. $res = Db::query($sql);
  647. if ($res !== false){
  648. $i++;
  649. }
  650. }
  651. if ($i !== 0){
  652. $this->success('更新成功');
  653. }else{
  654. $this->error('更新失败');
  655. }
  656. }
  657. /**
  658. * 产品质检废品系数调整
  659. *
  660. * @ApiMethod POST
  661. * @params object data
  662. */
  663. public function editGyWaste(){
  664. if (Request::instance()->isPost() == false){
  665. $this->error('非法请求');
  666. }
  667. $params = Request::instance()->post();
  668. if (empty($params) || !isset($params[0]['UniqID'])){
  669. $this->error('参数不能为空');
  670. }
  671. $i = 0;
  672. foreach ($params as $key=>$value){
  673. $data = [];
  674. if (!empty($value['artificial_zp'])){
  675. $data['人工检_正品板'] = $value['artificial_zp'];
  676. }
  677. if (!empty($value['artificial_cp'])){
  678. $data['人工检_次品板'] = $value['artificial_cp'];
  679. }
  680. if (!empty($value['artificial_fj'])){
  681. $data['人工检_废检'] = $value['artificial_fj'];
  682. }
  683. if (!empty($value['machine_zp'])){
  684. $data['机检_正品板'] = $value['machine_zp'];
  685. }
  686. if (!empty($value['machine_cp'])){
  687. $data['机检_次品板'] = $value['machine_cp'];
  688. }
  689. if (!empty($value['machine_fj'])){
  690. $data['机检_废检'] = $value['machine_fj'];
  691. }
  692. $sql = db('产品_工艺资料')->where('UniqID',$value['UniqID'])->fetchSql(true)->update($data);
  693. $res = Db::query($sql);
  694. if ($res !== false){
  695. $i++;
  696. }
  697. }
  698. if ($i !== 0){
  699. $this->success('更新成功');
  700. }else{
  701. $this->error('更新失败');
  702. }
  703. }
  704. /**
  705. * 获取产品印件资料
  706. * @ApiMethod GET
  707. * @params string UniqId
  708. */
  709. public function getProductYjInfo(){
  710. if (Request::instance()->isGet() == false){
  711. $this->error('非法请求');
  712. }
  713. $params = Request::instance()->param();
  714. if (empty($params['UniqId']) || empty($params['UniqId'])){
  715. $this->error('参数错误');
  716. }
  717. $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,
  718. 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,
  719. 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";
  720. $list = \db('产品_印件资料')->where('UniqId',$params['UniqId'])->field($field)->select();
  721. $this->success('请求成功',$list);
  722. }
  723. /**
  724. * 修改产品印件资料
  725. * @ApiMethod POST
  726. * @params array data
  727. */
  728. public function editProductYjInfo(){
  729. if (Request::instance()->isPost() == false){
  730. $this->error('非法请求');
  731. }
  732. $params = Request::instance()->post();
  733. if (empty($params) || !isset($params['UniqId'])){
  734. $this->error('参数不能为空');
  735. }
  736. $UniqId = $params['UniqId'];
  737. unset($params['UniqId']);
  738. $res = \db('产品_印件资料')->where('UniqId',$UniqId)->update($params);
  739. if ($res !== false){
  740. $this->success('更新成功');
  741. }else{
  742. $this->error('更新失败');
  743. }
  744. }
  745. /**
  746. * 新增产品印件资料
  747. * @ApiMethod POST
  748. * @params array data
  749. */
  750. public function addProductYjInfo(){
  751. if (Request::instance()->isPost() == false){
  752. $this->error('非法请求');
  753. }
  754. $params = Request::instance()->post();
  755. $UniqId = \db('产品_印件资料')->order('UniqId desc')->value('UniqId');
  756. if ($UniqId < 2000000){
  757. $UniqId = 2000000;
  758. }else{
  759. $UniqId = $UniqId + 1;
  760. }
  761. $params['UniqId'] = $UniqId;
  762. $params['sys_rq'] = date('Y-m-d H:i:s');
  763. $res = \db('产品_印件资料')->insert($params);
  764. if ($res !== false){
  765. $this->success('新增成功');
  766. }else{
  767. $this->error('新增失败');
  768. }
  769. }
  770. /**
  771. * 获取印件代码及名称
  772. * @ApiMethod GET
  773. *
  774. */
  775. public function getProductYjList(){
  776. if (Request::instance()->isGet() == false){
  777. $this->error('非法请求');
  778. }
  779. $params = Request::instance()->get();
  780. $search = $params['search'];
  781. // $num = config('product_code_digit');
  782. $num = 5;
  783. if (!empty($search)){
  784. $sql = "SELECT DISTINCT
  785. rtrim(a.`物料代码`) AS `物料代码`,
  786. rtrim(a.`物料名称`) AS `物料名称`,
  787. rtrim(b.`客户编号`) AS `客户编号`,
  788. rtrim(b.`客户名称`) AS `客户名称`
  789. FROM
  790. `物料_存货编码` a
  791. JOIN `产品_基本资料` b ON LEFT(a.`物料代码`,
  792. CASE
  793. WHEN a.`物料代码` REGEXP '[a-zA-Z]' THEN 5
  794. ELSE 4
  795. END
  796. ) = LEFT(b.`客户编号`,
  797. CASE
  798. WHEN a.`物料代码` REGEXP '[a-zA-Z]' THEN 5
  799. ELSE 4
  800. END
  801. )
  802. WHERE
  803. (a.`物料名称` LIKE '%{$search}%' OR a.`物料代码` LIKE '%{$search}%')
  804. GROUP BY
  805. a.`物料代码`
  806. ORDER BY
  807. a.`物料代码` ASC ";
  808. }else{
  809. $sql = "SELECT DISTINCT
  810. rtrim(a.`物料代码`) AS `物料代码`,
  811. rtrim(a.`物料名称`) AS `物料名称`,
  812. rtrim(b.`客户编号`) AS `客户编号`,
  813. rtrim(b.`客户名称`) AS `客户名称`
  814. FROM
  815. `物料_存货编码` a
  816. JOIN `产品_基本资料` b ON LEFT(a.`物料代码`,
  817. CASE
  818. WHEN a.`物料代码` REGEXP '[a-zA-Z]' THEN 5
  819. ELSE 4
  820. END
  821. ) = LEFT(b.`客户编号`,
  822. CASE
  823. WHEN a.`物料代码` REGEXP '[a-zA-Z]' THEN 5
  824. ELSE 4
  825. END
  826. )
  827. ORDER BY
  828. a.`物料代码` ASC";
  829. }
  830. $data = Db::query($sql);
  831. // 初始化一个关联数组,用于存储相同客户编号的数据
  832. $groupedData = [];
  833. foreach ($data as $row) {
  834. $customerCode = substr($row['物料代码'],0,$num).substr($row['客户编号'],2,2).'/'.$row['客户名称'];
  835. $materialCodePrefix = substr($row['物料代码'], 0, $num);
  836. if ($materialCodePrefix == 'Y1401' ){
  837. $materialCodePrefix = $materialCodePrefix.'/糊盒类产品(含贴码)';
  838. }else{
  839. $materialCodePrefix = $materialCodePrefix.'/直接领用产品';
  840. }
  841. // 如果关联数组中不存在该物料代码前四位的键,则创建一个空数组
  842. if (!isset($groupedData[$materialCodePrefix])) {
  843. $groupedData[$materialCodePrefix] = [];
  844. }
  845. // 如果物料代码前四位数组中不存在该客户编号的键,则创建一个空数组
  846. if (!isset($groupedData[$materialCodePrefix][$customerCode])) {
  847. $groupedData[$materialCodePrefix][$customerCode] = [];
  848. }
  849. // 去除客户编号和客户名称
  850. unset($row['客户编号']);
  851. unset($row['客户名称']);
  852. // 将当前行的数据添加到相应的物料代码前四位和客户编号的数组中
  853. $groupedData[$materialCodePrefix][$customerCode][] = $row;
  854. }
  855. $this->success('请求成功',$groupedData);
  856. }
  857. /**
  858. * 获取纸张代号及名称
  859. * @ApiMethod GET
  860. * @params string search
  861. */
  862. public function getProductZzList(){
  863. if (Request::instance()->isGet() == false){
  864. $this->error('非法请求');
  865. }
  866. $params = Request::instance()->get();
  867. $search = $params['search'];
  868. $num = config('product_code_digit');
  869. if (!empty($search)){
  870. $sql = "SELECT rtrim(a.`物料代码`) as 物料代码,rtrim(a.`物料名称`) as 物料名称,rtrim(a.`规格`) as 规格,rtrim(b.编号) as oneCode,rtrim(b.名称) as oneName,
  871. rtrim(c.`编号`) as twoCode,rtrim(c.`名称`) as twoName,rtrim(d.`编号`) as thrCode,rtrim(d.`名称`) as thrName
  872. FROM `物料_存货编码` a
  873. LEFT JOIN `物料_存货结构` b ON LEFT(a.物料代码,$num-2) = b.编号
  874. LEFT JOIN `物料_存货结构` c ON LEFT(a.物料代码,$num) = c.编号
  875. LEFT JOIN `物料_存货结构` d ON LEFT(a.物料代码,$num+2) = d.编号
  876. WHERE (a.物料名称 LIKE '%{$search}%' or a.物料代码 LIKE '%{$search}%') AND (
  877. a.物料代码 LIKE '00%'
  878. OR a.物料代码 LIKE '01%'
  879. OR a.物料代码 LIKE '04%'
  880. OR a.物料代码 LIKE '03%'
  881. OR a.物料代码 LIKE '14%'
  882. OR a.物料代码 LIKE 'Y00%'
  883. OR a.物料代码 LIKE 'Y01%'
  884. OR a.物料代码 LIKE 'Y04%'
  885. OR a.物料代码 LIKE 'J03%'
  886. OR a.物料代码 LIKE 'Y14%')";
  887. $data = Db::query($sql);
  888. }else{
  889. $sql = "SELECT rtrim(a.`物料代码`) as 物料代码,rtrim(a.`物料名称`) as 物料名称,rtrim(a.`规格`) as 规格,rtrim(b.编号) as oneCode,rtrim(b.名称) as oneName,
  890. rtrim(c.`编号`) as twoCode,rtrim(c.`名称`) as twoName,rtrim(d.`编号`) as thrCode,rtrim(d.`名称`) as thrName
  891. FROM `物料_存货编码` a
  892. LEFT JOIN `物料_存货结构` b ON LEFT(a.物料代码,$num-2) = b.编号
  893. LEFT JOIN `物料_存货结构` c ON LEFT(a.物料代码,$num) = c.编号
  894. LEFT JOIN `物料_存货结构` d ON LEFT(a.物料代码,$num+2) = d.编号
  895. WHERE a.物料代码 LIKE 'Y00%' or a.物料代码 LIKE 'Y01%' or a.物料代码 LIKE 'Y04%' or a.物料代码 LIKE 'J03%' or a.物料代码 LIKE 'Y14%' OR
  896. a.物料代码 LIKE '00%' or a.物料代码 LIKE '01%' or a.物料代码 LIKE '04%' or a.物料代码 LIKE '03%' or a.物料代码 LIKE '14%'";
  897. $data = Db::query($sql);
  898. }
  899. $mergedArray = [];
  900. foreach ($data as $item) {
  901. $oneCode = $item['oneCode'];
  902. $twoCode = $item['twoCode'];
  903. $thrCode = $item['thrCode'];
  904. $oneName = $item['oneName'];
  905. $twoName = $item['twoName'];
  906. $thrName = $item['thrName'];
  907. // Create a unique key using the combination of oneCode, twoCode, and thrCode
  908. $oneKey = "{$oneCode}/{$oneName}";
  909. $twoKey = "{$twoCode}/{$twoName}";
  910. $thrKey = "{$thrCode}/{$thrName}";
  911. // Initialize arrays if not already set
  912. if (!isset($mergedArray[$oneKey])) {
  913. $mergedArray[$oneKey] = [];
  914. }
  915. if (!isset($mergedArray[$oneKey][$twoKey])) {
  916. $mergedArray[$oneKey][$twoKey] = [];
  917. }
  918. // Append items to the arrays
  919. $mergedArray[$oneKey][$twoKey][$thrKey][] = $item;
  920. }
  921. $this->success('请求成功',$mergedArray);
  922. }
  923. /**
  924. *3.6工艺资料-获取产品工艺资料
  925. */
  926. public function getProductGyInfo(){
  927. if (Request::instance()->isGet() == false){
  928. $this->error('非法请求');
  929. }
  930. $params = Request::instance()->param();
  931. if (empty($params['UniqID']) || empty($params['UniqID'])){
  932. $this->error('参数错误');
  933. }
  934. $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,
  935. 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 工序备注,
  936. rtrim(a.质量要求) as 质量要求,rtrim(a.质量隐患) as 质量隐患,a.UniqID";
  937. $data = \db('产品_工艺资料')->alias('a')
  938. ->join('dic_lzsh b','a.Gy0_shdh = b.sys_bh','left')
  939. ->where('a.UniqID',$params['UniqID'])
  940. ->field($field)
  941. ->find();
  942. $this->success('请求成功',$data);
  943. }
  944. /**
  945. * 3.7工艺资料-获取车间及工艺名称
  946. * @ApiMethod GET
  947. *
  948. */
  949. public function getDepartName(){
  950. if (Request::instance()->isGet() == false){
  951. $this->error('非法请求');
  952. }
  953. $param = $this->request->param();
  954. $data = \db('erp_常用字典')->where('分类','印刷工艺')->order('编号 asc')->column('名称');
  955. $resultArray = [];
  956. foreach ($data as $item) {
  957. $parts = explode('_', $item);
  958. // 使用第一个部分作为一级键,第二个部分作为二级键
  959. $firstKey = $parts[0];
  960. $secondKey = $parts[1];
  961. $thirdValue = $parts[2];
  962. $resultArray[$firstKey][$secondKey][] = $thirdValue;
  963. }
  964. // if (isset($param['sort'])){
  965. // $result['标准工艺']['智能车间'] = $resultArray['标准工艺']['智能车间'];
  966. // }else{
  967. // unset($resultArray['标准工艺']['智能车间']);
  968. $result = $resultArray;
  969. // }
  970. $this->success('请求成功',$result);
  971. }
  972. /**
  973. * 3.8工艺资料-新增产品工艺
  974. */
  975. public function addProductGyInfo(){
  976. if (Request::instance()->isPost() == false){
  977. $this->error('非法请求');
  978. }
  979. $params = Request::instance()->post();
  980. $UniqId = \db('产品_工艺资料')->order('UniqID desc')->value('UniqID');
  981. if ($UniqId < 2000000){
  982. $UniqId = 2000000;
  983. }else{
  984. $UniqId = $UniqId + 1;
  985. }
  986. $params['UniqID'] = $UniqId;
  987. $params['Sys_rq'] = date('Y-m-d H:i:s');
  988. $sql = \db('产品_工艺资料')->fetchSql(true)->insert($params);
  989. $res = Db::query($sql);
  990. if ($res !== false){
  991. $this->success('新增成功');
  992. }else{
  993. $this->error('新增失败');
  994. }
  995. }
  996. /**
  997. * 3.9印版资料-获取产品印版资料
  998. * @ApiMethod GET
  999. */
  1000. public function getProductYbInfo(){
  1001. if (Request::instance()->isGet() == false){
  1002. $this->error('非法请求');
  1003. }
  1004. $params = Request::instance()->param();
  1005. if (empty($params['UniqID']) || empty($params['UniqID'])){
  1006. $this->error('参数错误');
  1007. }
  1008. $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,
  1009. rtrim(b.Add_gxmc) as Add_gxmc,rtrim(c.物料名称) as 物料名称,rtrim(a.YB_cpdh) as YB_cpdh";
  1010. $data = \db('产品_印版资料')->alias('a')
  1011. ->join('产品_工艺资料 b','a.YB_cpdh = b.Gy0_cpdh and a.YB_Yjno = b.Gy0_yjno and a.YB_gxh = b.Gy0_gxh','left')
  1012. ->join('物料_存货编码 c','a.存货编码 = c.物料代码','left')
  1013. ->where('a.UniqID',$params['UniqID'])->field($field)->order('存货编码')->find();
  1014. if (empty($data)){
  1015. $this->error('未查询到产品印版资料');
  1016. }
  1017. $where['Gy0_site'] = [
  1018. ['like','胶印%'],
  1019. ['like','烫模%'],
  1020. 'or'
  1021. ];
  1022. $option['Gy0_cpdh'] = $data['YB_cpdh'];
  1023. $option['Gy0_gxh'] = ['<',10];
  1024. $option['Gy0_sbbh'] = ['neq',''];
  1025. $gyData = \db('产品_工艺资料')->where($option)->where($where)
  1026. ->field('rtrim(Gy0_方案) as Gy0_方案,Gy0_yjno,Gy0_gxh,rtrim(gy0_gxmc) as gy0_gxmc,rtrim(Add_gxmc) as Add_gxmc')
  1027. ->order('Gy0_yjno,Gy0_gxh')->select();
  1028. $list = [];
  1029. foreach ($gyData as $key=>$value){
  1030. $yjno = $value['Gy0_yjno'] > 10 ? $value['Gy0_yjno'] : '0'.$value['Gy0_yjno'];
  1031. $gxh = $value['Gy0_gxh'] > 10 ? $value['Gy0_gxh'] : '0'.$value['Gy0_gxh'];
  1032. $list[$key]['gy'] = $value['Gy0_方案'].'-->'.$yjno.'-'.$gxh.' '.$value['gy0_gxmc'];
  1033. $list[$key]['gxmc'] = $value['Add_gxmc'];
  1034. }
  1035. $data['gy_data'] = $list;
  1036. $this->success('请求成功',$data);
  1037. }
  1038. /**
  1039. * 3.10印版资料-修改产品印版资料
  1040. * @ApiMethod POST
  1041. * @params string UniqId
  1042. */
  1043. public function editProductYbInfo(){
  1044. if (Request::instance()->isPost() == false){
  1045. $this->error('非法请求');
  1046. }
  1047. $params = Request::instance()->post();
  1048. if (empty($params) || !isset($params['UniqId'])){
  1049. $this->error('参数不能为空');
  1050. }
  1051. $UniqId = $params['UniqId'];
  1052. unset($params['UniqId']);
  1053. $sql = \db('产品_印版资料')->where('UniqId',$UniqId)->fetchSql(true)->update($params);
  1054. $res = Db::query($sql);
  1055. if ($res !== false){
  1056. $this->success('更新成功');
  1057. }else{
  1058. $this->error('更新失败');
  1059. }
  1060. }
  1061. /**
  1062. * 印版资料-获取物料名称
  1063. * @ApiMethod GET
  1064. *
  1065. */
  1066. public function getProductYbMaterialList(){
  1067. if (Request::instance()->isGet() == false){
  1068. $this->error('非法请求');
  1069. }
  1070. $sql = "SELECT rtrim(`编号`) as 编号, rtrim(`名称`) as 名称 FROM `物料_存货结构` WHERE `编号` IN ('0502','0503','0510','0511','0512','0513','0514','0520','0521','0523','0524','0525')";
  1071. $data = Db::query($sql);
  1072. $this->success('请求成功',$data);
  1073. }
  1074. /**
  1075. *3.12印版资料-获取详细存货名称
  1076. * @ApiMethod GET
  1077. * @params string code
  1078. */
  1079. public function getProductYbMaterialDetail(){
  1080. if (Request::instance()->isGet() == false){
  1081. $this->error('非法请求');
  1082. }
  1083. $params = Request::instance()->get();
  1084. if (empty($params) || !isset($params['code'])){
  1085. $this->error('参数不能为空');
  1086. }
  1087. $code = $params['code'];
  1088. $search = $params['search'];
  1089. $num = config('product_code_digit');
  1090. if (!empty($search)){
  1091. $sql = "SELECT rtrim(a.`物料代码`) as 物料代码,rtrim(a.`物料名称`) as 物料名称,rtrim(a.`规格`) as 规格,
  1092. rtrim(c.`编号`) as twoCode,rtrim(c.`名称`) as twoName,rtrim(d.`编号`) as thrCode,rtrim(d.`名称`) as thrName
  1093. FROM `物料_存货编码` a
  1094. LEFT JOIN `物料_存货结构` c ON LEFT(a.物料代码,$num) = c.编号
  1095. LEFT JOIN `物料_存货结构` d ON LEFT(a.物料代码,$num+2) = d.编号
  1096. WHERE a.物料代码 LIKE '{$code}%' AND a.物料名称 LIKE '%{$search}%'";
  1097. }else{
  1098. $sql = "SELECT rtrim(a.`物料代码`) as 物料代码,rtrim(a.`物料名称`) as 物料名称,rtrim(a.`规格`) as 规格,
  1099. rtrim(c.`编号`) as twoCode,rtrim(c.`名称`) as twoName,rtrim(d.`编号`) as thrCode,rtrim(d.`名称`) as thrName
  1100. FROM `物料_存货编码` a
  1101. LEFT JOIN `物料_存货结构` c ON LEFT(a.物料代码,$num) = c.编号
  1102. LEFT JOIN `物料_存货结构` d ON LEFT(a.物料代码,$num+2) = d.编号
  1103. WHERE a.物料代码 LIKE '{$code}%'";
  1104. }
  1105. $data = Db::query($sql);
  1106. $mergedArray = [];
  1107. foreach ($data as $item) {
  1108. $oneCode = '05';
  1109. $twoCode = $item['twoCode'];
  1110. $thrCode = $item['thrCode'];
  1111. $oneName = '版材';
  1112. $twoName = $item['twoName'];
  1113. $thrName = $item['thrName'];
  1114. $oneKey = "{$oneCode}/{$oneName}";
  1115. $twoKey = "{$twoCode}/{$twoName}";
  1116. $thrKey = "{$thrCode}/{$thrName}";
  1117. if (!isset($mergedArray[$oneKey])) {
  1118. $mergedArray[$oneKey] = [];
  1119. }
  1120. if (!isset($mergedArray[$oneKey][$twoKey])) {
  1121. $mergedArray[$oneKey][$twoKey] = [];
  1122. }
  1123. $mergedArray[$oneKey][$twoKey][$thrKey][] = $item;
  1124. }
  1125. $this->success('请求成功',$mergedArray);
  1126. }
  1127. /**
  1128. * 3.13印版资料-新增产品印版资料
  1129. * @ApiMethod POST
  1130. * @params array data
  1131. */
  1132. public function addProductYbInfo(){
  1133. if (Request::instance()->isPost() == false){
  1134. $this->error('非法请求');
  1135. }
  1136. $params = Request::instance()->post();
  1137. $UniqId = \db('产品_印版资料')->order('UniqID desc')->value('UniqID');
  1138. if ($UniqId < 2000000){
  1139. $UniqId = 2000000;
  1140. }else{
  1141. $UniqId = $UniqId + 1;
  1142. }
  1143. $params['UniqID'] = $UniqId;
  1144. $params['考核印数'] = '0.00';
  1145. $params['Sys_rq'] = date('Y-m-d H:i:s');
  1146. $sql = \db('产品_印版资料')->fetchSql(true)->insert($params);
  1147. $res = Db::query($sql);
  1148. if ($res !== false){
  1149. $this->success('新增成功');
  1150. }else{
  1151. $this->error('新增失败');
  1152. }
  1153. }
  1154. /**
  1155. * 3.14工艺资料-获取损耗代号
  1156. * @ApiMethod GET
  1157. * @params code
  1158. */
  1159. public function getLossCode(){
  1160. if (Request::instance()->isGet() == false){
  1161. $this->error('非法请求');
  1162. }
  1163. $params = Request::instance()->get();
  1164. if (empty($params) || !isset($params['code'])){
  1165. $this->error('参数不能为空');
  1166. }
  1167. $code = $params['code'];
  1168. $where['sys_bh|sys_mc'] = array('like','%'.$code.'%');
  1169. $data = \db('dic_lzsh')
  1170. ->where($where)
  1171. ->field('rtrim(sys_bh) as sys_bh, rtrim(sys_mc) as sys_mc,sys_rate0,sys_rate1')
  1172. ->select();
  1173. $this->success('请求成功',$data);
  1174. }
  1175. /**
  1176. * 产品工艺资料删除
  1177. * @return void
  1178. * @throws \think\Exception
  1179. * @throws \think\exception\PDOException
  1180. */
  1181. public function ProcessDetailDel()
  1182. {
  1183. // 1. 请求方法检查 - 删除操作建议使用POST方法
  1184. if (!$this->request->isGet()) {
  1185. $this->error('请求方法错误');
  1186. }
  1187. // 2. 参数获取与验证
  1188. $param = $this->request->param();
  1189. if (empty($param['UniqId'])) {
  1190. $this->error('参数错误: 缺少UniqId参数');
  1191. }
  1192. // 3. 参数处理
  1193. $uniqIds = explode(',', $param['UniqId']);
  1194. $uniqIds = array_filter(array_map('trim', $uniqIds)); // 过滤空值并去除空格
  1195. if (empty($uniqIds)) {
  1196. $this->error('参数错误: UniqId不能为空');
  1197. }
  1198. // 4. 获取当前用户ID
  1199. $userId = $param['sys_id'] ?? '';
  1200. if (empty($userId)) {
  1201. $this->error('参数错误: 缺少用户标识');
  1202. }
  1203. // 5. 查询要删除的工艺详情
  1204. $processDetails = Db::name('产品_工艺资料')
  1205. ->where('UniqID', 'in', $uniqIds)
  1206. ->field('UniqID, Gy0_cpdh, Gy0_yjno, Gy0_gxh')
  1207. ->select();
  1208. if (empty($processDetails)) {
  1209. $this->error('未找到要删除的工艺详情');
  1210. }
  1211. // 6. 准备日志记录
  1212. $logArray = [];
  1213. $currentTime = date('Y-m-d H:i:s');
  1214. foreach ($processDetails as $item) {
  1215. $logArray[] = [
  1216. 'Gd_gdbh' => $item['Gy0_cpdh'] ?? '',
  1217. 'yjno' => $item['Gy0_yjno'] ?? '',
  1218. 'gxh' => $item['Gy0_gxh'] ?? '',
  1219. 'ModifyUser' => $userId,
  1220. 'ModifyTime' => $currentTime,
  1221. 'FieldName' => '产品工艺删除',
  1222. 'OldValue' => '存在', // 记录删除前的状态
  1223. 'NewValue' => '已删除',
  1224. 'ModifySource' => '产品工艺删除'
  1225. ];
  1226. }
  1227. // 7. 执行删除操作
  1228. $deleteResult = Db::name('产品_工艺资料')
  1229. ->where('UniqID', 'in', $uniqIds)
  1230. ->delete();
  1231. if ($deleteResult === false) {
  1232. $this->error('删除工艺详情失败');
  1233. }
  1234. // 8. 记录操作日志(即使日志记录失败,也不影响主操作)
  1235. if (!empty($logArray)) {
  1236. $logResult = Db::name('系统操作日志表')->insertAll($logArray);
  1237. if ($logResult === false) {
  1238. // 记录日志失败,但不影响主操作
  1239. \think\Log::write('记录操作日志失败: ' . Db::name('系统操作日志表')->getLastSql());
  1240. }
  1241. }
  1242. // 9. 返回成功
  1243. $this->success('成功删除 ' . $deleteResult . ' 条工艺详情');
  1244. }
  1245. /**
  1246. * 产品印件资料删除
  1247. * @return void
  1248. * @throws \think\Exception
  1249. * @throws \think\exception\PDOException
  1250. */
  1251. public function PrintDetailDel()
  1252. {
  1253. if ($this->request->isGet() === false){
  1254. $this->error('请求错误');
  1255. }
  1256. $param = $this->request->param();
  1257. if (isset($param['UniqId']) === false){
  1258. $this->error('参数错误');
  1259. }
  1260. $printId = explode(',',$param['UniqId']);
  1261. $res = \db('产品_印件资料')->where('UniqID','in',$printId)->delete();
  1262. if ($res){
  1263. $this->success('删除成功');
  1264. }else{
  1265. $this->error('删除失败');
  1266. }
  1267. }
  1268. /**
  1269. * 新增产品
  1270. * @ApiMethod POST
  1271. *
  1272. */
  1273. public function addProduct(){
  1274. if (Request::instance()->isPost() == false){
  1275. $this->error('非法请求');
  1276. }
  1277. $params = Request::instance()->post();
  1278. if (empty($params)){
  1279. $this->error('参数不能为空');
  1280. }
  1281. $uniqId = \db('产品_基本资料')->order('UniqID desc')->value('UniqID');
  1282. if (empty($uniqId)){
  1283. $params['UniqID'] = 1;
  1284. }else{
  1285. $params['UniqID'] = $uniqId + 1;
  1286. }
  1287. $params['Sys_rq'] = date('Y-m-d H:i:s');
  1288. $sql = \db('产品_基本资料')->fetchSql(true)->insert($params);
  1289. $res = Db::query($sql);
  1290. if ($res !== false){
  1291. $this->success('新增成功');
  1292. }else{
  1293. $this->error('新增失败');
  1294. }
  1295. }
  1296. /**
  1297. * 产品资料删除
  1298. * @return void
  1299. * @throws \think\exception\PDOException
  1300. */
  1301. public function ProductDel()
  1302. {
  1303. if ($this->request->isGet() === false){
  1304. $this->error('请求错误');
  1305. }
  1306. $param = $this->request->param();
  1307. if (isset($param['UniqId']) === false){
  1308. $this->error('参数错误');
  1309. }
  1310. $WorkOrderId = explode(',',$param['UniqId']);
  1311. $i = 0;
  1312. foreach ($WorkOrderId as $key=>$value){
  1313. //获取产品编号
  1314. $product = \db('产品_基本资料')
  1315. ->where('UniqID',$value)
  1316. ->value('rtrim(产品编号)');
  1317. //删除工单资料、工艺资料、印件资料
  1318. \db()->startTrans();
  1319. try {
  1320. \db('产品_基本资料')->where('UniqID',$value)->delete();
  1321. if (!empty(\db('产品_工艺资料')->where('Gy0_cpdh',$product)->find())){
  1322. \db('产品_工艺资料')->where('Gy0_cpdh',$product)->delete();
  1323. }
  1324. if (!empty(\db('产品_印件资料')->where('yj_cpdh',$product)->find())){
  1325. \db('产品_印件资料')->where('yj_cpdh',$product)->delete();
  1326. }
  1327. \db()->commit();
  1328. }catch (\Exception $e){
  1329. \db()->rollback();
  1330. $i++;
  1331. }
  1332. }
  1333. if ($i === 0){
  1334. $this->success('删除成功');
  1335. }else{
  1336. $this->error('删除失败');
  1337. }
  1338. }
  1339. /**
  1340. * 修改产品工艺资料
  1341. * @ApiMethod POST
  1342. *
  1343. */
  1344. public function productEdit()
  1345. {
  1346. // 1. 请求方法检查
  1347. if (!Request::instance()->isPost()) {
  1348. $this->error('非法请求');
  1349. }
  1350. // 2. 参数获取与验证
  1351. $params = Request::instance()->post();
  1352. if (empty($params)) {
  1353. $this->error('参数不能为空');
  1354. }
  1355. // 3. 必需参数验证
  1356. if (empty($params['UniqID'])) {
  1357. $this->error('产品唯一标识不能为空');
  1358. }
  1359. $uniqId = trim($params['UniqID']);
  1360. // 4. 查询原始数据
  1361. $processDetail = Db::name('产品_工艺资料')
  1362. ->where('UniqID', $uniqId)
  1363. ->find();
  1364. if (empty($processDetail)) {
  1365. $this->error('找不到对应的产品工艺资料');
  1366. }
  1367. // 5. 准备更新数据
  1368. $updateData = [];
  1369. $logArray = [];
  1370. $currentTime = date('Y-m-d H:i:s');
  1371. $modifyUser = $params['Sys_id'] ?? '';
  1372. // 6. 构建更新数据和日志记录
  1373. foreach ($params as $key => $value) {
  1374. // 跳过不需要更新的字段
  1375. if (in_array($key, ['UniqID', 'Sys_id'])) {
  1376. continue;
  1377. }
  1378. // 只处理表中存在的字段
  1379. if (array_key_exists($key, $processDetail)) {
  1380. $oldValue = $processDetail[$key] ?? '';
  1381. $newValue = trim($value);
  1382. // 检查值是否发生变化
  1383. if ($oldValue != $newValue) {
  1384. $updateData[$key] = $newValue;
  1385. $logArray[] = [
  1386. 'Gd_gdbh' => $processDetail['Gy0_cpdh'] ?? '',
  1387. 'yjno' => $processDetail['Gy0_yjno'] ?? '',
  1388. 'gxh' => $processDetail['Gy0_gxh'] ?? '',
  1389. 'ModifyUser' => $modifyUser,
  1390. 'ModifyTime' => $currentTime,
  1391. 'FieldName' => $key,
  1392. 'OldValue' => $oldValue,
  1393. 'NewValue' => $newValue,
  1394. 'ModifySource' => '产品工艺调整'
  1395. ];
  1396. }
  1397. }
  1398. }
  1399. // 7. 如果没有需要更新的字段
  1400. if (empty($updateData)) {
  1401. $this->success('没有检测到数据变化');
  1402. }
  1403. // 8. 添加更新时间
  1404. $updateData['Mod_rq'] = $currentTime;
  1405. // 9. 更新产品工艺资料
  1406. // $updateResult = Db::name('产品_工艺资料')
  1407. // ->where('UniqID', $uniqId)
  1408. // ->update($updateData);
  1409. $sql = \db('产品_工艺资料')->where('UniqID',$uniqId)->fetchSql(true)->update($params);
  1410. $updateResult = Db::query($sql);
  1411. // 10. 检查更新结果
  1412. // ThinkPHP的update方法成功时返回影响的行数(>=0),失败返回false
  1413. if ($updateResult === false) {
  1414. $this->error('更新产品工艺资料失败');
  1415. }
  1416. // 11. 记录操作日志(即使日志记录失败,也不影响主操作)
  1417. if (!empty($logArray)) {
  1418. $logResult = Db::name('系统操作日志表')
  1419. ->insertAll($logArray);
  1420. if ($logResult === false) {
  1421. // 记录日志失败,但不影响主操作
  1422. \think\Log::write('记录操作日志失败: ' . Db::name('系统操作日志表')->getLastSql());
  1423. }
  1424. }
  1425. // 12. 返回成功
  1426. $this->success('修改成功');
  1427. }
  1428. /**
  1429. * 获取联数
  1430. * @return void
  1431. * @throws \think\db\exception\DataNotFoundException
  1432. * @throws \think\db\exception\ModelNotFoundException
  1433. * @throws \think\exception\DbException
  1434. */
  1435. public function getCouplet()
  1436. {
  1437. if ($this->request->isGet() === false){
  1438. $this->error('请求错误');
  1439. }
  1440. $param = $this->request->param();
  1441. if (empty($param['product']) || empty($param['yjno'])){
  1442. $this->error('参数错误');
  1443. }
  1444. $list = \db('产品_印件资料')
  1445. ->where('yj_cpdh',$param['product'])
  1446. ->where('yj_yjno',$param['yjno'])
  1447. ->field('rtrim(yj_ks) as 开数,rtrim(yj_ls) as 联数')
  1448. ->find();
  1449. $process = \db('产品_工艺资料')
  1450. ->where('Gy0_cpdh',$param['product'])
  1451. ->where('Gy0_yjno',$param['yjno'])
  1452. ->order('Gy0_gxh desc')
  1453. ->value('Gy0_gxh');
  1454. $list['工序号'] = $process + 1;
  1455. $this->success('成功',$list);
  1456. }
  1457. /**
  1458. * 产品工艺排序
  1459. * @return void
  1460. * @throws \think\Exception
  1461. * @throws \think\exception\PDOException
  1462. */
  1463. public function ProductYgSort()
  1464. {
  1465. // 验证请求方法
  1466. if (!$this->request->isGet()) {
  1467. $this->error('请求错误');
  1468. }
  1469. // 获取并验证参数
  1470. $param = $this->request->param();
  1471. if (empty($param) || !isset($param['cpdh'])) {
  1472. $this->error('参数错误');
  1473. }
  1474. if (!isset($param['yjno']) && empty($param['yjno'])) {
  1475. $this->error('请选择印件');
  1476. }
  1477. // 构建查询条件
  1478. $where = ['Gy0_cpdh' => $param['cpdh']];
  1479. $where['Gy0_yjno'] = $param['yjno'];
  1480. // 获取待排序的数据
  1481. $list = \db('产品_工艺资料')
  1482. ->where($where)
  1483. ->order('Gy0_gxh,Sys_rq desc,Mod_rq desc')
  1484. ->column('Gy0_gxh', 'UniqID');
  1485. // 更新排序字段
  1486. $i = 1;
  1487. foreach ($list as $key => $value) {
  1488. if ((int)$value<20){
  1489. $result = \db('产品_工艺资料')
  1490. ->where('UniqID', $key)
  1491. ->update(['Gy0_gxh' => $i]);
  1492. if ($result !== false) {
  1493. $i++;
  1494. } else {
  1495. // 记录失败日志
  1496. \think\facade\Log::error("更新排序失败,UniqID: {$key}");
  1497. }
  1498. }
  1499. }
  1500. $this->success('成功');
  1501. }
  1502. }