<?php

namespace app\api\controller\water;

use app\admin\model\sms\SmsRecord;
use app\admin\model\user\UserGroup;
use app\admin\model\water\WaterCate;
use app\admin\model\water\WaterDefault;
use app\admin\model\water\WaterMaterial;
use app\admin\model\water\WaterMaterialScience;
use app\admin\model\water\WaterQuery;
use app\api\controller\ZjSMSServerService;
use app\models\user\User;
use app\Request;
use crmeb\services\GroupDataService;
use crmeb\services\QrcodeService;
use crmeb\services\UtilService;
use think\facade\Db;

/**
 * 秒杀产品类
 * Class StoreSeckillController
 * @package app\api\controller\activity
 */
class WaterQueryController
{

    public function query(Request $request)
    {
        $data = UtilService::postMore([
            ['long', ''],// 长
            ['wide', ''],// 宽
            ['high', ''],// 高
            ['is_warm', ''], // 是否保温
            ['warm', ''], // 是否保温
            ['is_channel', ''], // 是否需要槽钢
            ['channel', ''], // 是否需要槽钢
            ['is_human_ladder', ''] ,// 是否需要人梯
            ['is_gc', ''] ,// 公差
            ['roof', ''], // 顶板厚度
            ['floor', ''], // 底板厚度
            ['side_plate', []], // 侧板厚度
            ['column', ''], // 立柱厚度
            ['lacing', []], // 拉筋厚度
            ['fula', []], // 辅拉
        ]);
        $user = User::where('uid', $request->uid())->find();
//        if ($user['member'] == 0){
//            if ($user['frequency'] == 0 and $user['free'] == 0){
//                return app('json')->fail('查询次数已用完');
//            }
//        }
        //顶板
        $data['uid'] = $request->uid();
        $calculation = $this->calculation($data);
        $amount = $this->AmountOfMoney($calculation, $data);// 计算总价,表格
        Db::startTrans();
        try {
            $res = WaterQuery::create([
                'uid' =>  $request->uid(),
                'long' => $data['long'],
                'wide' => $data['wide'],
                'high' => $data['high'],
                'is_warm' => $data['is_warm'],
                'is_channel' => $data['is_channel'],
                'is_ladder' => $data['is_human_ladder'],
                'is_gc' => $data['is_gc'],
                'price' => $amount['zj'],
                'weight' => $amount['zzl'],
            ]);
            foreach ($amount['table'] as $item){
                $item['query_id'] = $res['id'];
                WaterMaterial::create($item);
            }
            $amount['remind'] = '';
            $count = WaterQuery::where('uid', $request->uid())->whereBetweenTime('create_time', strtotime('today'), strtotime('tomorrow'))->count();
            if ($count > sys_config('remind')){
                //今天计算超过设置次数提醒
                $amount['remind'] = '你今日计算次数已超过'.sys_config('remind').'次';
            }
//            if ($user['member'] == 0){
//                if ($user['free'] > 0){
//                    User::where('uid', $request->uid())->dec('free', 1)->update();
//                }else{
//                    User::where('uid', $request->uid())->dec('frequency', 1)->update();
//                }
//            }
            if ($user['phone']){
                $res1 = $this->NewSmsSend('13905764599', ['code' => $user['phone']], '');
                $res1 = $this->NewSmsSend('13905764591', ['code' => $user['phone']], '');
            }
            Db::commit();
            return app('json')->success($amount);
        } catch (\Exception $e) {
            Db::rollback();
            return app('json')->fail($e->getMessage());
        }
    }


    /**
     * 发送短信
     * @param string $phone 手机号码
     * @param array $data 模板替换内容
     * @param string $template 模板编号
     * @return bool|string
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function NewSmsSend(string $phone, array $data, string $template)
    {
        try {
            $res = ZjSMSServerService::send($phone, $data);
            if ($res['status'] != '200') {
                return $res['msg'];
            } else {
                SmsRecord::sendRecord($phone, $data['code'], $template, '');
            }
            return true;
        } catch (Exception $exception) {
            return $exception->getMessage();
        }
    }

    public function AmountOfMoney($calculation, $data)
    {
        $user = User::where('uid', $data['uid'])->find();
        $discount = 0;
        if ($user['group_id'] > 0){
            $discount = UserGroup::where('id', $user['group_id'])->value('discount');
        }
        $table = [];
        $zj = 0; //总价
        $zzl = 0; // 总重量
        $where = [];
//        if ($data['is_gc'] > 0){
//            $where[] = ['is_gc', '=' ,1];
//        }else{
//            $where[] = ['is_gc', '=' ,0];
//        }
        foreach ($calculation['roof_number'] as $k => $v){
            if ($k == '1*1'){
                $details = WaterMaterialScience::where('cate_id', $data['roof'])->where($where)->where([['long', '=', 1], ['wide', '=', 1]])->find();

                $zj += $details['unit_price'] * $v;
                $zzl += $details['weight'] * $v;
                $table[] = ['title' => '顶板', 'name' => $details['name'], 'specifications' => $details['long'].'m*'.$details['wide'].'m', 'number' => $v, 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $v, 'weights' => $details['weight'] * $v ,'weight' => $details['weight'],'code' => $details['number'],'type' => 1];
            }elseif ($k == '1*0.5'){
                $details = WaterMaterialScience::where('cate_id', $data['roof'])->where($where)->where([['long', '=', 1], ['wide', '=', 0.5]])->find();

                $zj += $details['unit_price'] * $v;
                $zzl += $details['weight'] * $v;
                $table[] = ['title' => '顶板','name' => $details['name'], 'specifications' =>  $details['long'].'m*'.$details['wide'].'m', 'number' => $v, 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $v, 'weights' => $details['weight'] * $v ,'weight' => $details['weight'],'code' => $details['number'],'type' => 1];
            }elseif ($k == '0.5*0.5'){
                $details = WaterMaterialScience::where('cate_id', $data['roof'])->where($where)->where([['long', '=', 0.5], ['wide', '=', 0.5]])->find();

                $zj += $details['unit_price'] * $v;
                $zzl += $details['weight'] * $v;
                $table[] = ['title' => '顶板','name' => $details['name'], 'specifications' =>  $details['long'].'m*'.$details['wide'].'m', 'number' => $v, 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $v, 'weights' => $details['weight'] * $v ,'weight' => $details['weight'],'code' => $details['number'], 'type' => 1];
            }
        }
        // 底板
        $science = WaterMaterialScience::where('cate_id', $data['floor'])->order('wide DESC')->select()->toArray();
        if (count($science) == 1){
            $floor = $calculation['floor_number'];
            foreach ($science as $item) {
                $m2 = $item['long'] * $item['wide'];
                $floor1 = $floor / $m2;
                $zj +=  $item['unit_price'] * $floor1;
                $zzl += $item['weight'] * $floor1;
                $table[] = ['title' => '底板', 'name' => $item['name'], 'specifications' =>  $item['long'].'m*'.$item['wide'].'m', 'number' => $floor1, 'unit_price' => $item['unit_price'], 'company' => $item['company'], 'ot_price' => $item['unit_price'] * $floor1, 'weights' => $item['weight'] * $floor1,'weight' => $item['weight'],'code' => $item['number'], 'type' => 4];
            }
        }else {
            $floor = $calculation['floor_number'];
            foreach ($science as $item) {
                if ($floor > 0) {
                    $m2 = $item['long'] * $item['wide'];
                    if ($floor >= $m2) {
//                        $floor1 = intval($floor / $m2);
                        $floor1 = $floor / $m2;
                        $floor = 0;
//                        $floor = $floor - ($m2 * $floor1);
//                        if ($floor < 0.5 and $floor > 0) {
//                            $floor1 += 0.5;
//                            $floor = 0;
//                        }
                        $zj +=  $item['unit_price'] * $floor1;
                        $zzl += $item['weight'] * $floor1;
                        $table[] = ['title' => '底板', 'name' => $item['name'], 'specifications' =>  $item['long'].'m*'.$item['wide'].'m', 'number' => $floor1, 'unit_price' => $item['unit_price'], 'company' => $item['company'], 'ot_price' => $item['unit_price'] * $floor1, 'weights' => $item['weight'] * $floor1,'weight' => $item['weight'],'code' => $item['number'], 'type' => 4];
                    }
                }
            }
        }
        // 侧板
        $tables = [];
        $storey = 0;
        foreach ($calculation['side_number'] as $item){
            $storey ++;
            foreach ($item as $k => $v){
                if ($k == '1*1'){
                    $details = WaterMaterialScience::where('cate_id', $data['side_plate'][$storey-1])->where($where)->where([['long', '=', 1], ['wide', '=', 1]])->find();
                    $zj += $details['unit_price'] * $v;
                    $zzl += $details['weight'] * $v;
                    $tables[] = ['title' => '侧板'.$storey.'层' , 'name' => $details['name'], 'specifications' =>  $details['long'].'m*'.$details['wide'].'m', 'number' => $v, 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $v, 'weights' => $details['weight'] * $v , 'weight' => $details['weight'],'code' => $details['number'],'type' => 2];
                }elseif ($k == '1*0.5'){
                    $details = WaterMaterialScience::where('cate_id', $data['side_plate'][$storey-1])->where($where)->where([['long', '=', 1], ['wide', '=', 0.5]])->find();

                    $zj += $details['unit_price'] * $v;
                    $zzl += $details['weight'] * $v;
                    $tables[] = ['title' => '侧板'.$storey.'层' , 'name' => $details['name'], 'specifications' =>  $details['long'].'m*'.$details['wide'].'m', 'number' => $v, 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $v, 'weights' => $details['weight'] * $v ,'weight' => $details['weight'],'code' => $details['number'], 'type' => 2];
                }elseif ($k == '0.5*0.5'){
                    $details = WaterMaterialScience::where('cate_id', $data['side_plate'][$storey-1])->where($where)->where([['long', '=', 0.5], ['wide', '=', 0.5]])->find();

                    $zj += $details['unit_price'] * $v;
                    $zzl += $details['weight'] * $v;
                    $tables[] = ['title' => '侧板'.$storey.'层' , 'name' => $details['name'], 'specifications' =>  $details['long'].'m*'.$details['wide'].'m', 'number' => $v, 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $v, 'weights' => $details['weight'] * $v ,'weight' => $details['weight'],'code' => $details['number'], 'type' => 2];
                }
            }
        }

        $table = array_merge($table, Array_reverse($tables));

        $tables1 = [];
        $tables2 = [];
        //拉筋
        $lac_storey = 0;
        foreach ($calculation['lacing_number'] as $v){
            $lac_storey ++;
            $details = WaterMaterialScience::where('cate_id', $data['lacing'][$lac_storey-1])->where($where)->find();
            $zj += $details['unit_price'] * $v;
            $zzl += $details['weight'] * $v;
            $tables1[] = ['title' => '主拉'.$lac_storey.'层' , 'name' => $details['name'], 'specifications' =>  $details['long'].'*'.$details['wide'].'*'.'2.1', 'number' => $v, 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $v, 'weights' => $details['weight'] * $v ,'weight' => $details['weight'],'code' => $details['number'], 'type' => 3];
        }

        //辅拉
        $fula_storey = 0;
        foreach ($calculation['fula_number'] as $v){
            $fula_storey ++;
            $details = WaterMaterialScience::where('cate_id', $data['fula'][$fula_storey-1])->where($where)->find();
            $zj += $details['unit_price'] * $v;
            $zzl += $details['weight'] * $v;
            $tables2[] = ['title' => '辅拉'.$fula_storey.'层' , 'name' => $details['name'], 'specifications' =>  $details['long'].'*'.$details['wide'].'*'.'2.1', 'number' => $v, 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $v, 'weights' => $details['weight'] * $v ,'weight' => $details['weight'],'code' => $details['number'], 'type' => 6];
        }

        $table = array_merge($table, Array_reverse($tables1));
        $table = array_merge($table, Array_reverse($tables2));
        // 立柱
        $details = WaterMaterialScience::where('cate_id', $data['column'])->where($where)->find();
        $zj += $details['unit_price'] * $calculation['column_number'];
        $zzl += $details['weight'] * $calculation['column_number'];
        $table[] = ['title' => '立柱' , 'name' => $details['name'], 'specifications' =>  $details['long'].'*'.$details['wide'].'*'.'2.1', 'number' => $calculation['column_number'], 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $calculation['column_number'], 'weights' => $details['weight'] * $calculation['column_number'] ,'weight' => $details['weight'],'code' => $details['number'], 'type' => 5];

        //保温
        foreach ($calculation['warm'] as $k => $v){
            if ($k == 'warm1' and $v > 0){
                $details = WaterMaterialScience::where('cate_id', $data['warm'])->where([['long', '=', 1], ['wide', '=', 1]])->where($where)->find();
                $zj += $details['unit_price'] * $v;
                $zzl += $details['weight'] * $v;
                $table[] = ['title' => '保温板' , 'name' => $details['name'], 'specifications' => '1m*1m', 'number' => $v, 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $v, 'weights' => $details['weight'] * $v ,'weight' => $details['weight'],'code' => $details['number']??'无', 'type' => 7];
            }elseif ($k == 'warm2' and $v > 0){
                $details = WaterMaterialScience::where('cate_id', $data['warm'])->where([['long', '=', 1], ['wide', '=', 0.5]])->where($where)->find();
                $zj += $details['unit_price'] * $v;
                $zzl += $details['weight'] * $v;
                $table[] = ['title' => '保温板' , 'name' => $details['name'], 'specifications' => '1m*0.5m', 'number' => $v, 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $v, 'weights' => $details['weight'] * $v ,'weight' => $details['weight'],'code' => $details['number']??'无', 'type' => 7];
            }elseif ($k == 'warm3' and $v > 0){
                $details = WaterMaterialScience::where('cate_id', $data['warm'])->where([['long', '=', 0.5], ['wide', '=', 0.5]])->where($where)->find();
                $zj += $details['unit_price'] * $v;
                $zzl += $details['weight'] * $v;
                $table[] = ['title' => '保温板' , 'name' => $details['name'], 'specifications' => '0.5m*0.5m', 'number' => $v, 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $v, 'weights' => $details['weight'] * $v ,'weight' => $details['weight'],'code' => $details['number']??'无', 'type' => 7];
            }

        }
        //槽钢
        if ($data['is_channel'] > 0){
            $details = WaterMaterialScience::where('cate_id', $data['channel'])->where($where)->find();
            $zj += $details['unit_price'] * $calculation['channel_number'];
            $zzl += $details['weight'] * $calculation['channel_number'];
            $table[] = ['title' => '槽钢' , 'name' => $details['name'], 'specifications' => $details['company'], 'number' => $calculation['channel_number'], 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $calculation['channel_number'], 'weights' => $details['weight'] * $calculation['channel_number'] ,'weight' => $details['weight'],'code' => $details['number']??'无', 'type' => 8];
        }

        // 扶梯
        if ($data['is_human_ladder'] > 0){
            $details = WaterMaterialScience::where('cate_id', 9)->find();
            $zj += $details['unit_price'] * $calculation['ladder'];
            $zzl += $details['weight'] * $calculation['ladder'];
            $table[] = ['title' => '扶梯' , 'name' => $details['name'], 'specifications' => $details['company'], 'number' => $calculation['ladder'], 'unit_price' => $details['unit_price'], 'company' => $details['company'], 'ot_price' => $details['unit_price'] * $calculation['ladder'], 'weights' => $details['weight'] * $calculation['ladder'] ,'weight' => $details['weight'],'code' => $details['number']??'无', 'type' => 9];
        }

        foreach ($table as &$i){
            $i['ot_price'] = round($i['ot_price'], 2);
            $i['weight'] = round($i['weight'], 2);
        }
        if ($data['is_warm']){
            $zj += 169;
        }else{
            $zj += 105;
        }
        $zzl = $zzl * (sys_config('weight')/100);

        if ($discount > 0){
            foreach ($table as &$item)
            {
                $item['unit_price'] = $item['unit_price'] * $discount;
                $item['ot_price'] = $item['ot_price'] * $discount;
            }
            return ['table' => $table, 'zj' => round($zj * $discount, 2), 'zzl' => round($zzl,2)];
        }else{
            return ['table' => $table, 'zj' => round($zj, 2), 'zzl' => round($zzl, 2)];
        }
    }




    /**计算使用材料数量
     * @param $data
     * @return array
     */
    public function calculation($data)
    {

        $long = (double)$data['long'];
        $wide = (double)$data['wide'];
        $high = (double)$data['high'];
        $high_array = []; // 高度数组
        $roof_number = []; // 顶板数量
        $floor_number = 0; // 底板数量
        $side_number = []; // 侧板数量
        $lacing_number = []; // 拉经数量
        $fula_number = [];//辅拉数量
        $column_number = 0;// 立柱数量
        $channel_number = 0; // 槽钢
        $ladder = 0;// 扶梯管米数
        for ($i = 0; $i < intval($high); $i++){
            $high_array[] = 1;
        }
        if (intval($high) != $high){
            $high_array = array_merge($high_array, [0.5]);
        }
        $floor_number = $long * $wide;
        if (ceil($long) == $long && ceil($wide) == $wide && ceil($high) == $high){
            //都为整数
            $roof_number = ['1*1' =>  $long * $wide]; // 顶板数量
//            $floor_number = ['1*1' => $long * $wide];// 底板数量
            foreach ($high_array as $item){
                $side_number[] = ['1*1' => (($item * $long) + ($item * $wide)) * 2];
            }
        }elseif (ceil($long) != $long && ceil($wide) != $wide && ceil($high) != $high){
            //都不为整数
            $roof_number = ['1*1' =>  intval($long) * intval($wide), '1*0.5' => intval($long) + intval($wide), '0.5*0.5' => 1]; // 顶板数量
//            $floor_number = ['1*1' =>  intval($long) * intval($wide), '1*0.5' => intval($long) + intval($wide), '0.5*0.5' => 1];// 底板数量
            foreach ($high_array as $item){
                if ($item >= 1){
                    $side_number[] = ['1*1' => ((intval($long)) + (intval($wide))) * 2, '1*0.5' => 4];
                }else{
                    $side_number[] = ['1*0.5' => ((intval($long)) + (intval($wide))) * 2, '0.5*0.5' => 4];
                }
            }
        }elseif (ceil($long) != $long || ceil($wide) != $wide || ceil($high) != $high){
            //一个整数
            if (ceil($long) != $long && ceil($wide) == $wide && ceil($high) == $high){
                $roof_number = ['1*1' =>  intval($long) * intval($wide), '1*0.5' => $wide]; // 顶板数量
//                $floor_number = ['1*1' => intval($long) * intval($wide), '1*0.5' => $wide];// 底板数量
                foreach ($high_array as $item){
                    $side_number[] = ['1*1' => ((intval($long)) + (intval($wide))) * 2, '1*0.5' => 2];
                }
            }elseif (ceil($long) == $long && ceil($wide) != $wide and ceil($high) == $high){
                $roof_number = ['1*1' =>  intval($long) * intval($wide), '1*0.5' => $long]; // 顶板数量
//                $floor_number = ['1*1' => intval($long) * intval($wide), '1*0.5' => $long];// 底板数量
                foreach ($high_array as $item){
                    $side_number[] = ['1*1' => ((intval($long)) + (intval($wide))) * 2, '1*0.5' => 2];
                }
            }elseif (ceil($long) != $long && ceil($wide) != $wide and ceil($high) == $high){
                $roof_number = ['1*1' =>  intval($long) * intval($wide), '1*0.5' => intval($long) + intval($wide), '0.5*0.5' => 1]; // 顶板数量
//                $floor_number = ['1*1' =>  intval($long) * intval($wide), '1*0.5' => intval($long) + intval($wide), '0.5*0.5' => 1];// 底板数量
                foreach ($high_array as $item){
                    $side_number[] = ['1*1' => ((intval($long)) + (intval($wide))) * 2, '1*0.5' => 4];
                }
            } elseif (ceil($long) != $long && ceil($wide) == $wide and ceil($high) != $high){
                $roof_number = ['1*1' =>  intval($long) * intval($wide), '1*0.5' => $wide]; // 顶板数量
//                $floor_number = ['1*1' => intval($long) * intval($wide), '1*0.5' => $wide];// 底板数量
                foreach ($high_array as $item){
                    if ($item >= 1){
                        $side_number[] = ['1*1' => ((intval($long)) + (intval($wide))) * 2, '1*0.5' => 2];
                    }else{
                        $side_number[] = ['1*0.5' => ((intval($long)) + (intval($wide))) * 2, '0.5*0.5' => 2];
                    }
                }
            }elseif (ceil($long) == $long && ceil($wide) != $wide and ceil($high) != $high){
                $roof_number = ['1*1' =>  intval($long) * intval($wide), '1*0.5' => $long]; // 顶板数量
//                $floor_number = ['1*1' => intval($long) * intval($wide), '1*0.5' => $long];// 底板数量
                foreach ($high_array as $item){
                    if ($item >= 1){
                        $side_number[] = ['1*1' => ((intval($long)) + (intval($wide))) * 2, '1*0.5' => 2];
                    }else{
                        $side_number[] = ['1*0.5' => ((intval($long)) + (intval($wide))) * 2, '0.5*0.5' => 2];
                    }
                }
            }elseif (ceil($long) == $long && ceil($wide) == $wide and ceil($high) != $high){
                $roof_number = ['1*1' =>  $long * $wide]; // 顶板数量
//                $floor_number = ['1*1' => $long * $wide];// 底板数量
                foreach ($high_array as $item){
                    if ($item >= 1){
                        $side_number[] = ['1*1' => ((intval($long)) + (intval($wide))) * 2];
                    }else{
                        $side_number[] = ['1*0.5' => ((intval($long)) + (intval($wide))) * 2];
                    }
                }
            }
        }

        array_pop($high_array);// 删除最后一位,算出主拉层数
        // 拉筋
        foreach ($high_array as $item){
            $lacing_number[] = ceil((($long * (ceil($wide) - 1)) + ($wide * (ceil($long) - 1)))/2);
        }
        // 辅拉
        if ($high > 1){
            if ($high == 1.5){
                $fula_number = [2 * ( (ceil($long) - 1) + (ceil($wide) - 1) ) + 1];
            }elseif ($high == 2){
                $fula_number = [3 * ( (ceil($long) - 1) + (ceil($wide) - 1) ) + 1];
            }elseif ($high == 2.5){
                $fula_number = [
                    2 * ( (ceil($long) - 1) + (ceil($wide) - 1) ) + 1,
                    2 * ( (ceil($long) - 1) + (ceil($wide) - 1) ) + 1,
                ];
            }elseif ($high == 3){
                $fula_number = [
                    3 * ( (ceil($long) - 1) + (ceil($wide) - 1) ) + 1,
                    2 * ( (ceil($long) - 1) + (ceil($wide) - 1) ) + 1,
                ];
            }elseif ($high >= 3.5){
                foreach ($high_array as $item){
                    $fula_number[] = 2 * ( (ceil($long) - 1) + (ceil($wide) - 1) ) + 1;
                }
            }
        }
        //立柱
        $column_number = ceil(((ceil($long) - 1) * (ceil($wide) -1) * $high) / 2);
        //保温板数量
        $warm1 = 0;// 1*1 数量
        $warm2 = 0;// 1*0.5数量
        $warm3 = 0;// 0.5*0.5数量
        if ($data['is_warm'] > 0){
            foreach ($side_number as $item){
                // 侧板
                foreach ($item as $key => $value){
                    if ($key == '1*1'){
                        $warm1 += $value;
                    }elseif ($key == '1*0.5'){
                        $warm2 += $value;
                    }elseif ($key == '0.5*0.5'){
                        $warm3 += $value;
                    }
                }
            }
            foreach ($roof_number as $key => $value){
                // 顶板
                if ($key == '1*1'){
                    $warm1 += $value;
                }elseif ($key == '1*0.5'){
                    $warm2 += $value;
                }elseif ($key == '0.5*0.5'){
                    $warm3 += $value;
                }
            }
        }
        //槽钢
        if ($data['is_channel'] > 0){
            if (ceil($long) == $long && ceil($wide) == $wide && ceil($high) == $high){
                //都为整数
                $channel_number = $long * ($wide * 2 + 1) + $wide * 2;

            }elseif (ceil($long) != $long || ceil($wide) || $wide && ceil($high) || $high){
                //不为整数
                $channel_number = $long * (ceil($wide) * 2 + 1) + $wide * 2;
            }
        }
        // 扶梯管
        if ($data['is_human_ladder'] > 0){
            if ($high <= 1.5){
                $ladder = 5;
            }elseif ($high == 2){
                $ladder = 7;
            }elseif ($high >= 2.5){
                $ladder = $high * 4;
            }
        }
        $calculation = [
            'roof_number' => $roof_number,// 顶板
            'floor_number' => $floor_number,// 底板
            'side_number' => Array_reverse($side_number),// 侧板
            'lacing_number' => $lacing_number,// 拉筋
            'fula_number' => $fula_number,// 辅拉
            'column_number' => $column_number,// 立柱
            'warm' => ['warm1' => $warm1,'warm2' => $warm2,'warm3' => $warm3],// 保温
            'channel_number' => $channel_number,// 槽钢
            'ladder' => $ladder,// 保温
        ];

        return $calculation;

    }


    /**
     * 材料列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list()
    {
        $data = UtilService::getMore(['is_gc' , 0]);
        if ($data['is_gc'] > 0){
            $where[] = ['is_gc', '=' ,1];
        }else{
            $where[] = ['is_gc', '=' ,0];
        }
        $list = [];
        $list['warm'] = WaterCate::field('id,name')->where('type', 7)->where('is_show', 1)->select(); // 保温
        $list['channel'] =  WaterCate::field('id,name')->where('type', 8)->where('is_show', 1)->select(); // 槽钢
        $list['roof'] =  WaterCate::field('id,name')->where('type', 1)->where($where)->where('is_show', 1)->select();// 顶板
        $list['side_plate'] =  WaterCate::field('id,name')->where('type', 2)->where('is_show', 1)->where($where)->select();// 侧板
        $list['lacing'] =  WaterCate::field('id,name')->where('type', 3)->where('is_show', 1)->where($where)->select();// 拉筋
        $list['floor'] =  WaterCate::field('id,name')->where('type', 4)->where('is_show', 1)->where($where)->select(); // 底板
        $list['column'] =  WaterCate::field('id,name')->where('type', 5)->where('is_show', 1)->where($where)->select(); // 立柱
        $list['fula'] =  WaterCate::field('id,name')->where('type', 6)->where('is_show', 1)->where($where)->select(); // 辅拉

        return app('json')->success($list);
    }

    /**
     * 计算记录
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function query_list(Request $request)
    {
        $list = WaterQuery::where('uid', $request->uid())->select();
        $list = count($list) > 0 ? $list->toArray(): [];
        return app('json')->success($list);
    }

    /**
     * 计算详情
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function query_details(Request $request)
    {
        $data = UtilService::getMore(['id']);
        if (!$data['id'])  return app('json')->fail('传入正确参数');

        $list = WaterMaterial::where('query_id', $data['id'])->select();
        return app('json')->success($list->toArray());
    }


    /**
     * 默认选项
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function default(Request $request)
    {
        $data = UtilService::getMore(['high', 'is_gc']);
        if ($data['is_gc'] == 1){
            $where[] = ['is_gc', '=', 1];
        }else{
            $where[] = ['is_gc', '=', 0];
        }
        $list = WaterDefault::where('high', $data['high'])->where($where)->find();


        if ($list){
            $list['roof'] = ['id' => $list['roof'] , 'name' => WaterCate::where('id', $list['roof'])->value('name')];
            $list['floor'] = ['id' => $list['floor'] , 'name' => WaterCate::where('id', $list['floor'])->value('name')];
            $list['column'] = ['id' => $list['column'] , 'name' => WaterCate::where('id', $list['column'])->value('name')];


            $list['side_plate'] = explode(',', $list['side_plate']);
            $array = [];
            foreach ($list['side_plate'] as $item){
                $array[] = ['id' => $item , 'name'=> WaterCate::where('id', $item)->value('name')];
            }
            $list['side_plate'] = $array;
            $list['lacing'] = explode(',', $list['lacing']);
            $array = [];
            foreach ($list['lacing'] as $item){
                $array[] = ['id' => $item , 'name'=> WaterCate::where('id', $item)->value('name')];
            }
            $list['lacing'] = $array;
            $list['fula'] = explode(',', $list['fula']);
            $array = [];
            foreach ($list['fula'] as $item){
                $array[] = ['id' => $item , 'name'=> WaterCate::where('id', $item)->value('name')];
            }
            $list['fula'] = $array;
            $list = $list->toArray();
        }else{
            $list = [];
        }
        return app('json')->success($list);
    }


}