CDiandanOrderItem.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #include "../pch/pch.h"
  2. #include "CDiandanOrderItem.h"
  3. #include "../tool/CSqlite3.h"
  4. bool CDiandanOrderItem::operator==(CDiandanOrderItem& t)
  5. {
  6. if (this->m_is_taocan != t.m_is_taocan)
  7. {
  8. return false;
  9. }
  10. if (this->is_dabao != t.is_dabao)
  11. {
  12. return false;
  13. }
  14. if (this->m_is_taocan == true)
  15. {
  16. if (this->foodpackage_id != t.foodpackage_id)
  17. {
  18. return false;
  19. }
  20. int i = 0;
  21. for (std::vector<FoodNatureSelectValue>::iterator it = this->natureSelectedArray.begin(); it != this->natureSelectedArray.end(); it++)
  22. {
  23. if ((*it).nature_name != t.natureSelectedArray[i].nature_name)
  24. {
  25. return false;
  26. }
  27. if ((*it).m_nature_selects != t.natureSelectedArray[i].m_nature_selects)
  28. {
  29. return false;
  30. }
  31. i++;
  32. }
  33. }
  34. else
  35. {
  36. if (this->food_id != t.food_id)
  37. {
  38. return false;
  39. }
  40. if (this->is_nature != t.is_nature)
  41. {
  42. return false;
  43. }
  44. if (this->is_nature == true)
  45. {
  46. int i = 0;
  47. for (std::vector<FoodNatureSelectValue>::iterator it = this->natureSelectedArray.begin(); it != this->natureSelectedArray.end(); it++)
  48. {
  49. if ((*it).nature_name != t.natureSelectedArray[i].nature_name)
  50. {
  51. return false;
  52. }
  53. if ((*it).m_nature_selects != t.natureSelectedArray[i].m_nature_selects)
  54. {
  55. return false;
  56. }
  57. i++;
  58. }
  59. }
  60. }
  61. return true;
  62. }
  63. void CDiandanOrderItem::SetFoodInfo(CFood info)
  64. {
  65. m_foodinfo = info;
  66. }
  67. void CDiandanOrderItem::SetFoodpackageInfo(CFoodpackage info)
  68. {
  69. m_foodpackageinfo = info;
  70. m_is_taocan = true;
  71. }
  72. void CDiandanOrderItem::SetIsTaocan(bool is_taocan)
  73. {
  74. m_is_taocan = is_taocan;
  75. }
  76. void CDiandanOrderItem::SetDabao()
  77. {
  78. is_dabao = true;
  79. }
  80. CFood CDiandanOrderItem::GetFoodInfo()
  81. {
  82. return m_foodinfo;
  83. }
  84. CFoodpackage CDiandanOrderItem::GetFoodpackageInfo()
  85. {
  86. return m_foodpackageinfo;
  87. }
  88. bool CDiandanOrderItem::GetIsTaocan()
  89. {
  90. return m_is_taocan;
  91. }
  92. //根据当前选中的商品属性、份数,计算商品的展示价格
  93. void CDiandanOrderItem::UpdateShowPrice()
  94. {
  95. if (m_is_taocan == true)
  96. {
  97. show_price = CLewaimaiString::DoubleToString(std::stod(price) * std::stod(num), 2);
  98. }
  99. else
  100. {
  101. if (!is_nature)
  102. {
  103. show_price = CLewaimaiString::DoubleToString(std::stod(price) * std::stod(num), 2);
  104. }
  105. else
  106. {
  107. double nature_price = 0;
  108. for (std::vector<FoodNatureSelectValue>::iterator it = this->natureSelectedArray.begin(); it != this->natureSelectedArray.end(); it++)
  109. {
  110. std::vector<FoodNatureSelectValueInfo> infoArray = (*it).m_nature_selects;
  111. for (std::vector<FoodNatureSelectValueInfo>::iterator it2 = infoArray.begin(); it2 != infoArray.end(); it2++)
  112. {
  113. nature_price += it2->nature_select_value;
  114. }
  115. }
  116. double new_price = std::stod(price) + nature_price;
  117. show_price = CLewaimaiString::DoubleToString(new_price * std::stod(num), 2);
  118. }
  119. }
  120. }
  121. std::string CDiandanOrderItem::getSinglePrice()
  122. {
  123. if (m_is_taocan == true)
  124. {
  125. return price;
  126. }
  127. else
  128. {
  129. if (!is_nature)
  130. {
  131. return price;
  132. }
  133. else
  134. {
  135. double nature_price = 0;
  136. for (std::vector<FoodNatureSelectValue>::iterator it = this->natureSelectedArray.begin(); it != this->natureSelectedArray.end(); it++)
  137. {
  138. std::vector<FoodNatureSelectValueInfo> infoArray = (*it).m_nature_selects;
  139. for (std::vector<FoodNatureSelectValueInfo>::iterator it2 = infoArray.begin(); it2 != infoArray.end(); it2++)
  140. {
  141. nature_price += it2->nature_select_value;
  142. }
  143. }
  144. double new_price = std::stod(price) + nature_price;
  145. show_price = CLewaimaiString::DoubleToString(new_price, 2);
  146. return show_price;
  147. }
  148. }
  149. }
  150. std::string CDiandanOrderItem::getNameShow()
  151. {
  152. if (m_is_taocan)
  153. {
  154. if (is_dabao)
  155. {
  156. return CLewaimaiString::UnicodeToUTF8(L"【打包】") + foodpackage_name;
  157. }
  158. else
  159. {
  160. return foodpackage_name;
  161. }
  162. }
  163. else
  164. {
  165. if (is_dabao)
  166. {
  167. return CLewaimaiString::UnicodeToUTF8(L"【打包】") + food_name;
  168. }
  169. else
  170. {
  171. return food_name;
  172. }
  173. }
  174. }
  175. //获取用于展示的属性名字
  176. std::string CDiandanOrderItem::getNatureShow()
  177. {
  178. std::string show = "";
  179. if (m_is_taocan)
  180. {
  181. size_t i = 0;
  182. for (std::vector<FoodNatureSelectValue>::iterator it = this->natureSelectedArray.begin(); it != this->natureSelectedArray.end(); it++)
  183. {
  184. show += (*it).GetNatureSelectName();
  185. if (i < natureSelectedArray.size() - 1)
  186. {
  187. show += CLewaimaiString::UnicodeToUTF8(L"、");
  188. }
  189. i++;
  190. }
  191. }
  192. else
  193. {
  194. if (!is_nature)
  195. {
  196. return "";
  197. }
  198. size_t i = 0;
  199. for (std::vector<FoodNatureSelectValue>::iterator it = this->natureSelectedArray.begin(); it != this->natureSelectedArray.end(); it++)
  200. {
  201. show += (*it).GetNatureSelectName();
  202. if (i < natureSelectedArray.size() - 1)
  203. {
  204. show += CLewaimaiString::UnicodeToUTF8(L"、");
  205. }
  206. i++;
  207. }
  208. }
  209. return show;
  210. }
  211. //获取用于完整展示的商品名字,包含商品本身的名字和属性,例如“【打包】珍珠奶茶【大杯、热、多糖】”
  212. std::string CDiandanOrderItem::getNameWanzhengShow()
  213. {
  214. std::string name_first = getNameShow();
  215. std::string nature = "";
  216. //后面再加上属性或者套餐商品名字
  217. if (m_is_taocan)
  218. {
  219. nature = CLewaimaiString::UnicodeToUTF8(L"【") + getNatureShow() + CLewaimaiString::UnicodeToUTF8(L"】");
  220. }
  221. else
  222. {
  223. if (is_nature)
  224. {
  225. nature = CLewaimaiString::UnicodeToUTF8(L"【") + getNatureShow() + CLewaimaiString::UnicodeToUTF8(L"】");
  226. }
  227. else
  228. {
  229. nature = "";
  230. }
  231. }
  232. std::string total_name = name_first + nature;
  233. return total_name;
  234. }
  235. double CDiandanOrderItem::getMemberYouhui(std::string member_level)
  236. {
  237. if (is_member_price_used == false)
  238. {
  239. return 0;
  240. }
  241. for (std::vector<MemberPrice>::iterator it = m_member_price.begin(); it != m_member_price.end(); it++)
  242. {
  243. MemberPrice cur = *it;
  244. if (cur.level == member_level)
  245. {
  246. double member_food_price = atof(cur.price.c_str());
  247. double yuan_price = atof(price.c_str());
  248. double chajia = (yuan_price - member_food_price) * atof(num.c_str());
  249. return chajia;
  250. }
  251. }
  252. return 0;
  253. }
  254. double CDiandanOrderItem::getDabaoMoney()
  255. {
  256. if (is_dabao)
  257. {
  258. return atof(dabao_money.c_str()) * (int)atof(num.c_str());
  259. }
  260. return 0;
  261. }
  262. //获取点单相关的参数(仅用于非套餐)
  263. std::map<string, string> CDiandanOrderItem::getDiandanParam()
  264. {
  265. std::map<string, string> param;
  266. if (m_is_taocan)
  267. {
  268. return param;
  269. }
  270. param["food_id"] = food_id;
  271. if (is_nature)
  272. {
  273. param["food_name"] = food_name + CLewaimaiString::UnicodeToUTF8(L"【") + getNatureShow() + CLewaimaiString::UnicodeToUTF8(L"】");
  274. }
  275. else
  276. {
  277. param["food_name"] = food_name;
  278. }
  279. param["food_price"] = getSinglePrice();
  280. if (is_dabao)
  281. {
  282. param["is_dabao"] = "1";
  283. }
  284. else
  285. {
  286. param["is_dabao"] = "0";
  287. }
  288. param["quantity"] = num;
  289. param["type_id"] = m_foodinfo.type_id;
  290. param["foodpackage_id"] = "0";
  291. param["is_foodpackage"] = "0";
  292. return param;
  293. }
  294. //获取点单相关的套餐参数(仅用于套餐)
  295. rapidjson::Document CDiandanOrderItem::getDiandanTaocanParam(rapidjson::Document::AllocatorType &allocator)
  296. {
  297. rapidjson::Document doc(&allocator);
  298. doc.SetObject();
  299. if (m_is_taocan == false)
  300. {
  301. return doc;
  302. }
  303. Value v_foodpackage_id;
  304. v_foodpackage_id.SetString(foodpackage_id.c_str(), foodpackage_id.length(), allocator);
  305. doc.AddMember("foodpackage_id", v_foodpackage_id, allocator);
  306. std::string show_name = foodpackage_name;
  307. Value v_show_name;
  308. v_show_name.SetString(show_name.c_str(), show_name.length(), allocator);
  309. doc.AddMember("food_name", v_show_name, allocator);
  310. std::string single_price = getSinglePrice();
  311. Value v_single_price;
  312. v_single_price.SetString(single_price.c_str(), single_price.length(), allocator);
  313. doc.AddMember("food_price", v_single_price, allocator);
  314. if (is_dabao)
  315. {
  316. doc.AddMember("is_dabao", "0", allocator);
  317. }
  318. else
  319. {
  320. doc.AddMember("is_dabao", "1", allocator);
  321. }
  322. Value v_num;
  323. v_num.SetString(num.c_str(), num.length(), allocator);
  324. doc.AddMember("quantity", v_num, allocator);
  325. std::vector<std::map<string, string>> item;
  326. //遍历套餐的每个商品
  327. for (std::vector<FoodNatureSelectValue>::iterator it = natureSelectedArray.begin(); it != natureSelectedArray.end(); it++)
  328. {
  329. std::map<string, string> curFood;
  330. FoodNatureSelectValue curNature = *it;
  331. std::string foodpackage_nature = m_foodpackageinfo.nature;
  332. rapidjson::Document doc_tmp = CLewaimaiJson::StringToJson(foodpackage_nature, allocator);
  333. std::string food_id = doc_tmp[curNature.nNameIndex]["value"][curNature.m_nature_selects[0].nature_select_index].GetString();
  334. curFood["food_id"] = food_id;
  335. CFood foodinfo;
  336. CSqlite3 sqlite;
  337. bool is_found = sqlite.GetFoodById(food_id, foodinfo);
  338. if (!is_found)
  339. {
  340. continue;
  341. }
  342. curFood["food_name"] = foodinfo.name;
  343. curFood["food_price"] = foodinfo.price;
  344. if (is_dabao)
  345. {
  346. curFood["is_dabao"] = "1";
  347. }
  348. else
  349. {
  350. curFood["is_dabao"] = "0";
  351. }
  352. curFood["quantity"] = num;
  353. curFood["type_id"] = foodinfo.type_id;
  354. curFood["foodpackage_id"] = foodpackage_id;
  355. curFood["is_foodpackage"] = "1";
  356. item.push_back(curFood);
  357. }
  358. Value& data = CLewaimaiJson::ParamArrayToJson(item, allocator);
  359. doc.AddMember("item", data, allocator);
  360. return doc;
  361. }