From bade3bd2cebb2126ba4936397013b05fa8d930a0 Mon Sep 17 00:00:00 2001 From: TTX Date: Sat, 17 Jun 2023 15:20:05 +0800 Subject: [PATCH 1/2] xiugai --- pom.xml | 8 ++ .../com/online/edu/config/AlipayConfig.java | 36 +++++++++ .../order/CourseElectionController.java | 45 ++++++++++- .../order/CourseOrderController.java | 35 +++++++- .../edu/entity/order/CourseElection.java | 15 ++++ .../online/edu/entity/order/CourseOrder.java | 12 +++ .../edu/entity/order/vo/CourseElectionVO.java | 48 +++++++++++ .../edu/entity/order/vo/CourseOrderVO.java | 29 +++++++ .../mapper/order/CourseElectionMapper.java | 11 +++ .../edu/mapper/order/CourseOrderMapper.java | 6 ++ .../service/order/CourseElectionService.java | 7 ++ .../edu/service/order/CourseOrderService.java | 3 +- .../edu/service/order/impl/AlipayService.java | 81 +++++++++++++++++++ .../order/impl/CourseElectionServiceImpl.java | 18 +++++ .../order/impl/CourseOrderServiceImpl.java | 9 +++ .../java/com/online/edu/utils/JSONUtils.java | 16 ++++ .../resources/mapper/CourseElectionMapper.xml | 22 +++++ .../resources/mapper/CourseOrderMapper.xml | 12 +++ 18 files changed, 410 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/online/edu/config/AlipayConfig.java create mode 100644 src/main/java/com/online/edu/entity/order/vo/CourseElectionVO.java create mode 100644 src/main/java/com/online/edu/entity/order/vo/CourseOrderVO.java create mode 100644 src/main/java/com/online/edu/service/order/impl/AlipayService.java create mode 100644 src/main/java/com/online/edu/utils/JSONUtils.java diff --git a/pom.xml b/pom.xml index 215778a..c59a31c 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,14 @@ 17 + + com.alipay.sdk + alipay-easysdk + 2.2.3 + + + + org.springframework.boot spring-boot-starter-data-redis diff --git a/src/main/java/com/online/edu/config/AlipayConfig.java b/src/main/java/com/online/edu/config/AlipayConfig.java new file mode 100644 index 0000000..f5f1c57 --- /dev/null +++ b/src/main/java/com/online/edu/config/AlipayConfig.java @@ -0,0 +1,36 @@ +package com.online.edu.config; + +import com.alipay.easysdk.factory.Factory; +import com.alipay.easysdk.kernel.Config; +import org.springframework.stereotype.Component; + +@Component +public class AlipayConfig { + static { + Factory.setOptions(getOptions()); + } + + private static Config getOptions() { + Config config = new Config(); + + config.protocol = "https"; + + // 沙箱环境修改为 openapi.alipaydev.com + config.gatewayHost = "openapi-sandbox.dl.alipaydev.com/gateway.do"; + + config.signType = "RSA2"; + + config.appId = "9021000122682309"; + + // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中 + config.merchantPrivateKey ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCZnas0p9C7kXBonkSJTNK5e5rECUQHS3s1YnAUbxaxEKQ/k6vSShNYKhum2N5jXKc4sxFpwqRWrnmT9/a9/cc+pB7XCOZ2dJR7r6pFGCKpbi7bz+/eAVi2J+Zr+2a5fXcbuStYnkFBUPw5ad+UoGtnl/38VEiBHaY57catW3x5GTNiEwmDBXUga6GBrEcTdxhuYX5oe7URR0V4qXC5nKTw+iuyDik5Yqp+4UpXbDv1NEyxRDge1KEtr7YgIe8lF1K9sK3TKeuyQRueMx/I/sddliXpFa+7BlUqDBVhzcyQYYei5eo6k/OkR0GdDMS0bFoJlgIB/4Y1QrlBi62JFlWdAgMBAAECggEBAIOi+PNGWzhgiVMdTN3bVzhbE2SqDAG0TQCSOjlIeMmC2T0h7jtl/u2uJ8PyU6AR5bpQuLtIcEtXeBe0hVyJ/BNR0Ju0pMkdXaAgCGjtJcbLBJyYs5Qq0zpFNs+Rv5V0Tn0TUHHAK2stWvZYI5/ZWDCNrrhsATWjkq5Rc89adm/ONKy8INzggGlr7EoNL9tjP9Vg39MxBfvFP8HC+MFlNOEw+OCxmyaFk9djTFcPTD3tVBkyKZcCjhhSREZrlUstWfGUXhyuMNoJZ38kKJoyCrug7p+U50mNYeLXRl9OoBU+F2IwsUa72VqKN0rcudVcFKNQt79h+IVKldjndSwA1oECgYEAyeZzuiyKc/gA5o8ShdOv08e7g/t5LciXBEaMb4mhACV3zq6SNfFtv4V+8xYpVUNqEnDejMAM2Dgb/iUEcKkCN5V8RGooSll0vFH/jdnN46meJUV4OFplwjUrMWSz2lhxbtpyeMbR6fdgJ2OBYqQb4DTmNwsNXh+B43022GWQi0cCgYEAwscbFzXMtBgF+1JSA99FnMoXXhvUdwi/J1hRit4o6E2ZAAO6ZrMFbT+7vdOWeBDHqmUgqSPwmuNXfYVToYkNf1+3L0bRI9ywpnFYSzIF+0V+J7rKzN0a5+yYWgoiQ3MrQxVu1R6oEp4CAcTCEADAmoIZt1hE5rPcbSm4+vnZgfsCgYAWRgQyX8OOc6e1x8blWKsnE/vN3apKsLxoTlgj3e0TFQ8YkA21QcM3w8RzkNhSfbNwAV+r62v/VoFQEklhHGQQOb+R1JxJR4EnXWxx+7tJCGdX+Uf//4kGWdNACboFtogdEKBcTqLNyuqircmSVkR1AaD8JY39+j/cFu48BUhi9QKBgDbvWSCut9GDhQxhJ2iYLFYQWY7dYotAGEnMixMTnY4HdMOgSY51p24sr5LWfnXWpbnVU+TgkhfdTuvEEtZYhUoLgOsAmjP3JouLJE/xB+barr4d3Nzy74fCHyYl/Yvwi58cCGprfxPhJMfC6skArVHmp8IiTyySRfUz80w187bJAoGAbbjYLPjeaP+4ZZt0/Rf7oDRpX69GzbDo5PWGc6A1BbK21/aXoom8yJDZG1DHYX0tLcQ8QwcwcMmLRITg0HQ/1mF5RQcTaYDBurP+uc88a9t683ItTZcWuXBexA32I61o0fbWYqMNBYsC8gQxI6j+npUUe+x5soBQhQb16Vj70r0="; + + //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可 + config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuHXKkCpzlrER7wZqPE3hkqB9fYZ9neEgwldrkqcNg8ds6PGTjkpZJKdWBzz89i19Dfqoa2TffVyC2Rz5lYMoeN3WE/KsfySa4zHsKqzEm08ndO8O46xJb/2VbIxsc9cvRH5GEQiHxzWIAZpBEolZgETQyCBrGlcjMyhjG5+XQ22P2Iye/+6qMwQnnz95E6GnZH9hr6Z/Tcc1+PBNJMhGotJrOfnTYabu0KOHIxE0p4G8vRC5TKXEWHj9MeV49piTR5WO8W6LUBQq7Vn1XZ7+ZDuOdYQ4xQXuMAhxnX6HW17d6eJNn4Ir8g6BFt/0jnXoOT4x19mbXq39IUArg6NcUQIDAQAB"; + + //可设置异步通知接收服务地址(可选) + config.notifyUrl = ""; + + return config; + } +} diff --git a/src/main/java/com/online/edu/controller/order/CourseElectionController.java b/src/main/java/com/online/edu/controller/order/CourseElectionController.java index 71242d5..02476d4 100644 --- a/src/main/java/com/online/edu/controller/order/CourseElectionController.java +++ b/src/main/java/com/online/edu/controller/order/CourseElectionController.java @@ -3,13 +3,21 @@ package com.online.edu.controller.order; import com.online.edu.common.CommonResult; import com.online.edu.entity.order.CourseElection; +import com.online.edu.entity.order.vo.CourseElectionVO; import com.online.edu.service.order.CourseElectionService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.math.BigDecimal; +import java.util.Date; +import java.util.List; + +import static com.online.edu.utils.OrderSerialNumberUtil.getSerialNumber; + @Tag(name = "选课控制") @RestController @RequestMapping("/course/election") @@ -21,8 +29,9 @@ public class CourseElectionController { @Operation(description = "添加选课记录") @PostMapping("/add") - public CommonResult addCourse(@RequestBody @Valid CourseElection courseElection) { + public CommonResult addCourse(@RequestBody @Valid CourseElectionVO courseElectionvo) { //TODO:建立VO + CourseElection courseElection=new CourseElection(courseElectionvo.getId(),getSerialNumber(),courseElectionvo.getCourseId(),courseElectionvo.getCourseName(),courseElectionvo.getTeacherName(),courseElectionvo.getStudentId(),courseElectionvo.getStudentName(),courseElectionvo.getPrice(), 0,0,new Date(),new Date()); boolean success = courseElectionService.save(courseElection); if (success) { return CommonResult.success(courseElection); @@ -31,6 +40,40 @@ public class CourseElectionController { } } + /** + * 根据id查询选课结果 + */ + @GetMapping("/selectbyid") + public CommonResult> selectCoursebyid(@RequestBody Long studentid ){ + List courselist=courseElectionService.getById(studentid); + for(CourseElection e:courselist){ + System.out.println(e); + if(e.getStatus() == 0 && e.getPrice().compareTo(BigDecimal.ZERO) != 0 ){ + courselist.remove(e); + } + } + return CommonResult.success(courselist); + } + + /** + * 根据课程id查询选课结果 + */ + + @GetMapping("/selectbycourse") + public CommonResult> selectCoursebycourse(@RequestBody Long courseid ){ + List courselist=courseElectionService.getByCourse(courseid); + if(courselist.isEmpty()){ + return CommonResult.error(400,"查询失败"); + } + for(CourseElection e:courselist){ + if(e.getStatus() == 0 && e.getPrice().compareTo(BigDecimal.ZERO) != 0 ){ + courselist.remove(e); + } + } + return CommonResult.success(courselist); + } + + } diff --git a/src/main/java/com/online/edu/controller/order/CourseOrderController.java b/src/main/java/com/online/edu/controller/order/CourseOrderController.java index c3d6a1a..b613c5c 100644 --- a/src/main/java/com/online/edu/controller/order/CourseOrderController.java +++ b/src/main/java/com/online/edu/controller/order/CourseOrderController.java @@ -4,14 +4,19 @@ package com.online.edu.controller.order; import com.online.edu.common.CommonResult; import com.online.edu.entity.order.CourseElection; import com.online.edu.entity.order.CourseOrder; +import com.online.edu.entity.order.vo.CourseOrderVO; import com.online.edu.service.order.CourseElectionService; import com.online.edu.service.order.CourseOrderService; +import com.online.edu.service.order.impl.AlipayService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.math.BigDecimal; + @Tag(name = "订单控制") @RestController @RequestMapping("/course/order") @@ -24,10 +29,14 @@ public class CourseOrderController { @Autowired private CourseOrderService courseOrderService; + @Autowired + private AlipayService alipayService; + @Operation(description = "添加订单") @PostMapping("/add") - public CommonResult add(@RequestBody @Valid CourseOrder courseOrder) { + public CommonResult add(@RequestBody @Valid CourseOrderVO courseOrderVO) { //TODO:建立VO + CourseOrder courseOrder=new CourseOrder(courseOrderVO.getId(), courseOrderVO.getSerialNumber(), courseOrderVO.getPrice(), courseOrderVO.getAttr()); boolean success = courseOrderService.save(courseOrder); if (success) { return CommonResult.success(courseOrder); @@ -36,6 +45,30 @@ public class CourseOrderController { } } + /** + * 支付 + */ + + @GetMapping("/topay") + @ResponseBody + public String toPay(@RequestBody @Valid CourseOrderVO courseOrderVO) throws Exception { + String subject=courseOrderVO.getCoursename(); + String serialNumber=courseOrderVO.getSerialNumber(); + BigDecimal money=courseOrderVO.getPrice(); + String form = alipayService.toPay(subject, serialNumber, money); + return form; + } + + /** + * 根据订单号查询支付结果 + */ + + @GetMapping("/query") + @ResponseBody + public Object queryTradeStatus(@NotNull String serialNumber) throws Exception { + Object result = alipayService.queryTradeStatus(serialNumber); + return result; + } } diff --git a/src/main/java/com/online/edu/entity/order/CourseElection.java b/src/main/java/com/online/edu/entity/order/CourseElection.java index 22f6d6f..a616b57 100644 --- a/src/main/java/com/online/edu/entity/order/CourseElection.java +++ b/src/main/java/com/online/edu/entity/order/CourseElection.java @@ -64,4 +64,19 @@ public class CourseElection implements Serializable { * 更新时间 */ private Date updatedAt; + + public CourseElection(Long id,String serialNumber,Long courseId,String courseName,String teacherName,Long studentId,String studentName,BigDecimal price,Integer status,Integer isDeleted,Date createdAt,Date updatedAt){ + this.id=id; + this.serialNumber=serialNumber; + this.courseId=courseId; + this.courseName=courseName; + this.teacherName=teacherName; + this.studentId=studentId; + this.studentName=studentName; + this.price=price; + this.status=status; + this.isDeleted=isDeleted; + this.createdAt=createdAt; + this.updatedAt=updatedAt; + } } \ No newline at end of file diff --git a/src/main/java/com/online/edu/entity/order/CourseOrder.java b/src/main/java/com/online/edu/entity/order/CourseOrder.java index feaeb4d..47bd901 100644 --- a/src/main/java/com/online/edu/entity/order/CourseOrder.java +++ b/src/main/java/com/online/edu/entity/order/CourseOrder.java @@ -54,4 +54,16 @@ public class CourseOrder implements Serializable { * 更新时间 */ private Date updatedAt; + public CourseOrder(Long id, String serialNumber, BigDecimal price, String attr){ + this.id=id; + this.serialNumber=serialNumber; + this.payedat=new Date(); + this.price=price; + this.transactionId=""; + this.tradeState="Not_Finished"; + this.attr=attr; + this.isDeleted=0; + this.createdAt=new Date(); + this.updatedAt=new Date(); + } } \ No newline at end of file diff --git a/src/main/java/com/online/edu/entity/order/vo/CourseElectionVO.java b/src/main/java/com/online/edu/entity/order/vo/CourseElectionVO.java new file mode 100644 index 0000000..0308369 --- /dev/null +++ b/src/main/java/com/online/edu/entity/order/vo/CourseElectionVO.java @@ -0,0 +1,48 @@ +package com.online.edu.entity.order.vo; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import lombok.Data; + +import java.io.Serializable; +import java.math.BigDecimal; +import java.util.Date; + +/** + * 选课 + * + * @TableName course_election + */ +@Data +public class CourseElectionVO implements Serializable { + private static final long serialVersionUID = 1L; + /** + * + */ + @TableId(value = "id", type = IdType.ASSIGN_ID) + private Long id; + /** + * 课程id + */ + private Long courseId; + /** + * 课程名称 + */ + private String courseName; + /** + * 教师名称 + */ + private String teacherName; + /** + * 学生id + */ + private Long studentId; + /** + * 学生昵称 + */ + private String studentName; + /** + * 订单金额(分) + */ + private BigDecimal price; +} diff --git a/src/main/java/com/online/edu/entity/order/vo/CourseOrderVO.java b/src/main/java/com/online/edu/entity/order/vo/CourseOrderVO.java new file mode 100644 index 0000000..d5981bf --- /dev/null +++ b/src/main/java/com/online/edu/entity/order/vo/CourseOrderVO.java @@ -0,0 +1,29 @@ +package com.online.edu.entity.order.vo; + +import lombok.Data; + +import java.math.BigDecimal; + +@Data +public class CourseOrderVO { + /** + * + */ + private Long id; + /** + * 订单号 + */ + private String serialNumber; + /** + * 支付金额(分) + */ + private BigDecimal price; + /** + * 其他属性 + */ + private String attr; + /** + * 课程名 + */ + private String coursename; +} diff --git a/src/main/java/com/online/edu/mapper/order/CourseElectionMapper.java b/src/main/java/com/online/edu/mapper/order/CourseElectionMapper.java index e576ff2..e74b287 100644 --- a/src/main/java/com/online/edu/mapper/order/CourseElectionMapper.java +++ b/src/main/java/com/online/edu/mapper/order/CourseElectionMapper.java @@ -2,6 +2,10 @@ package com.online.edu.mapper.order; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.online.edu.entity.order.CourseElection; +import org.apache.ibatis.annotations.Param; + +import java.util.Date; +import java.util.List; /** * @author Nvnqs @@ -10,6 +14,13 @@ import com.online.edu.entity.order.CourseElection; * @Entity com.online.edu.entity.order.CourseElection */ public interface CourseElectionMapper extends BaseMapper { + int insertAll(CourseElection courseElection); + + List getById(@Param("studentId") Long studentId); + + List getByCourse(@Param("courseId") Long courseId); + + int updateById(@Param("serialNumber") String serialNumber, @Param("status") Integer status ,@Param("updateAt") Date updateAt); } diff --git a/src/main/java/com/online/edu/mapper/order/CourseOrderMapper.java b/src/main/java/com/online/edu/mapper/order/CourseOrderMapper.java index 1398bcf..487d2fa 100644 --- a/src/main/java/com/online/edu/mapper/order/CourseOrderMapper.java +++ b/src/main/java/com/online/edu/mapper/order/CourseOrderMapper.java @@ -1,7 +1,11 @@ package com.online.edu.mapper.order; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.online.edu.entity.order.CourseElection; import com.online.edu.entity.order.CourseOrder; +import org.apache.ibatis.annotations.Param; + +import java.util.Date; /** * @author Nvnqs @@ -10,7 +14,9 @@ import com.online.edu.entity.order.CourseOrder; * @Entity com.online.edu.entity.order.CourseOrder */ public interface CourseOrderMapper extends BaseMapper { + int insertAll(CourseOrder courseOrder); + int updateById(@Param("serialNumber") String serialNumber, @Param("tradeState") String tradeState, @Param("updateAt") Date updateAt); } diff --git a/src/main/java/com/online/edu/service/order/CourseElectionService.java b/src/main/java/com/online/edu/service/order/CourseElectionService.java index 7de1108..09d37ad 100644 --- a/src/main/java/com/online/edu/service/order/CourseElectionService.java +++ b/src/main/java/com/online/edu/service/order/CourseElectionService.java @@ -3,6 +3,8 @@ package com.online.edu.service.order; import com.baomidou.mybatisplus.extension.service.IService; import com.online.edu.entity.order.CourseElection; +import java.util.List; + /** * @author Nvnqs * @description 针对表【course_election(选课)】的数据库操作Service @@ -10,4 +12,9 @@ import com.online.edu.entity.order.CourseElection; */ public interface CourseElectionService extends IService { + boolean save(CourseElection courseElection); + + List getById(Long studentid); + + List getByCourse(Long courseid); } diff --git a/src/main/java/com/online/edu/service/order/CourseOrderService.java b/src/main/java/com/online/edu/service/order/CourseOrderService.java index 68d18d0..745e723 100644 --- a/src/main/java/com/online/edu/service/order/CourseOrderService.java +++ b/src/main/java/com/online/edu/service/order/CourseOrderService.java @@ -1,6 +1,7 @@ package com.online.edu.service.order; import com.baomidou.mybatisplus.extension.service.IService; +import com.online.edu.entity.order.CourseElection; import com.online.edu.entity.order.CourseOrder; /** @@ -9,5 +10,5 @@ import com.online.edu.entity.order.CourseOrder; * @createDate 2023-06-04 17:20:56 */ public interface CourseOrderService extends IService { - + boolean save(CourseOrder courseOrder); } diff --git a/src/main/java/com/online/edu/service/order/impl/AlipayService.java b/src/main/java/com/online/edu/service/order/impl/AlipayService.java new file mode 100644 index 0000000..6770a11 --- /dev/null +++ b/src/main/java/com/online/edu/service/order/impl/AlipayService.java @@ -0,0 +1,81 @@ +package com.online.edu.service.order.impl; + + +import com.alibaba.fastjson2.JSON; +import com.alipay.easysdk.factory.Factory; +import com.alipay.easysdk.kernel.util.ResponseChecker; +import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse; +import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse; +import com.online.edu.common.CommonResult; +import com.online.edu.entity.order.CourseElection; +import com.online.edu.entity.order.CourseOrder; +import com.online.edu.mapper.order.CourseElectionMapper; +import com.online.edu.mapper.order.CourseOrderMapper; +import com.online.edu.utils.JSONUtils; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.util.*; + +/** + * 支付服务 + */ + +@Service +public class AlipayService { + @Autowired + CourseOrderMapper courseOrderMapper; + + @Autowired + CourseElectionMapper courseElectionMapper; + + /** + * 生成支付表单 + * @param subject + * @param money + * @return + * @throws Exception + */ + public String toPay(String subject,String serialNumber, BigDecimal money) throws Exception { + AlipayTradePagePayResponse pay = Factory.Payment.Page().pay(subject, serialNumber, + String.valueOf(money), "http://localhost:28080"); + String payForm = null; + if (ResponseChecker.success(pay)) { + payForm = pay.getBody(); + } + return payForm; + } + + + /** + * 查询交易状态 + * @param outTradeNo 订单号 out_trade_no + * @return + * @throws Exception + */ + + public Object queryTradeStatus(String outTradeNo) throws Exception { + AlipayTradeQueryResponse query = Factory.Payment.Common().query(outTradeNo); + Map map = JSONUtils.jsonToMap(query.getHttpBody()); + Map resultMap = JSON.parseObject(map.get("alipay_trade_query_response").toString()); + Object trade_status = resultMap.get("trade_status"); + if ("TRADE_SUCCESS".equals(trade_status) || "TRADE_FINISHED".equals(trade_status)) { + courseOrderMapper.updateById(outTradeNo,"Trade_Finished",new Date()); + courseElectionMapper.updateById(outTradeNo, 1,new Date()); + } else { + return CommonResult.error(400,"支付失败"); + } + // 返回交易结果, 是否交易成功需要根据该对象中的 trade_status 来确定 + // trade_status 的枚举值如下, 请见 https://opendocs.alipay.com/apis/api_1/alipay.trade.query + // WAIT_BUYER_PAY(交易创建,等待买家付款) + // TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款) + // TRADE_SUCCESS(交易支付成功) + // TRADE_FINISHED(交易结束,不可退款) + // 当 trade_status 等于 TRADE_SUCCESS 或 TRADE_FINISHED 时, 表示支付成功 + return map.get("alipay_trade_query_response"); + } + +} diff --git a/src/main/java/com/online/edu/service/order/impl/CourseElectionServiceImpl.java b/src/main/java/com/online/edu/service/order/impl/CourseElectionServiceImpl.java index d9c4dbc..e245c63 100644 --- a/src/main/java/com/online/edu/service/order/impl/CourseElectionServiceImpl.java +++ b/src/main/java/com/online/edu/service/order/impl/CourseElectionServiceImpl.java @@ -4,8 +4,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.online.edu.entity.order.CourseElection; import com.online.edu.mapper.order.CourseElectionMapper; import com.online.edu.service.order.CourseElectionService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; + /** * @author Nvnqs * @description 针对表【course_election(选课)】的数据库操作Service实现 @@ -14,6 +17,21 @@ import org.springframework.stereotype.Service; @Service public class CourseElectionServiceImpl extends ServiceImpl implements CourseElectionService { + @Autowired + CourseElectionMapper courseElectionMapper; + + @Override + public boolean save(CourseElection courseElection){ + int i=courseElectionMapper.insertAll(courseElection); + System.out.println(i); + return i != 0; + } + + @Override + public List getById(Long studentid){ return courseElectionMapper.getById(studentid);} + + @Override + public List getByCourse(Long courseid){ return courseElectionMapper.getById(courseid);} } diff --git a/src/main/java/com/online/edu/service/order/impl/CourseOrderServiceImpl.java b/src/main/java/com/online/edu/service/order/impl/CourseOrderServiceImpl.java index ceaa2aa..932eed9 100644 --- a/src/main/java/com/online/edu/service/order/impl/CourseOrderServiceImpl.java +++ b/src/main/java/com/online/edu/service/order/impl/CourseOrderServiceImpl.java @@ -1,9 +1,12 @@ package com.online.edu.service.order.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.online.edu.entity.order.CourseElection; import com.online.edu.entity.order.CourseOrder; +import com.online.edu.mapper.order.CourseElectionMapper; import com.online.edu.mapper.order.CourseOrderMapper; import com.online.edu.service.order.CourseOrderService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** @@ -14,7 +17,13 @@ import org.springframework.stereotype.Service; @Service public class CourseOrderServiceImpl extends ServiceImpl implements CourseOrderService { + @Autowired + CourseOrderMapper courseOrderMapper; + @Override + public boolean save(CourseOrder courseOrder){ + return courseOrderMapper.insertAll(courseOrder) != 0; + } } diff --git a/src/main/java/com/online/edu/utils/JSONUtils.java b/src/main/java/com/online/edu/utils/JSONUtils.java new file mode 100644 index 0000000..3652ef1 --- /dev/null +++ b/src/main/java/com/online/edu/utils/JSONUtils.java @@ -0,0 +1,16 @@ +package com.online.edu.utils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.util.Map; + +public class JSONUtils { + + private static final ObjectMapper mapper = new ObjectMapper(); + + public static Map jsonToMap(String jsonStr) throws JsonProcessingException { + Map map = mapper.readValue(jsonStr, Map.class); + return map; + } +} diff --git a/src/main/resources/mapper/CourseElectionMapper.xml b/src/main/resources/mapper/CourseElectionMapper.xml index c0b4756..4db8c3b 100644 --- a/src/main/resources/mapper/CourseElectionMapper.xml +++ b/src/main/resources/mapper/CourseElectionMapper.xml @@ -19,5 +19,27 @@ + + insert into online_edu.course_election + (id, serial_number, course_id, course_name, teacher_name, student_id, student_name, price, status, is_deleted, created_at, updated_at) + values (#{id}, #{serialNumber}, #{courseId}, #{courseName}, #{teacherName}, #{studentId}, #{studentName}, #{status}, #{price}, #{isDeleted}, #{createdAt}, #{updatedAt}) + + + + + + + update online_edu.course_election + set trade_state = #{status, jdbcType=TINYINT}, updated_at = #{updatedAt, jdbcType=TIMESTAMP} + where serialNumber=#{serialNumber} + diff --git a/src/main/resources/mapper/CourseOrderMapper.xml b/src/main/resources/mapper/CourseOrderMapper.xml index 21dbadd..ce52da7 100644 --- a/src/main/resources/mapper/CourseOrderMapper.xml +++ b/src/main/resources/mapper/CourseOrderMapper.xml @@ -17,5 +17,17 @@ + + insert into online_edu.course_order + (id, serial_number, payedAt, price, transaction_id, trade_state, attr, is_deleted, created_at, updated_at) + values + (#{id}, #{serialNumber}, #{payedat}, #{price}, #{transactionId,jdbcType=VARCHAR}, #{tradeState}, #{attr}, #{isDeleted}, #{createdAt}, #{updatedAt}) + + + + update online_edu.course_order + set trade_state = #{tradeState, jdbcType=CHAR}, updated_at = #{updatedAt, jdbcType=TIMESTAMP} + where serialNumber=#{serialNumber} + -- Gitee From 95498d71674709bf0a4481ea982d40f87182219a Mon Sep 17 00:00:00 2001 From: TTX Date: Mon, 19 Jun 2023 17:13:36 +0800 Subject: [PATCH 2/2] ttx --- src/main/java/com/online/edu/config/AlipayConfig.java | 2 +- .../online/edu/controller/order/CourseOrderController.java | 4 ++-- .../java/com/online/edu/service/order/impl/AlipayService.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/online/edu/config/AlipayConfig.java b/src/main/java/com/online/edu/config/AlipayConfig.java index f5f1c57..41537a4 100644 --- a/src/main/java/com/online/edu/config/AlipayConfig.java +++ b/src/main/java/com/online/edu/config/AlipayConfig.java @@ -16,7 +16,7 @@ public class AlipayConfig { config.protocol = "https"; // 沙箱环境修改为 openapi.alipaydev.com - config.gatewayHost = "openapi-sandbox.dl.alipaydev.com/gateway.do"; + config.gatewayHost = "openapi-sandbox.dl.alipaydev.com"; config.signType = "RSA2"; diff --git a/src/main/java/com/online/edu/controller/order/CourseOrderController.java b/src/main/java/com/online/edu/controller/order/CourseOrderController.java index b613c5c..00c461e 100644 --- a/src/main/java/com/online/edu/controller/order/CourseOrderController.java +++ b/src/main/java/com/online/edu/controller/order/CourseOrderController.java @@ -51,12 +51,12 @@ public class CourseOrderController { @GetMapping("/topay") @ResponseBody - public String toPay(@RequestBody @Valid CourseOrderVO courseOrderVO) throws Exception { + public CommonResult toPay(@RequestBody @Valid CourseOrderVO courseOrderVO) throws Exception { String subject=courseOrderVO.getCoursename(); String serialNumber=courseOrderVO.getSerialNumber(); BigDecimal money=courseOrderVO.getPrice(); String form = alipayService.toPay(subject, serialNumber, money); - return form; + return CommonResult.success(form); } /** diff --git a/src/main/java/com/online/edu/service/order/impl/AlipayService.java b/src/main/java/com/online/edu/service/order/impl/AlipayService.java index 6770a11..5a4c2e7 100644 --- a/src/main/java/com/online/edu/service/order/impl/AlipayService.java +++ b/src/main/java/com/online/edu/service/order/impl/AlipayService.java @@ -41,7 +41,7 @@ public class AlipayService { */ public String toPay(String subject,String serialNumber, BigDecimal money) throws Exception { AlipayTradePagePayResponse pay = Factory.Payment.Page().pay(subject, serialNumber, - String.valueOf(money), "http://localhost:28080"); + String.valueOf(money), "http://localhost:8080"); String payForm = null; if (ResponseChecker.success(pay)) { payForm = pay.getBody(); -- Gitee