CDiandanOrderItem.cpp 8.3 KB

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