Files
material-api/web/collectors/__init__.py
2024-05-29 10:21:31 +08:00

278 lines
8.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
class Collector:
def __init__(self, year, month):
self.year = year
self.month = month
def get_avg(self):
query = PricePublish.get_query(name_in=(
'杉原木',
'松原木',
'锯材',
'锯材',
'毛竹',
'冷轧带肋钢筋网',
'预应力粗钢筋',
'高强钢丝',
'钢板',
'钢板',
'圆钢',
'钢轨',
'钢管',
'镀锌钢管',
'镀锌无缝钢管',
'镀锌钢板',
'钢丝绳',
'波形钢板(双波)',
'波形钢板(三波)',
'四氟板式橡胶组合支座',
'板式橡胶支座',
'盆式橡胶支座',
'盆式橡胶支座',
'盆式橡胶支座',
'盆式橡胶支座',
'盆式橡胶支座',
'盆式橡胶支座',
'模数式伸缩装置',
'模数式伸缩装置',
'钢绞线群锚(3孔)',
'钢绞线群锚(7孔)',
'钢绞线群锚(15孔)',
'不锈钢板',
'铁件',
'铝合金标志',
'铸铁管',
'钢板网',
'铁丝编织网',
'橡胶护舷',
'橡胶护舷',
'橡胶护舷',
'鼓型橡胶护舷',
'鼓型橡胶护舷',
'鼓型橡胶护舷',
'油漆',
'塑料排水板',
'路缘石',
'乳化炸药',
'硝铵炸药',
'导火线',
'普通雷管',
'电雷管',
'工业数码电子雷管',
'导爆索',
'引爆母线',
'重油',
'光圆钢筋',
'带肋钢筋',
'型钢',
'钢绞线',
'32.5级水泥',
'42.5级水泥',
'进口沥青',
'国产沥青',
'进口改性沥青',
'国产改性沥青',
'汽油89',
'汽油92',
'柴油0',
))
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),
PricePublish.tax,
PricePublish.unit,
)
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, \
price_zhangzhou, price_nanpin, price_longyan, price_ningde, price_pintan, tax, unit = item
PricePublish(
year=self.year,
month=self.month,
material_id=material_id,
name=name,
spec=spec,
price=price,
price_fuzhou=price_fuzhou,
price_xiamen=price_xiamen,
price_putian=price_putian,
price_sanming=price_sanming,
price_quanzhou=price_quanzhou,
price_zhangzhou=price_zhangzhou,
price_nanpin=price_nanpin,
price_longyan=price_longyan,
price_ningde=price_ningde,
price_pintan=price_pintan,
tax=tax,
type=2,
unit=unit,
).upsert()
def get_from_survey(self):
query = FujianSurvey.get_query(self.year, self.month, name_in=(
'杉原木',
'松原木',
'锯材',
'锯材',
'毛竹',
'冷轧带肋钢筋网',
'预应力粗钢筋',
'高强钢丝',
'钢板',
'钢板',
'圆钢',
'钢轨',
'钢管',
'镀锌钢管',
'镀锌无缝钢管',
'镀锌钢板',
'钢丝绳',
'波形钢板(双波)',
'波形钢板(三波)',
'四氟板式橡胶组合支座',
'板式橡胶支座',
'盆式橡胶支座',
'盆式橡胶支座',
'盆式橡胶支座',
'盆式橡胶支座',
'盆式橡胶支座',
'盆式橡胶支座',
'模数式伸缩装置',
'模数式伸缩装置',
'钢绞线群锚(3孔)',
'钢绞线群锚(7孔)',
'钢绞线群锚(15孔)',
'不锈钢板',
'铁件',
'铝合金标志',
'铸铁管',
'钢板网',
'铁丝编织网',
'橡胶护舷',
'橡胶护舷',
'橡胶护舷',
'鼓型橡胶护舷',
'鼓型橡胶护舷',
'鼓型橡胶护舷',
'油漆',
'塑料排水板',
'路缘石',
'乳化炸药',
'硝铵炸药',
'导火线',
'普通雷管',
'电雷管',
'工业数码电子雷管',
'导爆索',
'引爆母线',
'重油',
))
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,
tax=item.tax,
type=1,
unit=item.unit
).upsert()
def get_from_result(self):
query = PriceResult.get_query(self.year, self.month, name_in=(
'光圆钢筋',
'带肋钢筋',
'型钢',
'钢绞线',
'32.5级水泥',
'42.5级水泥',
'进口沥青',
'国产沥青',
'进口改性沥青',
'国产改性沥青',
'汽油89',
'汽油92',
'柴油0',
))
data = query.all()
for item in data:
PricePublish(
year=self.year,
month=self.month,
material_id=item.material_id,
name=item.name,
spec='',
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,
tax=9.00,
type=1,
).upsert()
def run(self):
self.get_from_survey()
self.get_from_result()
self.get_avg()
# todo 调查表入库时名称去除空格
if __name__ == '__main__':
from core.factory import ClientApp
with ClientApp().app_context():
collector = Collector(2023, 11)
collector.run()