diff --git a/src/main/kotlin/team8/fruitable/controller/action/OrderAction.kt b/src/main/kotlin/team8/fruitable/controller/action/OrderAction.kt index 065d1ae..35e3a3f 100644 --- a/src/main/kotlin/team8/fruitable/controller/action/OrderAction.kt +++ b/src/main/kotlin/team8/fruitable/controller/action/OrderAction.kt @@ -1,10 +1,7 @@ package team8.fruitable.controller.action import org.springframework.stereotype.Controller -import org.springframework.web.bind.annotation.CookieValue -import org.springframework.web.bind.annotation.PathVariable -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RequestParam +import org.springframework.web.bind.annotation.* import org.springframework.web.servlet.mvc.support.RedirectAttributes import team8.fruitable.controller.util.error import team8.fruitable.datebase.entity.Item @@ -25,37 +22,6 @@ class OrderAction( return token?.let(accountRepository::findByToken) } - @RequestMapping("/create/{item_id}/{item_number}") - fun create( - attributes: RedirectAttributes, - @CookieValue("TOKEN", required = false) token: String?, - @PathVariable("item_id") itemId: Long, - @PathVariable("item_number") itemNumber: Int, - @RequestParam("name") name: String, - @RequestParam("phone") phone: String, - @RequestParam("address") address: String, - @RequestParam("pay_type") payTypeId: Long, - @RequestParam("redirect", required = false) redirect: String? - ): String { - val user = this.getCurrentUser(token) ?: return error(attributes, "创建订单", "账户未登录", "/login") - val item = itemRepository.findById(itemId).orElse(null) ?: return error(attributes, "创建订单", "商品不存在") - val payType = - payTypeRepository.findById(payTypeId).orElse(null) ?: return error(attributes, "创建订单", "未知的支付类型") - val itemPrice = item.price?.times(itemNumber) ?: return error( - attributes, "创建订单", "错误的商品价格(可能是商品未添加对应的价格)" - ) - var order = Order(user, name, phone, address, payType, itemPrice, mutableListOf(item)) - order = orderRepository.save(order) - - // 更新 orders_to_items 表中的个数信息 - val orderedItem = orderedItemRepository.findByOrder(order)[0] - orderedItem.number = itemNumber - orderedItemRepository.save(orderedItem) - - redirect?.let { return "redirect:/${it}" } - return "redirect:/order/${order.id}" - } - @RequestMapping("/create") fun createForCart( attributes: RedirectAttributes, @@ -70,27 +36,34 @@ class OrderAction( ): String { val user = this.getCurrentUser(token) ?: return error(attributes, "创建订单", "账户未登录", "/login") val itemPairs = itemIds zip itemNumbers - val items: MutableList = mutableListOf() + val items: MutableList> = mutableListOf() var priceSum = .0 itemPairs.forEach { - items.plus( + val item = itemRepository.findById(it.first).orElse(null) ?: return error(attributes, "创建订单", "商品不存在") - ) + items.add(item to it.second) } items.forEach { item -> - item.price?.let { - priceSum += it - } ?: return error(attributes, "创建订单", "错误的商品价格(可能是商品未添加对应的价格)") + item.first.price?.let { + priceSum += it * item.second + } ?: return error(attributes, "创建订单", "[内部错误] 错误的商品价格(可能是商品未添加对应的价格)") } val payType = payTypeRepository.findById(payTypeId).orElse(null) ?: return error(attributes, "创建订单", "未知的支付类型") - val order = Order(user, name, phone, address, payType, priceSum, items) - orderRepository.save(order) + var order = Order(user, name, phone, address, payType, priceSum, items.map { it.first } as MutableList) + order = orderRepository.save(order) + + val orderedItems = orderedItemRepository.findByOrder(order) + (orderedItems zip items).forEach { + it.first.number = it.second.second + orderedItemRepository.save(it.first) + } + redirect?.let { return "redirect:/${it}" } return "redirect:/order/${order.id}" } - @RequestMapping("/update/{id}") + @PostMapping("/update/{id}") fun update( attributes: RedirectAttributes, @CookieValue("TOKEN", required = false) token: String?, diff --git a/src/main/kotlin/team8/fruitable/controller/page/OrderPage.kt b/src/main/kotlin/team8/fruitable/controller/page/OrderPage.kt index 9b7e050..6e90139 100644 --- a/src/main/kotlin/team8/fruitable/controller/page/OrderPage.kt +++ b/src/main/kotlin/team8/fruitable/controller/page/OrderPage.kt @@ -9,16 +9,17 @@ import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.servlet.mvc.support.RedirectAttributes import team8.fruitable.controller.util.error +import team8.fruitable.datebase.entity.Item import team8.fruitable.datebase.entity.User -import team8.fruitable.datebase.repository.AccountRepository -import team8.fruitable.datebase.repository.ItemRepository -import team8.fruitable.datebase.repository.OrderRepository +import team8.fruitable.datebase.repository.* @Controller class OrderPage( private val orderRepository: OrderRepository, + private val orderedItemRepository: OrderedItemRepository, private val accountRepository: AccountRepository, - private val itemRepository: ItemRepository + private val itemRepository: ItemRepository, + private val payTypeRepository: PayTypeRepository ) { private fun getCurrentUser(token: String?): User? { return token?.let(accountRepository::findByToken) @@ -26,7 +27,8 @@ class OrderPage( @RequestMapping("/orders") fun orders( - model: Model, attributes: RedirectAttributes, @CookieValue("TOKEN", required = false) token: String? + model: Model, attributes: RedirectAttributes, + @CookieValue("TOKEN", required = false) token: String? ): String { model["isOrder"] = true val user = this.getCurrentUser(token) ?: return error(attributes, "查看订单列表", "账户未登录", "/login") @@ -39,7 +41,8 @@ class OrderPage( model: Model, attributes: RedirectAttributes, @CookieValue("TOKEN", required = false) token: String?, - @RequestParam("id") id: Array + @RequestParam("items") itemIds: Array, + @RequestParam("numbers") itemNumbers: Array ): String { val user = this.getCurrentUser(token) ?: return error(attributes, "更新", "账户未登录", "/login") model["user"] = user @@ -56,8 +59,29 @@ class OrderPage( ): String { val user = this.getCurrentUser(token) ?: return error(attributes, "查看订单", "账户未登录", "/login") val item = itemRepository.findById(id).orElse(null) ?: return error(attributes, "查看订单", "商品不存在") + model["payTypes"] = payTypeRepository.findAll() model["items"] = arrayOf(Triple(item, number ?: 1, item.price!! * (number ?: 1))) model["user"] = user return "order/create" } + + @RequestMapping("/order/{id}") + fun order( + model: Model, + attributes: RedirectAttributes, + @CookieValue("TOKEN", required = false) token: String?, + @PathVariable("id") id: Long + ): String { + val user = this.getCurrentUser(token) ?: return error(attributes, "查看订单", "账户未登录", "/login") + val order = orderRepository.findById(id).orElse(null) ?: return error(attributes, "查看订单", "订单不存在") + val orderedItems = orderedItemRepository.findByOrder(order) + val items: MutableList> = mutableListOf() + orderedItems.forEach { + items.add(Triple(it.item!!, it.number, it.item!!.price!! * it.number)) + } + model["order"] = order + model["items"] = items + model["user"] = user + return "order/update" + } } \ No newline at end of file diff --git a/src/main/kotlin/team8/fruitable/datebase/entity/OrderedItem.kt b/src/main/kotlin/team8/fruitable/datebase/entity/OrderedItem.kt index 641749a..913fa58 100644 --- a/src/main/kotlin/team8/fruitable/datebase/entity/OrderedItem.kt +++ b/src/main/kotlin/team8/fruitable/datebase/entity/OrderedItem.kt @@ -18,6 +18,6 @@ class OrderedItem( @JoinColumn(name = "item_id", nullable = false) var item: Item? = null, - @Column(name = "number") - var number: Int? = 0 + @Column(name = "number", nullable = false) + var number: Int = 0 ) diff --git a/src/main/kotlin/team8/fruitable/datebase/entity/PayType.kt b/src/main/kotlin/team8/fruitable/datebase/entity/PayType.kt index 1c6cced..4617eb1 100644 --- a/src/main/kotlin/team8/fruitable/datebase/entity/PayType.kt +++ b/src/main/kotlin/team8/fruitable/datebase/entity/PayType.kt @@ -7,9 +7,19 @@ import jakarta.persistence.* class PayType( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "ID", nullable = false) + @Column(name = "id", nullable = false) var id: Long? = null, - @Column(name = "NAME", length = 16, nullable = false) + @Column(name = "name", length = 16, nullable = false) var name: String? = null, -) + + @Column(name = "picture", length = 16, nullable = false) + var picture: String? = null, + + @Column(name = "is_default", nullable = false) + var isDefault: Boolean = false, +) { + fun getPictureLink(): String { + return "/images/pay_type/${picture ?: "Card"}.svg" + } +} diff --git a/src/main/kotlin/team8/fruitable/datebase/entity/User.kt b/src/main/kotlin/team8/fruitable/datebase/entity/User.kt index 51de8ad..21570f5 100644 --- a/src/main/kotlin/team8/fruitable/datebase/entity/User.kt +++ b/src/main/kotlin/team8/fruitable/datebase/entity/User.kt @@ -83,6 +83,7 @@ class User( if (!email.isNullOrBlank()) this.email = email if (!address.isNullOrBlank()) this.address = address isAdmin?.let { this.isAdmin = it } + this.updateToken() } private final fun genPassword(password: String): String { diff --git a/src/main/resources/static/images/pay_type/Card.svg b/src/main/resources/static/images/pay_type/Card.svg new file mode 100644 index 0000000..b79fc8b --- /dev/null +++ b/src/main/resources/static/images/pay_type/Card.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/src/main/resources/static/styles/form.less b/src/main/resources/static/styles/form.less index c00b2b1..f79e9f1 100644 --- a/src/main/resources/static/styles/form.less +++ b/src/main/resources/static/styles/form.less @@ -22,6 +22,28 @@ > .ItemName { flex: 1 0 0; + + &[radioed] { + display: flex; + align-items: center; + justify-content: center; + + > .ItemInput { + display: none; + } + + > .ItemPicture { + width: 50px; + height: 50px; + padding: 10px 5px; + border-radius: 15px; + background: white; + } + + > .ItemInput:checked + .ItemPicture { + background: wheat; + } + } } > .ItemInput { @@ -30,6 +52,12 @@ border-radius: 10px; border: 3px solid skyblue; } + + > .ItemInput[disabled] { + background: unset; + border: unset; + color: black; + } } } diff --git a/src/main/resources/static/styles/order.less b/src/main/resources/static/styles/order.less index e69de29..8f6dca2 100644 --- a/src/main/resources/static/styles/order.less +++ b/src/main/resources/static/styles/order.less @@ -0,0 +1,36 @@ +.Item { + margin-top: 10px; + margin-bottom: 10px; + border-radius: 15px; + background-color: teal; + + + > .Picture { + display: flex; + align-items: center; + justify-content: center; + + > img { + border-radius: 15px; + width: 100px; + height: 100px; + } + } + + > .Infos { + display: flex; + flex-direction: column; + padding: 5px 10px; + color: wheat; + + > .Info { + display: flex; + flex-direction: row; + justify-content: space-between; + + > .Title { + + } + } + } +} diff --git a/src/main/resources/templates/order/create.mustache b/src/main/resources/templates/order/create.mustache index 7bcb07b..b9c98fd 100644 --- a/src/main/resources/templates/order/create.mustache +++ b/src/main/resources/templates/order/create.mustache @@ -49,40 +49,32 @@ {{#user.address}}value="{{user.address}}"{{/user.address}} required/> -
- - - - - - - - - - - - - - +
+ {{#payTypes}} + + {{/payTypes}}
{{#items}} -
+
商品图片
-
+
- {{first.name}} - - ¥${{first.price}} +
{{first.name}}
+
+ {{first.price}}¥ x{{second}} - ¥${{third}} - + = {{third}}¥ +
{{first.description}}
diff --git a/src/main/resources/templates/order/update.mustache b/src/main/resources/templates/order/update.mustache index b458bf1..a248957 100644 --- a/src/main/resources/templates/order/update.mustache +++ b/src/main/resources/templates/order/update.mustache @@ -12,6 +12,7 @@ + @@ -29,47 +30,76 @@
订单
+
+
+ + +
+ +
+ + +
+ + +
+
+ value="{{order.name}}" required/> +
+ +
+ +
+ value="{{order.address}}" required/>
-
- - - -
+ {{^order.isRevoked}} +
+ + {{^order.isPaid}} + + {{/order.isPaid}} + +
+ {{/order.isRevoked}}
{{#items}} -
+
- 商品图片 + 商品图片
- -
+
- {{a.name}} - - ¥${{a.price}} - x{{b}} - ¥${{c}} - +
{{first.name}}
+
+ {{first.price}}¥ + x{{second}} + = {{third}}¥ +
- {{a.description}} + {{first.description}}
{{/items}}