2024-05-29 10:21:31 +08:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from sqlalchemy import func
|
|
|
|
|
|
|
|
from commons.models.fujian_survey import FujianSurvey
|
|
|
|
from commons.models.price_publish import PricePublish
|
|
|
|
from commons.models.price_result import PriceResult
|
2025-02-21 14:59:26 +08:00
|
|
|
from commons.models.material import Material
|
2024-12-25 11:31:31 +08:00
|
|
|
|
|
|
|
|
2024-05-29 10:21:31 +08:00
|
|
|
class Collector:
|
|
|
|
|
2024-07-12 11:11:28 +08:00
|
|
|
def __init__(self, year, month, force=True):
|
2024-05-29 10:21:31 +08:00
|
|
|
self.year = year
|
|
|
|
self.month = month
|
2024-07-12 11:11:28 +08:00
|
|
|
self.force = True # todo-2 已发布的价格不在覆盖计算
|
2025-01-22 17:22:11 +08:00
|
|
|
self.digit_map = {}
|
2025-02-21 14:59:26 +08:00
|
|
|
# 获取所有材料信息
|
|
|
|
self.materials = Material.query.all()
|
|
|
|
self.material_codes = [m.code for m in self.materials if m.code]
|
2024-05-29 10:21:31 +08:00
|
|
|
|
|
|
|
def get_avg(self):
|
2025-02-21 14:59:26 +08:00
|
|
|
query = PricePublish.get_query(material_id_in=self.material_codes)
|
2024-05-29 10:21:31 +08:00
|
|
|
query = PricePublish.query_previous_month(query, start_date=datetime.date(self.year, self.month, 1), count=6)
|
|
|
|
query = query.with_entities(
|
|
|
|
PricePublish.material_id,
|
|
|
|
PricePublish.name,
|
|
|
|
PricePublish.spec,
|
|
|
|
func.avg(PricePublish.price),
|
|
|
|
func.avg(PricePublish.price_fuzhou),
|
|
|
|
func.avg(PricePublish.price_xiamen),
|
|
|
|
func.avg(PricePublish.price_putian),
|
|
|
|
func.avg(PricePublish.price_sanming),
|
|
|
|
func.avg(PricePublish.price_quanzhou),
|
|
|
|
func.avg(PricePublish.price_zhangzhou),
|
|
|
|
func.avg(PricePublish.price_nanpin),
|
|
|
|
func.avg(PricePublish.price_longyan),
|
|
|
|
func.avg(PricePublish.price_ningde),
|
|
|
|
func.avg(PricePublish.price_pintan),
|
2024-07-11 18:14:17 +08:00
|
|
|
func.avg(PricePublish.price_zhangzhoukfq),
|
2024-05-29 10:21:31 +08:00
|
|
|
PricePublish.tax,
|
|
|
|
PricePublish.unit,
|
|
|
|
)
|
2024-07-11 18:14:17 +08:00
|
|
|
query = query.filter(PricePublish.price != 0)
|
2024-05-29 10:21:31 +08:00
|
|
|
query = query.group_by(
|
|
|
|
PricePublish.material_id,
|
|
|
|
PricePublish.name,
|
|
|
|
PricePublish.spec,
|
|
|
|
PricePublish.tax,
|
|
|
|
PricePublish.unit,
|
|
|
|
)
|
|
|
|
data = query.all()
|
|
|
|
for item in data:
|
|
|
|
material_id, name, spec, price, price_fuzhou, price_xiamen, price_putian, price_sanming, price_quanzhou, \
|
2025-01-22 17:22:11 +08:00
|
|
|
price_zhangzhou, price_nanpin, price_longyan, price_ningde, price_pintan, price_zhangzhoukfq, tax, unit, = item
|
|
|
|
display_digit = self.digit_map.get(material_id, 1)
|
2024-05-29 10:21:31 +08:00
|
|
|
PricePublish(
|
|
|
|
year=self.year,
|
|
|
|
month=self.month,
|
|
|
|
material_id=material_id,
|
|
|
|
name=name,
|
|
|
|
spec=spec,
|
2024-07-11 18:14:17 +08:00
|
|
|
price=round(price, 2),
|
2024-09-10 16:57:15 +08:00
|
|
|
price_fuzhou=round(price_fuzhou or 0, 2),
|
|
|
|
price_xiamen=round(price_xiamen or 0, 2),
|
|
|
|
price_putian=round(price_putian or 0, 2),
|
|
|
|
price_sanming=round(price_sanming or 0, 2),
|
|
|
|
price_quanzhou=round(price_quanzhou or 0, 2),
|
|
|
|
price_zhangzhou=round(price_zhangzhou or 0, 2),
|
|
|
|
price_nanpin=round(price_nanpin or 0, 2),
|
|
|
|
price_longyan=round(price_longyan or 0, 2),
|
|
|
|
price_ningde=round(price_ningde or 0, 2),
|
|
|
|
price_pintan=round(price_pintan or 0, 2),
|
|
|
|
price_zhangzhoukfq=round(price_zhangzhoukfq or 0, 2),
|
2024-05-29 10:21:31 +08:00
|
|
|
tax=tax,
|
|
|
|
type=2,
|
|
|
|
unit=unit,
|
2025-01-21 16:04:21 +08:00
|
|
|
display_digit=display_digit,
|
2024-05-29 10:21:31 +08:00
|
|
|
).upsert()
|
|
|
|
|
|
|
|
def get_from_survey(self):
|
2025-02-21 14:59:26 +08:00
|
|
|
query = FujianSurvey.get_query(self.year, self.month, material_id_in=self.material_codes)
|
2024-05-29 10:21:31 +08:00
|
|
|
data = query.all()
|
|
|
|
for item in data:
|
|
|
|
PricePublish(
|
|
|
|
year=self.year,
|
|
|
|
month=self.month,
|
|
|
|
material_id=item.material_id,
|
|
|
|
name=item.name,
|
|
|
|
spec=item.spec,
|
|
|
|
price=item.price,
|
|
|
|
price_fuzhou=item.price,
|
|
|
|
price_xiamen=item.price,
|
|
|
|
price_putian=item.price,
|
|
|
|
price_sanming=item.price,
|
|
|
|
price_quanzhou=item.price,
|
|
|
|
price_zhangzhou=item.price,
|
|
|
|
price_nanpin=item.price,
|
|
|
|
price_longyan=item.price,
|
|
|
|
price_ningde=item.price,
|
|
|
|
price_pintan=item.price,
|
2024-07-11 18:14:17 +08:00
|
|
|
price_zhangzhoukfq=item.price,
|
2024-05-29 10:21:31 +08:00
|
|
|
tax=item.tax,
|
|
|
|
type=1,
|
|
|
|
unit=item.unit
|
|
|
|
).upsert()
|
|
|
|
|
|
|
|
def get_from_result(self):
|
2025-02-21 14:59:26 +08:00
|
|
|
query = PriceResult.get_query(self.year, self.month, material_id_in=self.material_codes)
|
2024-05-29 10:21:31 +08:00
|
|
|
data = query.all()
|
2025-01-22 17:22:11 +08:00
|
|
|
self.digit_map = {i.material_id:i.display_digit for i in data}
|
2024-05-29 10:21:31 +08:00
|
|
|
for item in data:
|
|
|
|
PricePublish(
|
|
|
|
year=self.year,
|
|
|
|
month=self.month,
|
|
|
|
material_id=item.material_id,
|
|
|
|
name=item.name,
|
2024-07-11 18:14:17 +08:00
|
|
|
spec=item.spec,
|
2024-05-29 10:21:31 +08:00
|
|
|
price=item.price_recommend,
|
|
|
|
price_fuzhou=item.price_recommend,
|
|
|
|
price_xiamen=item.price_recommend,
|
|
|
|
price_putian=item.price_recommend,
|
|
|
|
price_sanming=item.price_recommend,
|
|
|
|
price_quanzhou=item.price_recommend,
|
|
|
|
price_zhangzhou=item.price_recommend,
|
|
|
|
price_nanpin=item.price_recommend,
|
|
|
|
price_longyan=item.price_recommend,
|
|
|
|
price_ningde=item.price_recommend,
|
|
|
|
price_pintan=item.price_recommend,
|
2024-07-11 18:14:17 +08:00
|
|
|
price_zhangzhoukfq=item.price_recommend,
|
2024-05-29 10:21:31 +08:00
|
|
|
tax=9.00,
|
|
|
|
type=1,
|
2024-07-11 18:14:17 +08:00
|
|
|
unit=item.unit,
|
2025-01-21 16:04:21 +08:00
|
|
|
display_digit=item.display_digit,
|
2024-05-29 10:21:31 +08:00
|
|
|
).upsert()
|
|
|
|
|
|
|
|
def run(self):
|
2024-07-11 18:14:17 +08:00
|
|
|
# 当月价
|
2024-12-25 11:31:31 +08:00
|
|
|
# self.get_from_survey()
|
2024-05-29 10:21:31 +08:00
|
|
|
self.get_from_result()
|
2024-07-11 18:14:17 +08:00
|
|
|
# 近半年平均价
|
2024-05-29 10:21:31 +08:00
|
|
|
self.get_avg()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from core.factory import ClientApp
|
|
|
|
|
|
|
|
with ClientApp().app_context():
|
|
|
|
collector = Collector(2023, 11)
|
|
|
|
collector.run()
|