diff --git a/Mods/IndepthResearch/Mods/CompanyReport/Crud.py b/Mods/IndepthResearch/Mods/CompanyReport/Crud.py new file mode 100644 index 0000000..39e024f --- /dev/null +++ b/Mods/IndepthResearch/Mods/CompanyReport/Crud.py @@ -0,0 +1,64 @@ +import gridfs +from bson import ObjectId +from pymongo.database import Database +from sqlalchemy import or_ +from sqlalchemy.orm import Session +from . import Schemas +from .Models import CompanyReport + + +def company_report_add(db: Session, data: dict): + item = CompanyReport(**data) + db.add(item) + db.commit() + db.refresh(item) + return item + + +def company_report_delete(db: Session, item_id: int): + db.query(CompanyReport).filter_by(company_id=item_id).delete() + db.commit() + + +def company_report_get(db: Session, item_id: int): + item = db.query(CompanyReport).filter_by(company_id=item_id).first() + return item + + +def company_report_query(db: Session, schema: Schemas.CompanyReportQuery): + param = schema.dict() + company_id = param.get('company_id') + company = param.get('company') + date = param.get('date') + page = param.get('page') + pagesize = param.get('pagesize') + + items = db.query(CompanyReport).filter( + or_(CompanyReport.company == company, company is None), + or_(CompanyReport.date == date, date is None), + or_(CompanyReport.company_id == company_id, company_id is None)).offset( + (page - 1) * pagesize).limit(pagesize).all() + count = db.query(CompanyReport).filter( + or_(CompanyReport.company == company, company is None), + or_(CompanyReport.date == date, date is None), + or_(CompanyReport.company_id == company_id, company_id is None) + ).count() + + return items, count + + +def insert_file(db, file: bytes): + fs = gridfs.GridFS(db, 'docx') + return fs.put(file) + + +def get_file(db, fid: str): + fs = gridfs.GridFS(db, 'docx') + gf = fs.get(ObjectId(fid)) + return gf.read() + + +def delete_file(db, fid: str): + fs = gridfs.GridFS(db, 'docx') + fs.delete(ObjectId(fid)) + return True diff --git a/Mods/IndepthResearch/Mods/CompanyReport/File/template/data_temp.json b/Mods/IndepthResearch/Mods/CompanyReport/File/template/data_temp.json new file mode 100644 index 0000000..34e4826 --- /dev/null +++ b/Mods/IndepthResearch/Mods/CompanyReport/File/template/data_temp.json @@ -0,0 +1,74 @@ +{ + "公司名称": "远东执信企业征信有限公司", + "成立时间": "2006-03-01", + "注册资本": "6500万人民币", + "注册地址": "上海市杨浦区国通路127号16层(集中登记地)", + "区县": "高新区", + "省": "四川省", + "市": "成都市", + "近三年经济数据": { + "2020": { + "GDP": "100万亿元", + "GDP增长率": "6.5%", + "人均GDP": "7万元", + "常住人口": "14亿", + "一般公共预算收入": "18万亿元", + "税收收入": "15万亿元", + "税收占比": "80%", + "转移性收入": "3万亿元", + "一般公共预算支出": "20万亿元", + "土地出让收入": "4万亿元", + "政府性基金收入": "2万亿元", + "财政自给率": "90%", + "地方政府债务余额": "15万亿元", + "地方政府债务限额": "20万亿元", + "城投有息债务合计": "3万亿元", + "负债率": "60%", + "区域宽口径债务率": "75%", + "新房销售价格": "15000元/平米", + "住宅土地楼面价": "10000元/平米" + }, + "2021": { + "GDP": "105万亿元", + "GDP增长率": "7%", + "人均GDP": "7.5万元", + "常住人口": "14.1亿", + "一般公共预算收入": "20万亿元", + "税收收入": "16万亿元", + "税收占比": "80%", + "转移性收入": "4万亿元", + "一般公共预算支出": "22万亿元", + "土地出让收入": "5万亿元", + "政府性基金收入": "2.5万亿元", + "财政自给率": "91%", + "地方政府债务余额": "16万亿元", + "地方政府债务限额": "22万亿元", + "城投有息债务合计": "3.5万亿元", + "负债率": "55%", + "区域宽口径债务率": "70%", + "新房销售价格": "16000元/平米", + "住宅土地楼面价": "11000元/平米" + }, + "2022": { + "GDP": "110万亿元", + "GDP增长率": "7.5%", + "人均GDP": "8万元", + "常住人口": "14.2亿", + "一般公共预算收入": "22万亿元", + "税收收入": "18万亿元", + "税收占比": "82%", + "转移性收入": "4万亿元", + "一般公共预算支出": "24万亿元", + "土地出让收入": "6万亿元", + "政府性基金收入": "3万亿元", + "财政自给率": "92%", + "地方政府债务余额": "18万亿元", + "地方政府债务限额": "25万亿元", + "城投有息债务合计": "4万亿元", + "负债率": "50%", + "区域宽口径债务率": "65%", + "新房销售价格": "17000元/平米", + "住宅土地楼面价": "12000元/平米" + } + } +} \ No newline at end of file diff --git a/Mods/IndepthResearch/Mods/CompanyReport/File/template/report_data_template.json b/Mods/IndepthResearch/Mods/CompanyReport/File/template/report_data_template.json new file mode 100644 index 0000000..744ad90 --- /dev/null +++ b/Mods/IndepthResearch/Mods/CompanyReport/File/template/report_data_template.json @@ -0,0 +1,146 @@ +{ + "鍏徃鍚嶇О": "", + "澶栭儴绾у埆": "", + "鎶ュ憡鏃堕棿": "", + "鍘嗘涓婁細鎰忚": [], + "鎴愮珛鏃堕棿": "", + "娉ㄥ唽璧勬湰": "", + "娉ㄥ唽鍦板潃": "", + "鎴嚦骞翠唤": "", + "鎬昏祫浜": "", + "鎵鏈夎呮潈鐩": "", + "瀹炴敹璧勬湰": "", + "钀ヤ笟鏀跺叆": "", + "鍑鍒╂鼎": "", + "鐪": "", + "甯": "", + "鍖哄幙": "", + "鍦板尯绠浠": "", + "鍖哄煙杩戜笁骞寸粡娴庢暟鎹": [ + [ + "椤圭洰", + "2020", + "2021", + "2022" + ] + ], + "涓庝笂绾ф斂搴溿佸厔寮熷尯甯傦紙鍘匡級瀵规瘮琛": [ + [ + "鍖哄煙", + "鏍囩殑鍖哄煙", + "涓婄骇鏀垮簻", + "瀵规瘮甯傦紙鍘匡級1", + "瀵规瘮甯傦紙鍘匡級2", + "瀵规瘮甯傦紙鍘匡級3" + ] + ], + "鍖哄煙璐熼潰鑸嗘儏鎼滈泦": [ + [ + "鏃堕棿", + "鍖哄煙", + "鏂伴椈鏍囬", + "棰勮鍒嗙被", + "瓒呴摼鎺" + ] + ], + "鍖哄煙鍐呭煄鎶曞钩鍙板姣旇〃": [ + [ + "搴忓彿", + "鍏徃鍚嶇О", + "鏈鏂扮骇鍒", + "璇勭骇鏈烘瀯", + "鎬昏祫浜э紙浜垮厓锛", + "鍑璧勪骇锛堜嚎鍏冿級", + "鎬绘湁鎭哄姟锛堜嚎鍏冿級", + "璧勪骇璐熷虹巼锛%锛", + "钀ヤ笟鏀跺叆锛堜嚎鍏冿級" + ] + ], + "涓昏惀涓氬姟鏄庣粏": [ + [ + "", + "2020骞", + "", + "2021骞", + "", + "2022骞", + "", + "澶囨敞" + ], + [ + "涓氬姟", + "鏀跺叆锛堝崟浣嶏細浜垮厓锛", + "鍗犳瘮锛%锛", + "鏀跺叆锛堝崟浣嶏細浜垮厓锛", + "鍗犳瘮锛%锛", + "鏀跺叆锛堝崟浣嶏細浜垮厓锛", + "鍗犳瘮锛%锛", + "" + ] + ], + "璐㈠姟鏁版嵁琛": [ + [ + "绉戠洰", + "2020骞", + "2021骞", + "2022骞" + ], + [ + "璐㈠姟鏁版嵁", + "", + "", + "" + ] + ], + "鍙戝烘儏鍐": [ + [ + "鍊哄埜", + "绫诲瀷", + "鍙戣鏃ユ湡", + "鍒版湡鏃", + "鍙戣瑙勬ā", + "鍊哄埜浣欓", + "鍒╃巼" + ] + ], + "鎺堜俊鎯呭喌": "", + "瀵瑰鎷呬繚": "", + "鍙楅檺璧勪骇": "", + "鍙告硶璇夎": "", + "鍏徃鍙婂瓙鍏徃璐熼潰鑸嗘儏": "", + "鎵撳垎琛": [ + [ + "瑕佺礌", + "鎸囨爣", + "鏉冮噸", + "妗d綅" + ] + ], + "鍚堝苟璧勪骇璐熷鸿〃": [ + [ + "璐㈠姟鏁版嵁", + "2020", + "2021", + "2022", + "2023.3" + ] + ], + "鍚堝苟鍒╂鼎琛": [ + [ + "璐㈠姟鏁版嵁", + "2020", + "2021", + "2022", + "2023.3" + ] + ], + "鍚堝苟鐜伴噾娴侀噺琛": [ + [ + "璐㈠姟鏁版嵁", + "2020", + "2021", + "2022", + "2023.3" + ] + ] +} \ No newline at end of file diff --git a/Mods/IndepthResearch/Mods/CompanyReport/File/template/urban_investment_model.json b/Mods/IndepthResearch/Mods/CompanyReport/File/template/urban_investment_model.json new file mode 100644 index 0000000..f08a76c --- /dev/null +++ b/Mods/IndepthResearch/Mods/CompanyReport/File/template/urban_investment_model.json @@ -0,0 +1,162 @@ +{ + "寰楀垎绾у埆鏍囧噯": { + "AAA": 98, + "AA+": 96, + "AA": 94, + "AA-": 92, + "A+": 90, + "A": 88, + "A-": 86, + "BBB+": 84, + "BBB": 82, + "BBB-": 80, + "BB+": 78, + "BB": 76, + "BB-": 74, + "B+": 72, + "B": 70, + "B-": 68, + "CCC": 66, + "CC": 64, + "C": 62 + }, + "妗d綅寰楀垎鏍囧噯": { + "绗竴妗": 1, + "绗簩妗": 5, + "绗笁妗": 11, + "绗洓妗": 17, + "绗簲妗": 23, + "绗叚妗": 29, + "绗竷妗": 33, + "绗叓妗": 37 + }, + "鎸囨爣鍙婃潈閲": [ + { + "鎸囨爣": "涓氬姟绋冲畾鎬", + "鎸囨爣绫诲瀷": "瀹氭ф寚鏍", + "涓绾ф寚鏍": "涓氬姟鐘跺喌", + "浜岀骇鎸囨爣": "涓氬姟绋冲畾鎬", + "妗d綅": { + "绗竴妗": "鍦版柟鏀垮簻灏嗙浉鍏抽鍩熺殑澶ч噺涓撹惀鎬т笟鍔℃巿鏉冪粰鍏徃杩愯惀", + "绗簩妗": "鍦版柟鏀垮簻灏嗙浉鍏抽鍩熺殑涓昏涓撹惀鎬т笟鍔℃巿鏉冪粰鍏徃杩愯惀", + "绗笁妗": "鍦版柟鏀垮簻灏嗙浉鍏抽鍩熺殑杈冨涓撹惀鎬т笟鍔℃巿鏉冪粰鍏徃杩愯惀", + "绗洓妗": "鍦版柟鏀垮簻灏嗙浉鍏抽鍩熺殑涓瀹氶噺涓撹惀鎬т笟鍔℃巿鏉冪粰鍏徃杩愯惀", + "绗簲妗": "鍦版柟鏀垮簻灏嗙浉鍏抽鍩熺殑閮ㄥ垎涓撹惀鎬т笟鍔℃巿鏉冪粰鍏徃杩愯惀", + "绗叚妗": "鍦版柟鏀垮簻灏嗙浉鍏抽鍩熺殑灏戦噺涓撹惀鎬т笟鍔℃巿鏉冪粰鍏徃杩愯惀", + "绗竷妗": "鍦版柟鏀垮簻鎺堟潈缁欏叕鍙歌繍钀ョ殑涓撹惀鎬т笟鍔¤緝灏", + "绗叓妗": "鍦版柟鏀垮簻鎺堟潈缁欏叕鍙歌繍钀ョ殑涓撹惀鎬т笟鍔″緢灏" + }, + "鏉冮噸": 20 + }, + { + "鎸囨爣": "涓氬姟鍏泭鎬", + "鎸囨爣绫诲瀷": "瀹氭ф寚鏍", + "涓绾ф寚鏍": "涓氬姟鐘跺喌", + "浜岀骇鎸囨爣": "涓氬姟鍏泭鎬", + "妗d綅": { + "绗竴妗": "鍏泭鎬т笟鍔″崰姣旀瀬楂", + "绗簩妗": "鍏泭鎬т笟鍔″崰姣斿緢楂", + "绗笁妗": "鍏泭鎬т笟鍔″崰姣旈珮", + "绗洓妗": "鍏泭鎬т笟鍔″崰姣旇緝楂", + "绗簲妗": "鍏泭鎬т笟鍔″崰姣斾竴鑸", + "绗叚妗": "鍏泭鎬т笟鍔″崰姣旇緝浣", + "绗竷妗": "鍏泭鎬т笟鍔″崰姣斾綆", + "绗叓妗": "鍏泭鎬т笟鍔″崰姣斿緢浣" + }, + "鏉冮噸": 20 + }, + { + "鎸囨爣": "铻嶈祫娓犻亾", + "鎸囨爣绫诲瀷": "瀹氭ф寚鏍", + "涓绾ф寚鏍": "铻嶈祫鑳藉姏", + "浜岀骇鎸囨爣": "铻嶈祫娓犻亾", + "妗d綅": { + "绗竴妗": "铻嶈祫娓犻亾鏋佺晠閫", + "绗簩妗": "铻嶈祫娓犻亾寰堢晠閫", + "绗笁妗": "铻嶈祫娓犻亾鐣呴", + "绗洓妗": "铻嶈祫娓犻亾杈冪晠閫", + "绗簲妗": "铻嶈祫娓犻亾灏氬彲", + "绗叚妗": "铻嶈祫娓犻亾涓鑸", + "绗竷妗": "铻嶈祫娓犻亾鍙楅檺", + "绗叓妗": "鍩烘湰鏃犺瀺璧勬笭閬" + }, + "鏉冮噸": 20 + }, + { + "鎸囨爣": "鎵鏈夎呮潈鐩婏紙浜垮厓锛", + "鎸囨爣绫诲瀷": "瀹氶噺鎸囨爣", + "涓绾ф寚鏍": "璧勬湰缁撴瀯", + "浜岀骇鎸囨爣": "鎵鏈夎呮潈鐩婏紙浜垮厓锛", + "妗d綅": { + "绗竴妗": "[500,+inf)", + "绗簩妗": "[300,500)", + "绗笁妗": "[100,300)", + "绗洓妗": "[40,100)", + "绗簲妗": "[20,40)", + "绗叚妗": "[10,20)", + "绗竷妗": "[5,10)", + "绗叓妗": "(-inf,5)" + }, + "鏉冮噸": 15 + }, + { + "鎸囨爣": "鎬诲哄姟璧勬湰鍖栨瘮鐜囷紙%锛", + "鎸囨爣绫诲瀷": "瀹氶噺鎸囨爣", + "涓绾ф寚鏍": "璧勬湰缁撴瀯", + "浜岀骇鎸囨爣": "鎬诲哄姟璧勬湰鍖栨瘮鐜囷紙%锛", + "妗d綅": { + "绗竴妗": "[0, 30]", + "绗簩妗": "(30,50]", + "绗笁妗": "(50,60]", + "绗洓妗": "(60,70]", + "绗簲妗": "(70,75]", + "绗叚妗": "(75,80]", + "绗竷妗": "(80,85]", + "绗叓妗": "(85,+inf)" + }, + "鏉冮噸": 15 + }, + { + "鎸囨爣": "EBITDA鍒╂伅淇濋殰鍊嶆暟锛堝嶏級", + "鎸囨爣绫诲瀷": "瀹氶噺鎸囨爣", + "涓绾ф寚鏍": "鍋垮鸿兘鍔", + "浜岀骇鎸囨爣": "EBITDA鍒╂伅淇濋殰鍊嶆暟锛堝嶏級", + "妗d綅": { + "绗竴妗": "[8,+inf)", + "绗簩妗": "[4,8)", + "绗笁妗": "[2,4)", + "绗洓妗": "[1,2)", + "绗簲妗": "[0.4,1)", + "绗叚妗": "[0.1,0.4)", + "绗竷妗": "[0.05,0.1)", + "绗叓妗": "(-inf,0.05)" + }, + "鏉冮噸": 10 + } + ], + "璇勭骇璋冩暣": [ + { + "璋冩暣鍥犵礌": "鏀跨瓥鍥犵礌", + "绠浠": "鍩庢姇浼佷笟鐨勭壒娈婂睘鎬у喅瀹氫簡鍏朵笟鍔¤繍钀ヤ笌鏀垮簻鍏崇郴瀵嗗垏锛屽煄鎶曚紒涓氭墍鎵垮缓鐨勯」鐩鏁版槸鐢辨斂搴滄巿鏉冨缓璁炬垨杩愯惀锛屽叾鏀跺叆鏉ユ簮涓昏涓哄湴鏂规斂搴滃強鍏朵粬閮ㄩ棬鍜屾満鏋勩傚洜姝わ紝琛屼笟鏀跨瓥鍜岀洃绠℃斂绛栧彉鍖栨垨璋冩暣锛屽鍩庢姇浼佷笟鐨勭粡钀ャ佽瀺璧勫拰鍙戝睍浜х敓杈冨ぇ褰卞搷锛屼粠鑰屽奖鍝嶅煄鎶曚紒涓氱殑淇$敤鍩烘湰闈€" + }, + { + "璋冩暣鍥犵礌": "ESG涓庣鐞嗗洜绱", + "绠浠": "ESG 鍥犵礌涓鑸氳繃鐜銆佺ぞ浼氳矗浠诲拰鍏徃娌荤悊涓変釜鏂归潰杩涜璇勪环銆傞氬父锛屽煄鎶曚紒涓氶潰涓寸幆澧冨拰绀句細璐d换椋庨櫓杈冨皬銆傚叕鍙告不鐞嗕笌绠$悊鏂归潰锛岃繙涓滆祫淇′富瑕佸叧娉ㄥ煄鎶曚紒涓氬唴鎺у埗搴︺侀」鐩鐞嗐佽瀺璧勭鐞嗐佸瀛愬叕鍙哥鎺ц兘鍔涚瓑銆" + }, + { + "璋冩暣鍥犵礌": "骞冲彴鏁村悎", + "绠浠": "鍖哄煙鍩庢姇骞冲彴鏁村悎鏄叧绯诲煄鎶曚紒涓氫俊鐢ㄦ按骞崇殑閲嶈鍥犵礌锛屾斂搴滀富瀵间笅鐨勫钩鍙版暣鍚堝奖鍝嶅彈璇勫煄鎶曚紒涓氱殑瑙勬ā鍜屽畾浣嶏紝杩涜屽奖鍝嶅叾鏈潵鑾峰緱鏀垮簻鏀寔鐨勫姏搴︺" + }, + { + "璋冩暣鍥犵礌": "璧勪骇璐ㄩ噺", + "绠浠": "璧勪骇璐ㄩ噺涓昏鍒嗘瀽鍩庢姇浼佷笟鐨勮祫浜ф槸鍚﹁鏄庢樉楂樹及鎴栧瓨鍦ㄥぇ閲忔棤鏁堣祫浜э紝闇瑕侀噸瑙嗗湡鍦颁娇鐢ㄦ潈銆佹灄鏉冦佹捣鍩熶娇鐢ㄦ潈銆佹哗娑備娇鐢ㄦ潈銆佹渤閬撻噰鐮傛潈銆佸搧鐗岀壒璁哥粡钀ユ潈绛夎祫浜х殑鐩堝埄鎬с" + }, + { + "璋冩暣鍥犵礌": "鍏朵粬璋冩暣鍥犵礌", + "绠浠": "瀵逛簬鍏朵粬璋冩暣鍥犵礌锛堝寘鎷絾涓嶉檺浜庯細绐佸彂淇$敤浜嬩欢鍐插嚮鍖哄煙铻嶈祫鐜銆佽鎷呬繚浼佷笟缁忚惀鎯呭喌鎭跺寲鎴栧瓨鍦ㄤ弗閲嶈礋闈簨浠躲侀噸澶ц瘔璁笺佺兢浣撴т簨浠剁瓑锛夛紝杩滀笢璧勪俊灏嗚鍏蜂綋鎯呭喌浜堜互璇勪及銆" + } + ], + "澶栭儴鏀寔": { + "绫诲瀷": "鏀垮簻" + } +} \ No newline at end of file diff --git a/Mods/IndepthResearch/Mods/CompanyReport/Models.py b/Mods/IndepthResearch/Mods/CompanyReport/Models.py new file mode 100644 index 0000000..fa44689 --- /dev/null +++ b/Mods/IndepthResearch/Mods/CompanyReport/Models.py @@ -0,0 +1,17 @@ +from Context.common import common_db +from sqlalchemy import Column, Integer, String, Text, Date, Double + + +class CompanyReport(common_db.Base): + """ + 鍓嶆湡鐮旀姤 + """ + __tablename__ = "company_report" + company_id = Column(Integer, primary_key=True, comment="浼佷笟ID") + company = Column(String(128), comment="浼佷笟鍚嶇О") + date = Column(Date, comment="鐮旀姤鏃ユ湡") + file_id = Column(String(24), comment="鏂囦欢ID") + + def to_dict(self): + data = {c.name: getattr(self, c.name) for c in self.__table__.columns} + return data diff --git a/Mods/IndepthResearch/Mods/CompanyReport/Router.py b/Mods/IndepthResearch/Mods/CompanyReport/Router.py new file mode 100644 index 0000000..a4447f0 --- /dev/null +++ b/Mods/IndepthResearch/Mods/CompanyReport/Router.py @@ -0,0 +1,323 @@ +# -*- coding: utf-8 -*- +import copy +import datetime +import json +import os + +from fastapi import APIRouter, Depends, HTTPException +from sqlalchemy.orm import Session +from starlette.background import BackgroundTask +from starlette.responses import FileResponse + +from Mods.BasicInformation.Mods import CompanyIndexMain +from Mods.BasicInformation.Mods import CompanyBaseInfo +from Mods.BasicInformation.Mods import CompanyPlusProperty +from Mods.BasicInformation.Mods import CompanySearchMain +from Mods.BasicInformation.Mods.CompanyIndexMain.Crud import company_index_main_get +from Mods.BasicInformation.Mods.CompanyBaseInfo.Crud import company_base_info_get +from Mods.BasicInformation.Mods.CompanyPlusProperty.Crud import company_plus_property_get +from Mods.IndepthResearch.Mods import CompanyMeetingRecord +from Mods.OtherInformation.Mods import CompanyCreditRating +from Mods.OtherInformation.Mods import CompanyFinance +from Mods.OtherInformation.Mods import CompanyBusinessCompose +from Mods.OtherInformation.Mods import CompanyFinanceMain +from Mods.OtherInformation.Mods import CompanyBond +from Mods.OtherInformation.Mods import CompanyCredit +from Mods.OtherInformation.Mods import CompanyGuarantee +from Mods.OtherInformation.Mods import CompanyRestrictedAssets +from Mods.OtherInformation.Mods import CompanyJudicialDocument +from Mods.RegionalEconomies.Mods import AreaFiscalRevenue +from Mods.CommonInformation.Mods import OpinionInformation + +from docx import Document +from docxtpl import DocxTemplate + +from Mods.RegionalEconomies.Mods.AreaIndexMain.Crud import area_index_main_get +from Mods.RegionalEconomies.models import t_area_report +from Utils.MongoUtils import MongoConnect +from . import Schemas +from . import Crud + +from Context.common import common_db, mg_db + + +def del_file(path): + os.remove(path) + + +router = APIRouter(tags=["鍓嶆湡鐮旀姤"]) + + +@router.post("/company_report/query", summary="鏌ヨ鐮旀姤", response_model='') +def company_score_add(schemas: Schemas.CompanyReportQuery, db: Session = Depends(common_db.get_db)): + items, total = Crud.company_report_query(db=db, schema=schemas) + return { + "code": 200, + "message": "鏌ヨ鎴愬姛", + "content": {"items": items, "total": total} + } + + +@router.post("/company_report/get", summary="鏌ョ湅鐮旀姤", response_model='') +def company_score_add(company_id: int = "浼佷笟ID", db: Session = Depends(common_db.get_db), + mongo_db: Session = Depends(mg_db.get_db)): + company = CompanyIndexMain.Crud.company_index_main_get(db=db, item_id=company_id) + if company: + raise HTTPException(status_code=202, detail="浼佷笟ID涓嶅瓨鍦") + + report = Crud.company_report_get(db=db, item_id=company_id) + if not report: + raise HTTPException(status_code=404, detail="鎶ュ憡涓嶅瓨鍦") + + file_name = "{}鍓嶆湡鐮旂┒鎶ュ憡.docx".format(company.company_name) + path = os.path.join(os.getcwd(), 'Mods', 'IndepthResearch', 'Mods', 'CompanyReport', 'File', 'generate', + file_name) + result = Crud.get_file(mongo_db, fid=str(report.file_id)) + myfile = open(path, mode='wb') + myfile.write(result) + task = BackgroundTask(del_file, path) + return FileResponse(path, filename=file_name, media_type='application/octet-stream', background=task) + + +@router.get("/company_report/generate", summary="鐢熸垚鐮旀姤") +def company_score_add(company_id: int = "浼佷笟ID", db: Session = Depends(common_db.get_db), + mongo_db: MongoConnect = Depends(mg_db.get_db)): + company = CompanyIndexMain.Crud.company_index_main_get(db=db, item_id=company_id) + if not company: + raise HTTPException(status_code=202, detail="浼佷笟ID涓嶅瓨鍦") + + def download(fid): + """鏍规嵁file_id涓嬭浇鎶ュ憡""" + file_name = "{}鍓嶆湡鐮旂┒鎶ュ憡.docx".format(company.company_name) + path = os.path.join(os.getcwd(), 'Mods', 'IndepthResearch', 'Mods', 'CompanyReport', 'File', 'generate', + file_name) + result = Crud.get_file(mongo_db, fid=str(fid)) + myfile = open(path, mode='wb') + myfile.write(result) + task = BackgroundTask(del_file, path) + return FileResponse(path, filename=file_name, media_type='application/octet-stream', background=task) + + report = Crud.company_report_get(db=db, item_id=company_id) + if report: + res = download(fid=report.file_id) + return res + + # 璇诲彇鏁版嵁妯℃澘 + template_path = os.path.join(os.getcwd(), 'Mods', 'IndepthResearch', 'Mods', 'CompanyReport', 'File', 'template', + 'report_data_template.json') + with open(template_path, encoding='utf-8') as f: + template = json.load(f) + if not template: + raise HTTPException(status_code=401, detail="鏁版嵁妯℃澘璇诲彇澶辫触") + + template['鍏徃鍚嶇О'] = company.company_name + template['鎶ュ憡鏃堕棿'] = datetime.datetime.now().strftime("%Y骞%m鏈%d鏃") + + # 澶栭儴绾у埆 + company_credit_rating = CompanyCreditRating.Crud.company_credit_rating_query(db=db, + params=CompanyCreditRating.Schemas.CompanyCreditRatingQuery( + company_id=company_id)) + template['澶栭儴绾у埆'] = company_credit_rating[1].all()[0].result if company_credit_rating else '-' + + # 鍘嗗彶涓婁細璁板綍 + company_meeting_record = CompanyMeetingRecord.Crud.company_meeting_record_query(db=db, + params=CompanyMeetingRecord.Schemas.CompanyMeetingRecordQuery( + company_id=company_id)) + + meeting_record = [["涓婁細鏃堕棿", "椤圭洰绫诲瀷", "涓婁細鎰忚"]] + for item in company_meeting_record[1]: + item_list = list() + item_list.append(item.metting_time) + item_list.append(item.project_type) + item_list.append(item.result) + meeting_record.append(item_list) + + template['鍘嗘涓婁細鎰忚'] = meeting_record + + # 宸ュ晢淇℃伅 + company_base_info = CompanyBaseInfo.Crud.company_base_info_query(db=db, + params=CompanyBaseInfo.Schemas.CompanyBaseInfoQuery( + company_id=company_id)) + template['鎴愮珛鏃堕棿'] = company_base_info[1][0].estiblish_date + template['娉ㄥ唽璧勬湰'] = company_base_info[1][0].reg_capital + template['娉ㄥ唽鍦板潃'] = company_base_info[1][0].reg_location + + # 璐㈠姟鏁版嵁 + # company_base_info = CompanyFinance.Crud.company_finance_query(db=mongo_db, type_name=None, params=[]) + + # 鍩庢姇灞炴 + company_plus_property = CompanyPlusProperty.Crud.company_plus_property_query(db=db, + params=CompanyPlusProperty.Schemas.CompanyPlusPropertyQuery( + company_id=company_id)) + + # 鍖哄煙璐㈡斂鏀舵敮 + # area_fiscal_revenue = AreaFiscalRevenue.Crud.area_fiscal_revenue_get(db=db, item_id=company_id) + + # 鑸嗘儏璧勮 + # opinion_information = OpinionInformation.Crud.opinion_information_get(db=db, item_id=company_id) + + # 鍩庢姇鏌ヨ鏁版嵁琛 + # company_search_main = CompanySearchMain.Crud.company_search_main_get(db=db, item_id=company_id) + + # 涓氬姟鏋勬垚 + # company_business_compose = CompanyBusinessCompose.Crud.company_business_compose_get(db=db, item_id=company_id) + + # 璐㈠姟鎶ヨ〃 + # company_finance_main = CompanyFinanceMain.Crud.company_finance_main_get(db=db, item_id=company_id) + + # 鍙戝烘儏鍐 + # company_bond = CompanyBond.Crud.company_bond_get(db=db, item_id=company_id) + + # 鎺堜俊鎯呭喌 + # company_credit = CompanyCredit.Crud.company_credit_get(db=db, item_id=company_id) + + # 鎻愪緵鎷呬繚 + # company_guarantee = CompanyGuarantee.Crud.company_guarantee_get(db=db, item_id=company_id) + + # 鍙楅檺璧勪骇 + # company_restricted_assets = CompanyRestrictedAssets.Crud.company_restricted_assets_get(db=db, item_id=company_id) + + # 瑁佸垽鏂囦功 + # company_judicial_document = CompanyJudicialDocument.Crud.company_judicial_document_get(db=db, item_id=company_id) + + # 澶勭悊瀵瑰簲鏁版嵁 + report_data = {} + + # 鐢熸垚word鐮旀姤 + doc_path = os.path.join(os.getcwd(), 'Mods', 'IndepthResearch', 'Mods', 'CompanyReport', 'File', 'template', + '鍓嶆湡鐮旂┒鎶ュ憡锛堝煄鎶曟ā鏉匡級.docx') + doc = DocxTemplate(doc_path) + # 妯℃澘鏁版嵁鏇挎崲 + doc.render(report_data) + file_path = os.path.join(os.getcwd(), 'Mods', 'IndepthResearch', 'Mods', 'CompanyReport', 'File', 'template', + '鍓嶆湡鐮旂┒鎶ュ憡.docx') + # 鎶ュ憡琛ㄦ牸澶勭悊 + doc.save(file_path) + file = Document(file_path) + file.save(file_path) + + # 淇濆瓨鎶ュ憡鍒癿ongodb锛屽苟杩斿洖obj_id + with open(file_path, 'rb') as f: + file_id = Crud.insert_file(mongo_db, file=f.read()) + insert_data = { + "company": company.company_name, + "company_id": company_id, + "date": datetime.datetime.now().strftime("%Y-%m-%d"), + "file_id": str(file_id) + } + Crud.company_report_add(db, data=insert_data) + os.remove(file_path) + # 涓嬭浇鎶ュ憡 + response = download(fid=str(file_id)) + + return response + + +@router.get("/company_report/generate_report", summary="鐢熸垚鍓嶆湡鐮旀姤") +def company_score_add(company_id: int = "浼佷笟id", db: Session = Depends(common_db.get_db), + mongo_db: MongoConnect = Depends(mg_db.get_db)): + # 鏍规嵁浼佷笟id鏌ヨ浼佷笟鍚嶇О + company_index_main = company_index_main_get(db=db, item_id=company_id) + + # 鏍规嵁浼佷笟id鏌ヨ宸ュ晢淇℃伅 + company_base_info = company_base_info_get(db=db, item_id=company_id) + + # 鏍规嵁浼佷笟id鏌ヨ鍖哄幙淇℃伅 + company_plus_property = company_plus_property_get(db=db, item_id=company_id) + + # 璇诲彇鏁版嵁妯℃澘 + template_path = os.path.join(os.getcwd(), 'Mods', 'IndepthResearch', 'Mods', 'CompanyReport', 'File', 'template', + 'data_temp.json') + + with open(template_path, encoding='gbk') as f: + template = json.load(f) + if not template: + raise HTTPException(status_code=401, detail="鏁版嵁妯℃澘璇诲彇澶辫触") + + # 鏌ヨ鏁版嵁搴撴暟鎹 + report_data = copy.deepcopy(template) + report_data['鍏徃鍚嶇О'] = company_index_main.company_name + report_data['鎴愮珛鏃堕棿'] = company_base_info.estiblish_date + report_data['娉ㄥ唽璧勬湰'] = company_base_info.reg_capital + report_data['娉ㄥ唽鍦板潃'] = company_base_info.reg_location + area_id = company_plus_property.area + province_id = company_plus_property.province + city_id = company_plus_property.city + report_data['鍖哄幙'] = area_index_main_get(db=db, item_id=area_id).name if area_id else None + report_data['鐪'] = area_index_main_get(db=db, item_id=province_id).name if province_id else None + report_data['甯'] = area_index_main_get(db=db, item_id=city_id).name if city_id else None + + # area_id鏌ヨ + # query_id = city_id if city_id else province_id + # data_year = db.query(t_area_report).filter(t_area_report.c['area_id'] == query_id) + # data = [dict(zip(t_area_report.c.keys(), item)) for item in data_year] + # result = { + # "2020": {}, + # "2021": {}, + # "2022": {} + # } + # + # for d in data: + # year = d["骞翠唤"] + # if str(year) not in result: + # continue + # result[str(year)]["GDP"] = d["GDP(浜垮厓)"] + # result[str(year)]["GDP澧為暱鐜"] = d["GDP澧為(%)"] + # report_data['杩戜笁骞寸粡娴庢暟鎹'] = result + + # 鍐欏叆妯℃澘docx + # 鐢熸垚word鐮旀姤 + + # 澶勭悊闈炶〃鏍奸儴鍒 + doc_path = os.path.join(os.getcwd(), 'Mods', 'IndepthResearch', 'Mods', 'CompanyReport', 'File', 'template', + '鍓嶆湡鐮旂┒鎶ュ憡锛堝煄鎶曟ā鏉匡級.docx') + doc = DocxTemplate(doc_path) + doc.render(report_data) + file_path = os.path.join(os.getcwd(), 'Mods', 'IndepthResearch', 'Mods', 'CompanyReport', 'File', 'generate', + '鍓嶆湡鐮旂┒鎶ュ憡.docx') + doc.save(file_path) + + # 澶勭悊琛ㄦ牸閮ㄥ垎 + document = Document(file_path) + + data = report_data.get('杩戜笁骞寸粡娴庢暟鎹') + result1 = [["杩戜笁骞寸粡娴庢暟鎹", "2020", "2021", "2022"]] + + for k, v in data["2020"].items(): + r = [k, v, data["2021"][k], data["2022"][k]] + result1.append(r) + + table = document.tables[0] + rows = len(table.rows) + cols = len(table.columns) + for row in range(rows): + for col in range(cols): + table.cell(row, col).text = str(result1[row][col]) + + document.save(file_path) + + temp_path = "Mods/IndepthResearch/Mods/CompanyReport/File/generate" + if not os.path.exists(temp_path): + os.makedirs(temp_path) + file_name = "鍓嶆湡璋冪爺鎶ュ憡.docx" + file_path = f"{temp_path}/{file_name}" + return {"url": "/" + file_path} + + +@router.post("/company_report/delete", summary="鍒犻櫎鐮旀姤", response_model='') +def company_score_add(company_id: int = "浼佷笟ID", db: Session = Depends(common_db.get_db), + mongo_db: MongoConnect = Depends(mg_db.get_db)): + report = Crud.company_report_get(db=db, item_id=company_id) + if not report: + raise HTTPException(status_code=404, detail="鎶ュ憡涓嶅瓨鍦") + + # 鏍规嵁file_id鍒犻櫎mongodb鐨勬暟鎹 + Crud.delete_file(mongo_db, fid=report.file_id) + + # 鏍规嵁company_id 鍒犻櫎mysql鐨勬暟鎹 + Crud.company_report_delete(db, item_id=company_id) + + return { + "code": 200, + "message": "鍒犻櫎鎴愬姛" + } diff --git a/Mods/IndepthResearch/Mods/CompanyReport/Schemas.py b/Mods/IndepthResearch/Mods/CompanyReport/Schemas.py new file mode 100644 index 0000000..613a9b9 --- /dev/null +++ b/Mods/IndepthResearch/Mods/CompanyReport/Schemas.py @@ -0,0 +1,18 @@ +from typing import List, Optional +from datetime import date +from pydantic import BaseModel + + +class CompanyReportAdd(BaseModel): + company_id: int + company: str + file_id: Optional[str] + date: str + + +class CompanyReportQuery(BaseModel): + company_id: int = None + company: str = None + date: str = None + page: Optional[int] + pagesize: Optional[int] diff --git a/Mods/IndepthResearch/Mods/CompanyReport/__init__.py b/Mods/IndepthResearch/Mods/CompanyReport/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Mods/IndepthResearch/Mods/CompanyScore/Router.py b/Mods/IndepthResearch/Mods/CompanyScore/Router.py index 0956d72..eeabf30 100644 --- a/Mods/IndepthResearch/Mods/CompanyScore/Router.py +++ b/Mods/IndepthResearch/Mods/CompanyScore/Router.py @@ -1,3 +1,6 @@ +import json +import os + from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session @@ -40,4 +43,20 @@ def company_score_query(req: Schemas.CompanyScoreQueryReq, db: Session = Depends items = [Schemas.CompanyScoreInfo(**item.to_dict()) for item in query] return Schemas.CompanyScoreQueryRes(count=count, items=items) + +@router.get("/company_score/model", summary="鏌ヨ鎵撳垎妯″瀷") +def company_score_query(): + try: + path = os.path.join(os.getcwd(), 'static', 'urban_investment_model.json') + with open(path, encoding='utf-8') as f: + data = json.load(f) + return { + "code": 200, + "message": "鏂板缓鎴愬姛", + "data": data + } + except FileNotFoundError: + raise HTTPException(detail="鏈煡璇㈠埌妯″瀷", status_code=404) + + ######### diff --git a/Mods/IndepthResearch/Router.py b/Mods/IndepthResearch/Router.py index 0fd3aec..dd7b7b0 100644 --- a/Mods/IndepthResearch/Router.py +++ b/Mods/IndepthResearch/Router.py @@ -2,14 +2,16 @@ from fastapi import APIRouter, Depends from .Mods.CompanyScore import Router as CompanyScoreRouter from .Mods.CompanyConclusion import Router as CompanyConclusionRouter from .Mods.CompanyMeetingRecord import Router as CompanyMeetingRecordRouter +from .Mods.CompanyReport import Router as CompanyReport from Context.common import auth_util router = APIRouter( prefix="/indepth_research", - dependencies=[Depends(auth_util.token_data_depend)] + # dependencies=[Depends(auth_util.token_data_depend)] ) router.include_router(CompanyScoreRouter.router) router.include_router(CompanyConclusionRouter.router) router.include_router(CompanyMeetingRecordRouter.router) +router.include_router(CompanyReport.router) diff --git a/Mods/RegionalEconomies/models.py b/Mods/RegionalEconomies/models.py index 2683c11..39f0193 100644 --- a/Mods/RegionalEconomies/models.py +++ b/Mods/RegionalEconomies/models.py @@ -2,7 +2,7 @@ from sqlalchemy import Column, Float, Integer, MetaData, Table from Context.common import common_db t_area_report = Table( - 'area_report', common_db.Base, + 'area_report', common_db.Base.metadata, Column('area_id', Integer), Column('骞翠唤', Integer), Column('GDP(浜垮厓)', Float), diff --git a/requirements.txt b/requirements.txt index b2f4e00..38704d2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,4 +12,6 @@ pymysql==1.0.2 fastapi-crudrouter==0.8.6 redis==4.5.1 jurigged==0.5.5 -pymongo==4.3.3 \ No newline at end of file +pymongo==4.3.3 +docxtpl~=0.16.6 +python-docx~=0.8.11 \ No newline at end of file