// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\common\model; /** * 门店销售数据每日统计模型 * Class ShopsDailySalesSt * @package app\common\model */ class GoodsSalesRank extends BaseModel { // 定义表名 protected $name = 'goods_sales_rank'; // 定义主键 protected $pk = 'id'; /** * 商品销量排行昨日统计数据添加 * @author: zjwhust * @Time: 2022/3/31 15:31 */ public function addGoodsSalesRankDaily(){ $orderGood = new \app\api\model\OrderGoods(); $data = []; $baseTo = strtotime(date("Y-m-d"),time()); $now = time(); $from = $baseTo - 86400; $to = $from + 86399; $temp = []; //批量插入,当门店过多时,需优化成分批插入 self::where('start_time','>=',$from)->where('end_time','<=',$to)->delete(); $tj = $orderGood->addGoodsSalesRank($from,$to); if($tj){ foreach ($tj as $item){ $temp['sale_volume'] = $item['sale_volume']; $temp['sale_nums'] = $item['sale_nums']; $temp['goods_id'] = $item['goods_id']; $temp['start_time'] = $from; $temp['end_time'] = $to; $temp['create_time'] = $now; $temp['update_time'] = $now; $data[] = $temp; } } self::insertAll($data); } /** * 商品销量排行里诶博爱 * @param $from * @param $to * @return mixed * @author: zjwhust * @Time: 2022/3/31 16:54 */ public function listRank($from,$to){ return self::alias('r') ->where('start_time','>=',$from) ->where('end_time','<=',$to) ->field('sum(r.sale_nums) as sale_num_total,sum(r.sale_volume) as sale_volume_total,r.goods_id,g.goods_name,g.goods_no,g.goods_price_min as goods_price') ->leftJoin('goods g','g.goods_id=r.goods_id') ->group('r.goods_id') ->order('sale_num_total desc,sale_volume_total desc') ->paginate(15); } /** * 导出记录 */ public function export($from,$to) { $data['header'] = ['排名', '商品名称','商品编码', '商品单价(元)','销量', '销售额']; $data['filename'] = '商品销量排名导出'; $data['data'] = []; $list = self::alias('r') ->where('start_time','>=',$from) ->where('end_time','<=',$to) ->field('sum(r.sale_nums) as sale_num_total,sum(r.sale_volume) as sale_volume_total,r.goods_id,g.goods_name,g.goods_no,g.goods_price_min as goods_price') ->leftJoin('goods g','g.goods_id=r.goods_id') ->group('r.goods_id') ->order('sale_num_total desc,sale_volume_total desc') ->select(); foreach ($list as $k=>$arr){ $new_list['rank'] = $k+1; $new_list['goods_name'] = $arr['goods_name']; $new_list['goods_no'] = $arr['goods_no']; $new_list['goods_price'] = $arr['goods_price']; $new_list['sale_num_total'] = $arr['sale_num_total']; $new_list['sale_volume_total'] = $arr['sale_volume_total']; $data['data'][] = $new_list; } return $data; } }