| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 | <?phpnamespace App\Console\Commands;use App\Model\AreaMap;use App\Model\BasicType;use App\Model\BasicTypeAllUse;use App\Model\Construction;use App\Model\Customer;use App\Model\CustomerInfo;use App\Model\CustomerReport;use App\Model\CustomerReportDepart;use App\Model\Depart;use App\Model\Employee;use App\Model\FollowUpRecord;use App\Model\PaymentReceipt;use App\Model\PaymentReceiptInfo;use App\Model\Product;use App\Model\PurchaseOrder;use App\Model\SalesOrder;use App\Model\SalesOrderInfo;use App\Model\SalesOrderOtherFee;use App\Model\SalesOrderProductInfo;use App\Model\SalesOrderReport;use App\Model\SeeRange;use App\Service\ProductService;use Illuminate\Console\Command;use Illuminate\Support\Facades\DB;class SalesWriteReport extends Command{    /**     * The name and signature of the console command.     *     * @var string     */    protected $signature = 'command:sales_write_report';    /**     * The console command description.     *     * @var string     */    protected $description = 'Command description';    /**     * Create a new command instance.     *     * @return void     */    public function __construct()    {        parent::__construct();    }    /**     * Execute the console command.     *     * @return mixed     */    public function handle()    {        echo "执行任务--------start---------------\n";        try {            $this->SalesWrite();        }catch (\Throwable $exception){            echo "发生异常:" . $exception->getFile() . "|" . $exception->getMessage() . "|" . $exception->getLine() . "\n";        }        echo "执行任务--------end---------------\n";    }    private function SalesWrite(){        //今天到上月今天的时间戳        $nowStamp = time();        $lastMonth = date("Y-m-d 00:00:00", strtotime("-1 month"));        $lastMonthStamp = strtotime($lastMonth);        $area = Depart::where('del_time',0)            ->where('parent_id',0)            ->pluck('area','id')            ->toArray();        SalesOrder::where("del_time",0)            ->where('crt_time','>=', $lastMonthStamp)            ->where('crt_time','<=', $nowStamp)            ->orderBy('id')            ->chunkById(200, function ($data) use($nowStamp, $area){                // 开启事务                DB::transaction(function () use ($data, $nowStamp, $area) {                    $dataArray = $data->toArray();                    $main = $this->processingData($dataArray, $nowStamp,$area);                    if (! empty($main)) {                        SalesOrderReport::whereIn('sale_order_id',array_column($dataArray,'id'))                            ->update(['del_time' => $nowStamp]);                        SalesOrderReport::insert($main);                        echo '200条写入中' . PHP_EOL;                    } else {                        echo '暂无数据写入' . PHP_EOL;                    }                });            });    }    private function processingData($data,$time,$area){        $id = array_column($data,'id');        $contact_product = $this->getSalesOrderProduct($id);        $customer = $this->getCustomer(array_column($data,'customer_id'));        $basic = $this->getBasic($data);        $zp_money = $this->getOtherMoney($id);        $fz_man = $this->getFzMan($id);        $construction = $this->getConstruction($id);        $payment = $this->getPayment(array_column($data,'order_number'));        $purchase = $this->getPurchaseFrom(array_filter(array_column($data,'contact_order_no')));        $insert = [];        foreach ($data as $value){            $receipt = $payment[$value['order_number']] ?? 0;            $contact_product_tmp = $contact_product[$value['id']] ?? [];            $contact_product_tmp_str = implode('、',$contact_product_tmp);            $customer_tmp = $customer[$value['customer_id']] ?? "";            $basic_tmp = $basic[$value['plat_type']] ?? "";            $area_tmp = $area[$value['top_depart_id']] ?? 0;            $zp_tmp = $zp_money[$value['id']] ?? 0;            $fz_tmp = $fz_man[$value['id']] ?? "";            $purchase_tmp = $purchase[$value['contact_order_no']] ?? 0;            $state = $this->makeState($value);            if(isset($construction[$value['id']])){                $c = $construction[$value['id']];                if($c <= Construction::STATE_THREE){                    $state_tmp = 2;                }elseif($c <= Construction::STATE_FIVE){                    $state_tmp = 3;                }else{                    $state_tmp = 4;                }            }else{                $state_tmp = $state;            }            $insert[] = [                'sale_order_id' => $value['id'],                'sales_order_type' => $value['sales_order_type'],                'receipt' => $receipt,                'model_type' => $value['model_type'],                'order_number' => $value['order_number'],                'contact_product' => $contact_product_tmp_str,                'fz_man' => $fz_tmp,                'plat_type_title' => $basic_tmp,                'customer_title' => $customer_tmp,                'crt_id' => $value['crt_id'],                'crt_time' => $value['crt_time'],                'contract_fee' => $value['contract_fee'],                'zp_contract_fee' => $zp_tmp,                'top_depart_id' => $value['top_depart_id'],                'from_top_depart_id' => $purchase_tmp,                'area' => $area_tmp,                'state' => $state_tmp,            ];        }        return $insert;    }    private function getSalesOrderProduct($data_id)    {        $product = [];        $sales_p_info = SalesOrderProductInfo::where('del_time',0)            ->whereIn('sales_order_id',$data_id)            ->select('product_id','sales_order_id')            ->get()->toArray();        $map = Product::whereIn('id',array_unique(array_column($sales_p_info,'product_id')))            ->pluck('title','id')            ->toArray();        foreach ($sales_p_info as $value){            $tmp = $map[$value['product_id']] ?? "";            $product[$value['sales_order_id']][] = $tmp;        }        return $product;    }    private function getCustomer($data_id)    {        return Customer::whereIn('id',$data_id)            ->pluck('title','id')            ->toArray();    }    private function getBasic($data)    {        $array = array_unique(array_column($data,'plat_type'));        return BasicType::whereIn('id',$array)            ->pluck('title','id')            ->toArray();    }    private function getOtherMoney($id){        $fee = [];        $sales_o_info = SalesOrderOtherFee::where('del_time',0)            ->whereIn('sales_order_id',$id)            ->get()->toArray();        foreach ($sales_o_info as $value){            $fee[$value['sales_order_id']] = $value['other_fee_1'];        }        return $fee;    }    private function getFzMan($id){        //单据中选择的负责人        $sales_order = SalesOrderInfo::where('del_time',0)            ->where('type',SalesOrderInfo::type_two)            ->whereIn('sales_order_id',$id)            ->select('sales_order_id','data_id')            ->get()->toArray();        $empList = Employee::whereIn('id', array_unique(array_column($sales_order,'data_id')))            ->pluck('emp_name','id')            ->toArray();        $employee_two = [];        foreach ($sales_order as $value){            $str = $empList[$value['data_id']] ?? "";            if(! $str) continue;            if(isset($employee_two[$value['sales_order_id']])){                $employee_two[$value['sales_order_id']] .= ',' . $str;            }else{                $employee_two[$value['sales_order_id']] = $str;            }        }        return $employee_two;    }    private function makeState($value){        if($value['sales_order_type'] == SalesOrder::Order_type_one){            //安装件            if(in_array($value['state'], [SalesOrder::State_minus_one, SalesOrder::State_zero, SalesOrder::State_one])){               return 0;            }elseif(in_array($value['state'], [SalesOrder::State_two, SalesOrder::State_three, SalesOrder::State_four])){                return 1;            }else{                return 1;            }        }else{            return 0;        }        return $state;    }    private function getConstruction($id){        $construction = Construction::where('del_time',0)            ->whereIn('sales_order_id', $id)            ->select('sales_order_id','state')            ->get()->toArray();        $return = [];        foreach ($construction as $value){            if(isset($return[$value['sales_order_id']])){                if($value['state'] > $return[$value['sales_order_id']]) $return[$value['sales_order_id']] = $value['state'];            }else{                $return[$value['sales_order_id']] = $value['state'];            }        }        return $return;    }    private function getPayment($id){        $infos = PaymentReceiptInfo::where('del_time',0)            ->where('type',PaymentReceiptInfo::type_three)//回款信息            ->where('data_order_type', PaymentReceipt::data_type_one)//合同            ->where('data_type', PaymentReceipt::type_one)//收款            ->whereIn('data_order_no',$id)            ->get()->toArray();        $infos_map = [];        foreach ($infos as $value){            if(isset($infos_map[$value['data_order_no']])){                $tmp = bcadd($value['amount'], $infos_map[$value['data_order_no']],2);                $infos_map[$value['data_order_no']] = $tmp;            }else{                $infos_map[$value['data_order_no']] = $value['amount'];            }        }        return $infos_map;    }    private function getPurchaseFrom($order_no){        return PurchaseOrder::whereIn('order_number',$order_no)            ->pluck('top_depart_id','order_number')            ->toArray();    }}
 |