<?php
/**
 * 客户调价单管理
 * Created by PhpStorm.
 * User: phperstar
 * Date: 2019/4/26
 * Time: 9:41 AM
 */

namespace JinDouYun\Controller\Price;

use JinDouYun\Controller\BaseController;
use Mall\Framework\Core\StatusCode;
use Mall\Framework\Core\ErrorCode;

use JinDouYun\Model\Price\MCustomerPriceAdjustment;

class CustomerPriceAdjustment extends BaseController
{
    private $objMCustomerPriceAdjustment;

    /**
     * CustomerPriceAdjustment constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     * @throws \Exception
     */
    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMCustomerPriceAdjustment = new MCustomerPriceAdjustment($this->onlineUserId, $this->onlineEnterpriseId);
    }

    /**
     * 公共接收参数方法
     * @return mixed
     */
    public function commonParams()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        foreach ($params as $key => $value) {
            $sheetData[$key] = [
                'createUserId'    => $this->onlineUserId,
                'createUserName'  => isset($value['createUserName']) ? $value['createUserName'] : '',
                'goodsName'       => isset($value['goodsName']) ? $value['goodsName'] : '',
                'goodsId'         => isset($value['goodsId']) ? $value['goodsId'] : '',
                'shopId'          => isset($value['shopId']) ? $value['shopId'] : '',
                'shopName'        => isset($value['shopName']) ? $value['shopName'] : '',
                'salePrice'       => isset($value['salePrice']) ? $value['salePrice'] : '',
                'createTime'      => time(),
                'updateTime'      => time(),
                'deleteStatus'    => StatusCode::$standard,
                'effectiveStatus' => StatusCode::$auditStatus['auditing'],
                'customerName'    => isset($value['customerName']) ? $value['customerName'] : '',
                'customerId'      => isset($value['customerId']) ? $value['customerId'] : '',
            ];

            foreach ($sheetData[$key] as $k => $v) {
                if (empty($v) && $v !== 0) {
                    $this->sendOutput($k . '参数错误', ErrorCode::$paramError);
                }
            }

            $sheetData[$key]['createTime'] = time();
            $sheetData[$key]['updateTime'] = time();
            $sheetData[$key]['startTime'] = isset($value['startTime']) ? $value['startTime'] : '';
            $sheetData[$key]['endTime'] = isset($value['endTime']) ? $value['endTime'] : '';
            $sheetData[$key]['goodsCode'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $sheetData[$key]['goodsId'], StatusCode::$code['goodsBasic']['length']);
            $sheetData[$key]['salePrice'] = json_encode($sheetData[$key]['salePrice']);
            $sheetData[$key]['effective'] = getArrayItem($value,'effective',StatusCode::$delete);
        }

        return $sheetData;
    }

    /**
     * 新增客户调价单
     *
     * @throws \Exception
     */
    public function add()
    {
        $sheetData = self::commonParams();
        $result = $this->objMCustomerPriceAdjustment->add($sheetData);
        if ($result->isSuccess()) {
            //调价单生效操作
            $sheetIds = $result->getData();
            foreach ($sheetIds as $key => $id){
                $row = getArrayItem($sheetData,$key,[]);
                if($row['effective'] == StatusCode::$standard){
                    $effectiveResult = $this->objMCustomerPriceAdjustment->effective($id,$row);
                    if (!$effectiveResult->isSuccess()){
                        parent::sendOutput($effectiveResult->getData());
                    }
                }
            }
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 获取指定用户区域可售商品
     */
    public function goodsLists()
    {
        $params['provinceId'] = $this->request->param('provinceId');
        $params['cityId'] = $this->request->param('cityId');
        $params['districtId'] = $this->request->param('districtId');
        $params['shopId'] = $this->request->param('shopId');
        $params['page'] = $this->request->param('page');
        $params['pageSize'] = $this->request->param('pageSize');

        foreach ($params as $k => $v) {
            if (empty($v) && $v !== 0) {
                $this->sendOutput($k . '参数错误', ErrorCode::$paramError);
            }
        }

        $params['keyword'] = $this->request->param('keyword') ?: '';

        $result = $this->objMCustomerPriceAdjustment->goodsLists($params);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize'  => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 所有调价单列表
     */
    public function getAll()
    {
        $params['page'] = $this->request->param('page') ?: 1;
        $params['pageSize'] = $this->request->param('pageSize') ?: 10;
        $params['startTime'] = $this->request->param('startTime');
        $params['endTime'] = $this->request->param('endTime');
        $params['effectiveStatus'] = $this->request->param('effectiveStatus');
        $params['keyword'] = $this->request->param('keyword');
        $params['goodsId'] = $this->request->param('goodsId');
        $export = $this->request->param('export');
        if (isset($this->shopId) && !empty($this->shopId)) $params['shopId'] = $this->shopId;
        $result = $this->objMCustomerPriceAdjustment->getAll($params,$export);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize'  => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 生效操作
     * @throws \Exception
     */
    public function effective()
    {
        $id = $this->request->param('request_id');
        if (!$id) {
            parent::sendOutput('请指定要生效的数据id', ErrorCode::$paramError);
        }

        $paramsData=$this->request->getRawJson();
        $params=[
            'createTime'=>isset($paramsData['createTime']) ? $paramsData['createTime']: '',
            'effectiveUserName'=>isset($paramsData['effectiveUserName']) ? $paramsData['effectiveUserName']: '',
            'effectiveUserId'=>$this->onlineUserId,
        ];
        foreach ($params as $k => $v) {
            if (empty($v) && $v !== 0) {
                $this->sendOutput($k . '参数错误', ErrorCode::$paramError);
            }
        }

        $result = $this->objMCustomerPriceAdjustment->effective($id,$params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 客户调价单搜索
     */
    public function search()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $selectParams = [
            'keyword'         => isset($params['keyword']) ? $params['keyword'] : '',//订单编号，收货人，商品名称
            'startTime'       => isset($params['startTime']) ? $params['startTime'] : '',//开始时间
            'endTime'         => isset($params['endTime']) ? $params['endTime'] : '',//结束时间
            'effectiveStatus' => isset($params['effectiveStatus']) ? $params['effectiveStatus'] : '',//生效状态
        ];
        $pageParams = pageToOffset(isset($params['page']) ? $params['page'] : 1, isset($params['pageSize']) ? $params['pageSize'] : 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        $result = $this->objMCustomerPriceAdjustment->search($selectParams);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => isset($params['page']) ? $params['page'] : 1,
                'pageSize'  => isset($params['pageSize']) ? $params['pageSize'] : 10,
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
    
    
    /**
     * 获取指定商品的最后生效的客户调价单数据
     */
    public function getCustomerPriceByGoodsIds()
    {
        $params['page'] = $this->request->param('page') ?: 1;
        $params['pageSize'] = $this->request->param('pageSize') ?: 10;
        $params['goodsId'] = $this->request->param('goodsId');
        $result = $this->objMCustomerPriceAdjustment->getCustomerPriceByGoodsIds($params);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize'  => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
    
    
    /**
     * 删除指定商品的最后生效的客户调价单数据
     */
    public function delCustomerPrice()
    {
        $paramsDate = $this->request->getRawJson();

        $params['id']    = $paramsDate['id'];
        $params['skuId'] = $paramsDate['skuId'];
        foreach ($params as $k => $v) {
            if (empty($v) && $v !== 0) {
                $this->sendOutput($k . '参数错误', ErrorCode::$paramError);
            }
        }
        $result = $this->objMCustomerPriceAdjustment->delCustomerPrice($params);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
}