<?php

/**
 * +----------------------------------------------------------------------
 * | Created By PhpStorm
 * +----------------------------------------------------------------------
 * | DATE: 2021/8/2
 * +----------------------------------------------------------------------
 * | TIME: 13:11
 * +----------------------------------------------------------------------
 * | Author: YangXing(925032253@qq.com)
 * +----------------------------------------------------------------------
 * | Class: bmpAction
 * +----------------------------------------------------------------------
 */

namespace api\action\v1;


use api\action\baseAction;
use \Exception;
use model\interIO\projectNotice;
use model\interIO\sampleDevelopChange;

class bmpAction extends baseAction
{
    //设置结果接口
    public function setSampleDCResult()
    {
        try {
            $param = $this->getParaam();
            $param['no'] = trim($param['no']);
            $param['result'] = trim($param['result']);
            $param['sdc_tech_cause'] = trim($param['technical']);
            $param['sdc_salesext_cause'] = trim($param['business']);
            errorLog(['开发需求变更',$param],'FROMBMP');
            if(empty($param['no'])){
                 $this->returnJson(500,'no不可为空');
            }
            if(mb_strlen($param['sdc_tech_cause']) > 1024){
                $this->returnJson(500,'technical'.'长度超过了限制50');
            }
            if(mb_strlen($param['sdc_salesext_cause']) > 1024){
                $this->returnJson(500,'business'.'长度超过了限制50');
            }
            if(mb_strlen($param['result']) > 50){
                $this->returnJson(500,'no'.'长度超过了限制50');
            }
            if(empty($param['result'])){
                $this->returnJson(500,'result不可为空');
            }
            if(mb_strlen($param['result']) > 50){
                $this->returnJson(500,'result'.'长度超过了限制50');
            }
            $db = new sampleDevelopChange();
            if(!$db->is($param['no'])){
                $this->returnJson(500,'no不存在');
            }
            $result = $db->setSdcResult($param['no'],$param['result'], $param['sdc_tech_cause'],$param['sdc_salesext_cause']);

            if($result === false){
                $this->returnJson(500,'参数错误,请重新再试');
            }else{
                $this->returnJson(200,'ok');
            }
        }catch (\Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
    }

    //工程通知
    public function engineeringChange()
    {
        try {
            $param = $this->getParaam();
            $data = [
                'spn_change_number' => trim($param['spn_change_number']),
                'spn_effective_date' => trim($param['spn_effective_date']),
                'spn_document_code' => trim($param['spn_document_code']),
                'spn_file_name' => trim($param['spn_file_name']),
                'spn_sign' => trim($param['spn_sign']),
                'spn_num_places' => trim($param['spn_num_places']),
                'spn_change_content' => trim($param['spn_change_content']),
                'spn_change_reason' => trim($param['spn_change_reason']),
                'spn_pro_change' => trim($param['spn_pro_change']),
                'spn_customer_name' => trim($param['spn_customer_name']),
                'spn_sample_no' => trim($param['spn_sample_no']),
            ];
            errorLog(['工程通知',$param],'FROMBMP');
            foreach ($data as $key => &$value)
            {
                if($key == 'spn_effective_date'){
                    if(mb_strlen($value) > 30){
                        $this->returnJson(500,'spn_effective_date'.'长度超过了限制30');
                    }
                }
                if(mb_strlen($value) > 255) {
                    $this->returnJson(500,$key.'长度超过了限制255');
                }
            }
            if(empty($data['spn_customer_name'])){
                $this->returnJson(500,'spn_customer_name不可为空');
            }
            if(empty($data['spn_change_number'])){
                $this->returnJson(500,'spn_change_number不可为空');
            }
            $db = new projectNotice();
            if($db->is($param['spn_change_number'],$param['spn_customer_name'])){
                //$this->returnJson(500,'spn_change_number+spn_customer_name已经存在');
            }
            $result = $db->add($data);
            if($result === false){
                $this->returnJson(500,'添加失败请重试');
            }else{
                $this->returnJson(200,'ok');
            }
        }catch (Exception $e) {
            $this->returnJson(500,$e->getMessage());
        }
    }


    /** 将样品状态发给 BMP
     * - 此接口仅是开发环境模拟 BMP 接口, 并不在生产环境实际使用
     */
    public function __sampleProcess()
    {
        try {
            $data = [
                'USERID' => '014297', // 工号
                'CDATE' => '2024-05-29', // 申请时间
                'KFLX' => '客户来样开发', // 开发类型
                'CPX' => '悬挂件', // 产品线
                'LSH' => '202401170091', // 样品流水号
                'OE' => '2063304902', // OE
                'CX' => 'MERCEDES-BENZ', // 适用车型
                'XCX' => 'C-CLASS (W206) 2022-', // 小车型
                'BYL' => '15000', // 保有量
                'SC' => '500000', // 潜在市场
                'YJXL' => '8000', // 预计销量/年
                'KFYY' => '新品开发', // 开发原因
            ];
            $data = json_encode($data, JSON_UNESCAPED_UNICODE);
            header('Content-Type', 'application/json');
            ob_clean();
            echo  $data;
        } catch (\Exception $e) {
            $this->returnJson(500, $e->getMessage());
        }
    }

    /** 借样单 */
    public function borrowSample()
    {
        try {
            $param = $this->getParaam();
            foreach (['CDATE', 'BH', 'ZT'] as $mustKey) {
                if (!isset($param[$mustKey]) || trim($param[$mustKey]) === '') {
                    $this->returnJson(500, $mustKey . '不能为空');
                }
                $param[$mustKey] = trim($param[$mustKey]);
            }
            if (!preg_match('/\d{4}-\d{2}-\d{2}/', $param['CDATE'])) {
                $this->returnJson(500, 'CDATE 借样时间 不是一个 年-月-日 的日期格式, 例 2024-01-02');
            }
            if (!in_array($param['ZT'], ['1', '0'])) {
                $this->returnJson(500, 'ZT 状态 必须是 1 或者 0');
            }
            $param['GSP'] = $param['GSP'] ?? '';
            $db = new \lib\db();
            $sno = $param['BH'];
            $sample = new \model\sample\sample();
            $sRow = $sample->getRowByFieldValue('s_plmseq', $sno);
            if (empty($sRow)) {
                $this->returnJson(500,  $sno . ' 样品流水号不存在');
            }
            $sid = $sRow['s_id'];
            $data = [
                'sbo_sid' => $sid,
                'sbo_sno' => $sno,
                'sbo_date' => $param['CDATE'],
                'sbo_outnum' => $param['GSP'],
                'sbo_status' => $param['ZT'],
            ];
            $db->replace('sample_borrow', $data, ['sbo_sid']);
            $this->returnJson(200, 'ok');
        } catch (\Exception $e) {
            $this->returnJson(500, $e->getMessage());
        }
    }

    /** 橡胶件悬挂件模具费
     */
    public function mouldCostForRS()
    {
        try {
            $param = $this->getParaam();
            $param = array_merge(
                [
                    'NUM' => '', # BPM流程流水号
                    'LSH' => '', # 样品流水号
                    'QDL' => '', # 开发起订量
                    'MJF' => '', # 模具费预计
                    'HSZC' => '', # 模县费回收政策	
                    'FHSL' => '', # 模具费返还数量
                    'KFSJ' => '', # 预计开发完成时间
                ],
                $param
            );
            foreach ($param as $k => $v) {
                $param[$k] = trim($v);
            }
            foreach (['LSH', 'MJF'] as $mustKey) {
                if (trim($param[$mustKey]) === '') {
                    $this->returnJson(500, $mustKey . '不能为空');
                }
            }
            if (!is_numeric($param['MJF'])) {
                $this->returnJson(500, 'MJF 模具费预计 必须是一个数字');
            }
            /*
            if ($param['QDL'] != '' && !is_numeric($param['QDL'])) {
                $this->returnJson(500, '开发起订量 必须是一个数字');
            }
            */
            /*
            if ($param['KFSJ'] != '' && !preg_match('/\d{4}-\d{2}-\d{2}/', $param['KFSJ'])) {
                $this->returnJson(500, 'KFSJ 预计开发完成时间	 不是一个 年-月-日 的日期格式, 例 2024-01-02');
            }*/
            $db = new \lib\db();
            $sno = $param['LSH'];
            $sample = new \model\sample\sample();
            $sRow = $sample->getRowByFieldValue('s_plmseq', $sno);
            if (empty($sRow)) {
                $this->returnJson(500,  $sno . ' 样品流水号不存在');
            }
            $sid = $sRow['s_id'];
            $data = [
                'smc_sid' => $sid,
                'smc_sno' => $sno,
                'smc_bpmline' => '11825',
                'smc_bpmno' => $param['NUM'],
                'smc_cost' => $param['MJF'],
                'smc_qdl' => $param['QDL'],
                'smc_fhsl' => $param['FHSL'],
                'smc_kfsj' => $param['KFSJ'],
                'smc_hszc' => $param['HSZC'],
            ];
            $db->replace('sample_mould_cost', $data, ['smc_sid']);
            $this->returnJson(200, 'ok');
        } catch (\Exception $e) {
            $this->returnJson(500, $e->getMessage());
        }
    }

    /** 传动轴轮毂模具费
     */
    public function mouldCostForCH()
    {
        try {
            $param = $this->getParaam();
            $param = array_merge(
                [
                    'LSH' => '', # 样品流水号
                    'ARRINUM' => '', # 样品到样号码
                    'INNUM' => '', # 内部型号
                    'CDATE' => '', # 申请时间	
                    'PRICE' => '', # 模具费预计
                    'REMARK' => '', # 开发描述		
                ],
                $param
            );
            foreach ($param as $k => $v) {
                $param[$k] = trim($v);
            }
            //foreach (['ARRINUM', 'INNUM', 'CDATE'] as $mustKey) {
            if ($param['LSH'] == '' && ($param['LSH'] == 'ARRINUM' || $param['LSH'] == 'INNUM')) {
                $this->returnJson(500, '样品流水号 和 样品到样号码 + 内部型号 必须有一组');
            }
            foreach (['CDATE'] as $mustKey) {
                if ($param[$mustKey] === '') {
                    $this->returnJson(500, $mustKey . '不能为空');
                }
            }
            if (!is_numeric($param['PRICE'])) {
                $this->returnJson(500, 'PRICE 模具费预计 必须是一个数字');
            }
            /*
            if ($param['CDATE'] != '' && !preg_match('/\d{4}-\d{2}-\d{2}/', $param['CDATE'])) {
                $this->returnJson(500, 'KFSJ 预计开发完成时间	 不是一个 年-月-日 的日期格式, 例 2024-01-02');
            }*/
            $db = new \lib\db();
            $sample = new \model\sample\sample();
            if ( $param['LSH'] != '') {
                $sno = $param['LSH'];
                $sample = new \model\sample\sample();
                $sRow = $sample->getRowByFieldValue('s_plmseq', $sno);
            } else {
                $sRow = $sample->getRowByWhere([
                    's_arri_number' => $param['ARRINUM'], # 样品到样号码
                    's_innum' => $param['INNUM'], # 内部型号
                ]);
                $sno = $sRow['s_plmseq'];

            }
            if (empty($sRow)) {
                $this->returnJson(500,  "到样号码 = '{$param['ARRINUM']}' 与 内部型号 = '{$param['INNUM']}' 的样品记录不存在}");
            }
            $sid = $sRow['s_id'];
            $data = [
                'smc_sid' => $sid,
                'smc_sno' => $sno,
                'smc_arri_number' => $param['ARRINUM'],
                'smc_innum' => $param['INNUM'],
                'smc_bpmline' => '11826',
                'smc_cost' => $param['PRICE'],
                'smc_remark' => $param['REMARK'],
            ];
            $db->replace('sample_mould_cost', $data, ['smc_sid']);
            $this->returnJson(200, 'ok');
        } catch (\Exception $e) {
            $this->returnJson(500, $e->getMessage());
        }
    }
}