|
- <?php
-
- namespace backend\modules\api\models;
-
- use yii\base\Exception;
- use yii\db\ActiveRecord;
- use Yii;
- use yii\db\Expression;
-
- /**
- * This is the model class for table "opera_product_run".
- *
- * @property integer $PROD_RUN_ID
- * @property integer $PROD_ID
- * @property integer $PROD_TYPE
- * @property integer $TO_ORG_ID
- * @property integer $ORG_ID
- * @property integer $CANCEL_FLAG
- * @property integer $CREATE_USER_ID
- * @property integer $UPDATE_USER_ID
- * @property string $PROD_PRICE
- * @property string $CUS_PRICE
- * @property string $REMARK
- * @property string $CREATE_TIME
- * @property string $UPDATE_TIME
- * @property string $RUN_DATE
- * @property string $RUN_TIME
- * @property integer $TOTAL_COUNT
- * @property integer $SALED_COUNT
- * @property integer $IS_ONSALE
- */
- class OperaProductRun extends ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'opera_product_run';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['PROD_ID', 'PROD_TYPE', 'TO_ORG_ID', 'ORG_ID', 'CANCEL_FLAG', 'CREATE_USER_ID', 'UPDATE_USER_ID', 'TOTAL_COUNT', 'SALED_COUNT', 'IS_ONSALE'], 'integer'],
- [['PROD_PRICE', 'CUS_PRICE'], 'number'],
- [['REMARK'], 'required'],
- [['REMARK'], 'string'],
- [['CREATE_TIME', 'UPDATE_TIME'], 'safe'],
- [['RUN_DATE', 'RUN_TIME'], 'string', 'max' => 20],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'PROD_RUN_ID' => 'Prod Run ID',
- 'PROD_ID' => '产品ID',
- 'PROD_TYPE' => '产品类别,DICT_TYPE.ID',
- 'TO_ORG_ID' => '所属一级组织机构ID,BASE_ORGANIZATION.ORG_ID,非0',
- 'ORG_ID' => '直属组织机构ID,BASE_ORGANIZATION.ORG_ID,非0',
- 'CANCEL_FLAG' => '记录有效性标记,CANCEL_FLAG=0记录有效;CANCEL_FLAG=1,记录已删除',
- 'CREATE_USER_ID' => '记录创建用户ID',
- 'UPDATE_USER_ID' => '记录最后更新用户ID',
- 'PROD_PRICE' => '结算价格',
- 'CUS_PRICE' => '零售价',
- 'REMARK' => '备注',
- 'CREATE_TIME' => '创建时间',
- 'UPDATE_TIME' => '更新时间',
- 'RUN_DATE' => '日期',
- 'RUN_TIME' => '时间',
- 'TOTAL_COUNT' => '库存数',
- 'SALED_COUNT' => '已售数',
- 'IS_ONSALE' => '门票是否上架,0下架,1上架',
- ];
- }
-
- /**
- * Function Description:获取产品详细信息并整理
- * Function Name: getTicketProductListByTicketDate
- * @param $main_prod_id
- * @param $run_date
- * @param $prod_arr
- *
- * @return array|bool|ActiveRecord[]
- *
- * @author 张帅
- */
- public function getTicketProductListByTicketDate($main_prod_id, $run_date, $prod_arr,$to_org_id)
- {
- #region 获取所有票种id
- $prod_id_arr = [];
- foreach ($prod_arr as $key => $vel) {
- $prod_id_arr[] = $vel['prod_id'];
- }
- #endregion
-
- $result = [];
- foreach($prod_id_arr as $sub_prod_id){
- $prod = $this->getProdInfo($main_prod_id,$sub_prod_id,$run_date,$to_org_id);
- if($prod){
- $result[$prod['prod_id']] = $prod;
- }
- }
-
- #region 获取数据详情
- // $result = self::find()
- // ->select([
- // 'p.prod_id',//票种id
- // 'parent_prod_id' => 'p.parent_id',//父票种id
- // 'p.prod_code',
- // 'p.prod_name',
- // 'p.prod_type',
- // 'pr.run_date',//日期
- // 'pr.run_time',//时间
- // 'pr.cus_price',//销售价
- // 'pr.prod_price',//成本价
- // 'pr.total_count',//库存
- // 'pr.saled_count',//已售
- // ])
- // ->from(self::tableName() . ' as pr')
- // ->leftJoin(OperaProduct::tableName() . ' as p', 'p.prod_id = pr.prod_id')
- // ->where([
- // 'and',
- // ['in', 'p.prod_id', $prod_id_arr],
- // ['=', 'p.parent_id', $main_prod_id],
- // ['=', 'pr.run_date', $run_date],
- // ['=', 'p.cancel_flag', 0],
- // ['=', 'pr.cancel_flag', 0],
- // ['=', 'p.is_onsale', 1],
- // ['=', 'pr.is_onsale', 1],
- // ])
- // ->groupBy(['prod_id'])
- // ->indexBy('prod_id')
- // ->asArray()->all();
- #endregion
-
- #region 判断售卖数据是否和数据库数据相同
- if (count($result) !== count($prod_arr)) {
- $result = [];
- $result['code'] = '1';
- $result['info'] = '产品有误';
- return $result;
- }
- #endregion
-
- #region 判断库存并补充数据
- foreach ($result as $key => $vel) {
- if ($vel['total_count'] - $vel['saled_count'] < $prod_arr[$vel['prod_id']]['num']) {
- $result = [];
- $result['code'] = '1';
- $result['info'] = '库存不足';
- return $result;
- } else {
- $result[$key]['order_price'] = $prod_arr[$vel['prod_id']]['price'];
- $result[$key]['people_num'] = $prod_arr[$vel['prod_id']]['num'];
- }
- }
- #endregion
-
- return $result;
- }
-
- /**
- * Function Description:定时任务将outside_ticket同步到opera_product_run
- * Function Name: uptRun
- *
- * @return int
- *
- * @author 娄梦宁
- */
- public function uptRun()
- {
- try {
- $date = date('Y-m-d', strtotime('-1 day'));
- $sql = '' . "insert into opera_product_run (`PROD_ID`,`ORG_ID`,`PROD_TYPE`,`PROD_PRICE`,`REMARK`,`RUN_DATE`,`TOTAL_COUNT`)
- select b.prod_id,1369,b.prod_type,a.agent_price,b.remark,a.ticket_date,a.ticket_num from outside_ticket a left join opera_product b on a.ticket_code=b.prod_code
- where a.ticket_date>'$date' and b.ORG_ID=1369 and not exists (select * from opera_product_run where PROD_ID=b.prod_id and run_date=a.ticket_date) group by a.id";
- Yii::$app->db->createCommand($sql)->execute();
- $sql = '' . "update opera_product_run a LEFT join (select b.prod_id,a.agent_price,a.ticket_date,a.ticket_num from outside_ticket a left join opera_product b on a.ticket_code=b.prod_code
- where a.ticket_date>'$date' and b.ORG_ID=1369 and exists (select * from opera_product_run where PROD_ID=b.prod_id and run_date=a.ticket_date) group by a.id
- ) b on a.run_date=b.ticket_date and a.prod_id=b.prod_id
- set TOTAL_COUNT=b.ticket_num,a.PROD_PRICE=b.agent_price where a.org_id=1369";
- Yii::$app->db->createCommand($sql)->execute();
- return 1;
- } catch (Exception $e) {
- return 2;
- }
- }
-
- /**
- * Des:获取详细信息
- * Name: getRunInfoByProdIdAndDate
- * @param $prod
- * @param $date
- * @return array
- * @author 倪宗锋
- */
- public function getRunInfoByProdIdAndDate($prod, $date)
- {
- $select = [
- 'prod_price',
- 'cus_price',
- 'total_count',
- 'is_onsale'
- ];
- $where = [
- 'and',
- ['=', 'PROD_ID', $prod],
- ['=', 'RUN_DATE', $date]
- ];
- $getInfo = self::find()->select($select)
- ->where($where)
- ->asArray()
- ->one();
- return $getInfo;
- }
-
- /**
- * Function Description:获取具体日期子票种信息
- * Function Name: getProdInfo
- * @param $main_prod_id
- * @param $sub_prod_id
- * @param $run_date
- * @param $to_org_id
- *
- * @return array|null|ActiveRecord
- *
- * @author 冒炎
- */
- public function getProdInfo($main_prod_id,$sub_prod_id,$run_date,$to_org_id){
- $select = [
- 'a.run_time',
- 'a.run_date',
- 'a.prod_price',
- 'a.total_count',
- 'a.saled_count',
- 'a.is_onsale',
- 'a.cus_price',
- 'base_price'=>'a.cus_price',
- '(a.total_count-a.saled_count) as stock',
- 'b.prod_id',
- 'sub_prod_id'=>'b.prod_id',
- 'parent_prod_id'=>'b.parent_id',
- 'main_prod_id'=>'b.parent_id',
- 'b.prod_code',
- 'b.prod_name',
- 'b.prod_type',
-
- ];
- $where1 = [
- 'and',
- ['=','a.to_org_id',$to_org_id],
- ['=', 'a.prod_id', $sub_prod_id],
- ['=', 'b.parent_id', $main_prod_id],
- ['=', 'a.run_date', $run_date],
- ['=', 'a.cancel_flag', 0],
- ['=', 'b.cancel_flag', 0],
- // ['=', 'a.is_onsale', 1],
- ['=', 'b.is_onsale', 1],
- ];
- $where2 = [
- 'and',
- ['=','a.to_org_id',0],
- ['=', 'a.prod_id', $sub_prod_id],
- ['=', 'b.parent_id', $main_prod_id],
- ['=', 'a.run_date', $run_date],
- ['=', 'a.cancel_flag', 0],
- ['=', 'b.cancel_flag', 0],
- ['=', 'a.is_onsale', 1],
- ['=', 'b.is_onsale', 1],
- ];
- $res1 = self::find()
- ->select($select)
- ->from(self::tableName() . ' a')
- ->leftJoin('opera_product as b','a.prod_id = b.prod_id')
- ->where($where1)
- ->asArray()
- ->one();
- $res2 = self::find()
- ->select($select)
- ->from(self::tableName() . ' a')
- ->leftJoin('opera_product as b','a.prod_id = b.prod_id')
- ->where($where2)
- ->asArray()
- ->one();
- if($res1){
- if($res1['is_onsale'] == 0){
- return [];
- }else{
- return $res1;
- }
- }else{
- if(!empty($res2)&&$res2['is_onsale'] == 0){
- return [];
- }else{
- return $res2;
- }
- }
-
- }
- }
|