Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

309 righe
10 KiB

  1. <?php
  2. namespace backend\modules\api\models;
  3. use yii\base\Exception;
  4. use yii\db\ActiveRecord;
  5. use Yii;
  6. use yii\db\Expression;
  7. /**
  8. * This is the model class for table "opera_product_run".
  9. *
  10. * @property integer $PROD_RUN_ID
  11. * @property integer $PROD_ID
  12. * @property integer $PROD_TYPE
  13. * @property integer $TO_ORG_ID
  14. * @property integer $ORG_ID
  15. * @property integer $CANCEL_FLAG
  16. * @property integer $CREATE_USER_ID
  17. * @property integer $UPDATE_USER_ID
  18. * @property string $PROD_PRICE
  19. * @property string $CUS_PRICE
  20. * @property string $REMARK
  21. * @property string $CREATE_TIME
  22. * @property string $UPDATE_TIME
  23. * @property string $RUN_DATE
  24. * @property string $RUN_TIME
  25. * @property integer $TOTAL_COUNT
  26. * @property integer $SALED_COUNT
  27. * @property integer $IS_ONSALE
  28. */
  29. class OperaProductRun extends ActiveRecord
  30. {
  31. /**
  32. * @inheritdoc
  33. */
  34. public static function tableName()
  35. {
  36. return 'opera_product_run';
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['PROD_ID', 'PROD_TYPE', 'TO_ORG_ID', 'ORG_ID', 'CANCEL_FLAG', 'CREATE_USER_ID', 'UPDATE_USER_ID', 'TOTAL_COUNT', 'SALED_COUNT', 'IS_ONSALE'], 'integer'],
  45. [['PROD_PRICE', 'CUS_PRICE'], 'number'],
  46. [['REMARK'], 'required'],
  47. [['REMARK'], 'string'],
  48. [['CREATE_TIME', 'UPDATE_TIME'], 'safe'],
  49. [['RUN_DATE', 'RUN_TIME'], 'string', 'max' => 20],
  50. ];
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function attributeLabels()
  56. {
  57. return [
  58. 'PROD_RUN_ID' => 'Prod Run ID',
  59. 'PROD_ID' => '产品ID',
  60. 'PROD_TYPE' => '产品类别,DICT_TYPE.ID',
  61. 'TO_ORG_ID' => '所属一级组织机构ID,BASE_ORGANIZATION.ORG_ID,非0',
  62. 'ORG_ID' => '直属组织机构ID,BASE_ORGANIZATION.ORG_ID,非0',
  63. 'CANCEL_FLAG' => '记录有效性标记,CANCEL_FLAG=0记录有效;CANCEL_FLAG=1,记录已删除',
  64. 'CREATE_USER_ID' => '记录创建用户ID',
  65. 'UPDATE_USER_ID' => '记录最后更新用户ID',
  66. 'PROD_PRICE' => '结算价格',
  67. 'CUS_PRICE' => '零售价',
  68. 'REMARK' => '备注',
  69. 'CREATE_TIME' => '创建时间',
  70. 'UPDATE_TIME' => '更新时间',
  71. 'RUN_DATE' => '日期',
  72. 'RUN_TIME' => '时间',
  73. 'TOTAL_COUNT' => '库存数',
  74. 'SALED_COUNT' => '已售数',
  75. 'IS_ONSALE' => '门票是否上架,0下架,1上架',
  76. ];
  77. }
  78. /**
  79. * Function Description:获取产品详细信息并整理
  80. * Function Name: getTicketProductListByTicketDate
  81. * @param $main_prod_id
  82. * @param $run_date
  83. * @param $prod_arr
  84. *
  85. * @return array|bool|ActiveRecord[]
  86. *
  87. * @author 张帅
  88. */
  89. public function getTicketProductListByTicketDate($main_prod_id, $run_date, $prod_arr,$to_org_id)
  90. {
  91. #region 获取所有票种id
  92. $prod_id_arr = [];
  93. foreach ($prod_arr as $key => $vel) {
  94. $prod_id_arr[] = $vel['prod_id'];
  95. }
  96. #endregion
  97. $result = [];
  98. foreach($prod_id_arr as $sub_prod_id){
  99. $prod = $this->getProdInfo($main_prod_id,$sub_prod_id,$run_date,$to_org_id);
  100. if($prod){
  101. $result[$prod['prod_id']] = $prod;
  102. }
  103. }
  104. #region 获取数据详情
  105. // $result = self::find()
  106. // ->select([
  107. // 'p.prod_id',//票种id
  108. // 'parent_prod_id' => 'p.parent_id',//父票种id
  109. // 'p.prod_code',
  110. // 'p.prod_name',
  111. // 'p.prod_type',
  112. // 'pr.run_date',//日期
  113. // 'pr.run_time',//时间
  114. // 'pr.cus_price',//销售价
  115. // 'pr.prod_price',//成本价
  116. // 'pr.total_count',//库存
  117. // 'pr.saled_count',//已售
  118. // ])
  119. // ->from(self::tableName() . ' as pr')
  120. // ->leftJoin(OperaProduct::tableName() . ' as p', 'p.prod_id = pr.prod_id')
  121. // ->where([
  122. // 'and',
  123. // ['in', 'p.prod_id', $prod_id_arr],
  124. // ['=', 'p.parent_id', $main_prod_id],
  125. // ['=', 'pr.run_date', $run_date],
  126. // ['=', 'p.cancel_flag', 0],
  127. // ['=', 'pr.cancel_flag', 0],
  128. // ['=', 'p.is_onsale', 1],
  129. // ['=', 'pr.is_onsale', 1],
  130. // ])
  131. // ->groupBy(['prod_id'])
  132. // ->indexBy('prod_id')
  133. // ->asArray()->all();
  134. #endregion
  135. #region 判断售卖数据是否和数据库数据相同
  136. if (count($result) !== count($prod_arr)) {
  137. $result = [];
  138. $result['code'] = '1';
  139. $result['info'] = '产品有误';
  140. return $result;
  141. }
  142. #endregion
  143. #region 判断库存并补充数据
  144. foreach ($result as $key => $vel) {
  145. if ($vel['total_count'] - $vel['saled_count'] < $prod_arr[$vel['prod_id']]['num']) {
  146. $result = [];
  147. $result['code'] = '1';
  148. $result['info'] = '库存不足';
  149. return $result;
  150. } else {
  151. $result[$key]['order_price'] = $prod_arr[$vel['prod_id']]['price'];
  152. $result[$key]['people_num'] = $prod_arr[$vel['prod_id']]['num'];
  153. }
  154. }
  155. #endregion
  156. return $result;
  157. }
  158. /**
  159. * Function Description:定时任务将outside_ticket同步到opera_product_run
  160. * Function Name: uptRun
  161. *
  162. * @return int
  163. *
  164. * @author 娄梦宁
  165. */
  166. public function uptRun()
  167. {
  168. try {
  169. $date = date('Y-m-d', strtotime('-1 day'));
  170. $sql = '' . "insert into opera_product_run (`PROD_ID`,`ORG_ID`,`PROD_TYPE`,`PROD_PRICE`,`REMARK`,`RUN_DATE`,`TOTAL_COUNT`)
  171. 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
  172. 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";
  173. Yii::$app->db->createCommand($sql)->execute();
  174. $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
  175. 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
  176. ) b on a.run_date=b.ticket_date and a.prod_id=b.prod_id
  177. set TOTAL_COUNT=b.ticket_num,a.PROD_PRICE=b.agent_price where a.org_id=1369";
  178. Yii::$app->db->createCommand($sql)->execute();
  179. return 1;
  180. } catch (Exception $e) {
  181. return 2;
  182. }
  183. }
  184. /**
  185. * Des:获取详细信息
  186. * Name: getRunInfoByProdIdAndDate
  187. * @param $prod
  188. * @param $date
  189. * @return array
  190. * @author 倪宗锋
  191. */
  192. public function getRunInfoByProdIdAndDate($prod, $date)
  193. {
  194. $select = [
  195. 'prod_price',
  196. 'cus_price',
  197. 'total_count',
  198. 'is_onsale'
  199. ];
  200. $where = [
  201. 'and',
  202. ['=', 'PROD_ID', $prod],
  203. ['=', 'RUN_DATE', $date]
  204. ];
  205. $getInfo = self::find()->select($select)
  206. ->where($where)
  207. ->asArray()
  208. ->one();
  209. return $getInfo;
  210. }
  211. /**
  212. * Function Description:获取具体日期子票种信息
  213. * Function Name: getProdInfo
  214. * @param $main_prod_id
  215. * @param $sub_prod_id
  216. * @param $run_date
  217. * @param $to_org_id
  218. *
  219. * @return array|null|ActiveRecord
  220. *
  221. * @author 冒炎
  222. */
  223. public function getProdInfo($main_prod_id,$sub_prod_id,$run_date,$to_org_id){
  224. $select = [
  225. 'a.run_time',
  226. 'a.run_date',
  227. 'a.prod_price',
  228. 'a.total_count',
  229. 'a.saled_count',
  230. 'a.is_onsale',
  231. 'a.cus_price',
  232. 'base_price'=>'a.cus_price',
  233. '(a.total_count-a.saled_count) as stock',
  234. 'b.prod_id',
  235. 'sub_prod_id'=>'b.prod_id',
  236. 'parent_prod_id'=>'b.parent_id',
  237. 'main_prod_id'=>'b.parent_id',
  238. 'b.prod_code',
  239. 'b.prod_name',
  240. 'b.prod_type',
  241. ];
  242. $where1 = [
  243. 'and',
  244. ['=','a.to_org_id',$to_org_id],
  245. ['=', 'a.prod_id', $sub_prod_id],
  246. ['=', 'b.parent_id', $main_prod_id],
  247. ['=', 'a.run_date', $run_date],
  248. ['=', 'a.cancel_flag', 0],
  249. ['=', 'b.cancel_flag', 0],
  250. // ['=', 'a.is_onsale', 1],
  251. ['=', 'b.is_onsale', 1],
  252. ];
  253. $where2 = [
  254. 'and',
  255. ['=','a.to_org_id',0],
  256. ['=', 'a.prod_id', $sub_prod_id],
  257. ['=', 'b.parent_id', $main_prod_id],
  258. ['=', 'a.run_date', $run_date],
  259. ['=', 'a.cancel_flag', 0],
  260. ['=', 'b.cancel_flag', 0],
  261. ['=', 'a.is_onsale', 1],
  262. ['=', 'b.is_onsale', 1],
  263. ];
  264. $res1 = self::find()
  265. ->select($select)
  266. ->from(self::tableName() . ' a')
  267. ->leftJoin('opera_product as b','a.prod_id = b.prod_id')
  268. ->where($where1)
  269. ->asArray()
  270. ->one();
  271. $res2 = self::find()
  272. ->select($select)
  273. ->from(self::tableName() . ' a')
  274. ->leftJoin('opera_product as b','a.prod_id = b.prod_id')
  275. ->where($where2)
  276. ->asArray()
  277. ->one();
  278. if($res1){
  279. if($res1['is_onsale'] == 0){
  280. return [];
  281. }else{
  282. return $res1;
  283. }
  284. }else{
  285. if(!empty($res2)&&$res2['is_onsale'] == 0){
  286. return [];
  287. }else{
  288. return $res2;
  289. }
  290. }
  291. }
  292. }