发表于: 2017-12-20 21:42:54

1 569


一.今天完成的主要事情

1.编写新增提现订单接口

 /**

     * 4. 新增提现订单接口

     *

     * @param

     * @return

     * @throws ServiceException

     * @throws ServiceDaoException

     */

    @RequestMapping(value = "/a/u/withdrawal", method = RequestMethod.POST)

    public String addWithdrawOrder(HttpServletRequest request, HttpServletResponse response, ModelMap model,

                                   @RequestParam("idList") List<Long> idList,

                                   Long schoolId, String schoolName, BigDecimal withdrawAmount) throws Exception {

        //打印参数

        log.info("Inteface POST /a/u/withdrawal, Method addWithdrawOrder() parameters: schoolId is : " + schoolId +

                ", schoolName: " + schoolName + ", withdrawAmount: " + withdrawAmount + ", IdList: " + idList);

        //校验参数

        if (DataUtils.isNullOrEmpty(schoolId)) {

            log.info("parameter schoolId can not be null, schoolId: " + schoolId);

            model.addAttribute("code", -1000);

            return "/common/failure";

        }

        if (DataUtils.isNullOrEmpty(schoolName)) {

            log.info("parameter schoolName can not be null, schoolName: " + schoolName);

            model.addAttribute("code", -1000);

            return "/common/failure";

        }

        if (DataUtils.isNullOrEmpty(withdrawAmount)) {

            log.info("parameter withdrawAmount can not be null, withdrawAmount: " + withdrawAmount);

            model.addAttribute("code", -1000);

            return "/common/failure";

        }

        if (CollectionUtils.isEmpty(idList)) {

            log.info("parameter IdList can not be null or empty, IdList: " + idList);

            model.addAttribute("code", -1000);

            return "/common/failure";

        }

        //实现逻辑

        //TODO 该部分功能写好后打开注释

        /*String managerIdStr = CookieUtil.getKeyIdentity(request, CookieUtil.UserID);

        Long managerId = Long.parseLong(managerIdStr);

        */

        //1.先生成Withdraw对象

        Withdraw withdraw = new Withdraw();

        withdraw.setSchoolId(schoolId);

        withdraw.setSchoolName(schoolName);

        withdraw.setWithdrawAmount(withdrawAmount);

        withdraw.setStatus(1);

        withdraw.setCreateBy(-1L);

        withdraw.setUpdateBy(-1L);

        /*withdraw.setCreateBy(managerId);

        withdraw.setUpdateBy(managerId);*/

        try {

            //2.将该对象插入的withdrw表中,获取该条记录的withdrawId

            Long withdrawId = withdrawService.insert(withdraw);

            log.info("insert withdraw success, withdraw Id is : " + withdrawId);

            //3.根据传入进来的IdList,查enrollment_detail表,获取enrollmentDetail对象List.

            List<EnrollmentDetail> enrollmentDetailList = new ArrayList<>();

            enrollmentDetailList = enrollmentDetailService.getObjectsByIds(idList);

            if (CollectionUtils.isEmpty(enrollmentDetailList)) {

                log.info("Can not query data by idList, idList is : " + idList);

                model.addAttribute("code", -1001);

                return "/common/failure";

            }

            log.info("get enrollmentDetail list by idList, list size is : " + enrollmentDetailList.size());

            //4.将该List中的每个元素的withdrawId的值改为withdrawId

            for (int i = 0; i < enrollmentDetailList.size(); i++) {

                enrollmentDetailList.get(i).setWithdrawId(withdrawId);

               /* enrollmentDetailList.get(i).setUpdateBy(managerId);*/

            }

            //5.批量更新enrollment_detail表

            enrollmentDetailService.updateList(enrollmentDetailList);

            log.info("update enrollmentDetailList success, list size is " + enrollmentDetailList.size());

            model.addAttribute("code", 0);

            model.addAttribute("id", withdrawId);

            return "/common/insert";

        } catch (Throwable t) {

            log.error(t.getMessage());

            log.error("Method addWithdrawOrder() failed, schoolId: " + schoolId + ", schoolName: " +

                    schoolName + ", withdrawAmount: " + withdrawAmount + ", idList: " + idList);

            model.addAttribute("code", -1);

            return "/common/failure";

        }

    }

实现过程还是和之前的一样,先打印传入的参数,然后校验传入的参数,接下来是实现业务逻辑,期间要注意打日志

打日志的时候要注意几点:

一是日志的内容要有意义,要能通过日志知道整个程序大概做了哪些事情

二是日志中一定要打印关键参数,方便日后出现问题的时候调试

三是我们一般是先从service中获取记录Id,然后通过Id再获取到数据,我们通过条件查Id的时候获取的IdList很可能是空的,所以应该先打印获取到的idList,然后通过idList获取对象list的时候,对象List就不需要打印List中的对象的具体值,而是打印长度,看是否和idList的长度一致即可

2.编写处理订单的接口

/**

     * 9.处理提现订单接口

     *

     * @param

     * @return

     * @throws ServiceException

     * @throws ServiceDaoException

     */

    @RequestMapping(value = "/a/u/withdrawal/{id}", method = RequestMethod.PUT)

    public String dealWithdrawOrder(HttpServletRequest request, HttpServletResponse response, ModelMap model,

                                    @PathVariable Long id, @RequestParam List<Long> idList, Integer operate) throws Exception {

        log.info("Interface PUT /a/u/withdrawal/{id}, Method dealWithdrawOrder() parameter, id: " + id +

                ", operate: " + operate + ", idList:" + idList);

        if (CollectionUtils.isEmpty(idList)) {

            log.info("parameter idList can not be null or empty, idList is : " + idList);

            model.addAttribute("code", -1000);

            return "/common/failure";

        }

        if (DataUtils.isNullOrEmpty(operate)) {

            log.info("parameter operate can not be null, operate is : " + operate);

            model.addAttribute("code", -1000);

            return "/common/failure";

        }

        if (operate < 0 || operate > 2) {

            log.info("Error valure of paremeter operate, operate is : " + operate);

            model.addAttribute("code", -1001);

            return "/common/failure";

        }

        //TODO 该部分功能写好后打开注释

        /*String managerIdStr = CookieUtil.getKeyIdentity(request, CookieUtil.UserID);

        Long managerId = Long.parseLong(managerIdStr);

        */

        try {

            //根据Id获取withdraw对象

            Withdraw withdrawal = withdrawService.getObjectById(id);

            log.info("Get withdraw object by id, id is : " + id);

            if (DataUtils.isNullOrEmpty(withdrawal)) {

                log.info("Can not find data by withdraw id, withdraw id is : " + id);

                model.addAttribute("code", -1001);

                return "/common/failure";

            }

            //根据idList获取enrollmentDetail对象List

            List<EnrollmentDetail> enrollmentDetailList = new ArrayList<>();

            enrollmentDetailList = enrollmentDetailService.getObjectsByIds(idList);

            if (CollectionUtils.isEmpty(enrollmentDetailList)) {

                log.info("Can not query data by idList, idList is : " + idList);

                model.addAttribute("code", -1001);

                return "/common/failure";

            }

            log.info("get enrollmentDetail list by idList, list size is : " + enrollmentDetailList.size());

            //更新订单状态

            withdrawal.setStatus(0);

            /*withdrawal.setUpdateBy(managerId);*/

            withdrawService.update(withdrawal);

            log.info("update withdraw's status success, status is " + withdrawal.getStatus());

            //新增一条结算记录

            Settlement settlement = new Settlement();

            settlement.setSchoolId(withdrawal.getSchoolId());

            settlement.setSchoolName(withdrawal.getSchoolName());

            settlement.setSettlementAmount(withdrawal.getWithdrawAmount());

            settlement.setSettlementAmount(withdrawal.getWithdrawAmount());

            settlement.setStatus(operate);

            settlement.setCreateBy(-1L);

            settlement.setUpdateBy(-1L);

           /* settlement.setCreateBy(managerId);

            settlement.setUpdateBy(managerId);*/

            Long settlementId = settlementService.insert(settlement);

            log.info("insert settlement success, settlement id is : " + settlementId);

            //如果提现通过,则增加一条支出记录

            Long recordId = 0L;

            if (operate == 0) {

                Record record = new Record();

                record.setSchoolId(withdrawal.getSchoolId());

                record.setSchoolName(withdrawal.getSchoolName());

                record.setAmount(withdrawal.getWithdrawAmount());

                record.setStatus(1);

                record.setCreateBy(-1L);

                record.setUpdateBy(-1L);

               /* record.setUpdateBy(managerId);

                record.setCreateBy(managerId);*/

                recordId = recordService.insert(record);

                log.info("insert record success, record id is : " + recordId);

            }

            //将enrollmentDetailList中的每个元素的settlementId的值改为

            for (int i = 0; i < enrollmentDetailList.size(); i++) {

                enrollmentDetailList.get(i).setSettlementId(settlementId);

                if (operate == 0) {

                    enrollmentDetailList.get(i).setExpensesId(recordId);

                }

                /*enrollmentDetailList.get(i).setUpdateBy(managerId);*/

            }

            //批量更新enrollment_detail表

            enrollmentDetailService.updateList(enrollmentDetailList);

            log.info("update enrollmentDetailList success, list size is " + enrollmentDetailList.size());

            model.addAttribute("code", 0);

            return "/common/success";

        } catch (Throwable t) {

            log.error(t.getMessage());

            log.error("Method dealWithdrawOrder() failed, parameter id is : " + id);

            model.addAttribute("code", -1);

            return "/common/failure";

        }

    }

同样,这两个接口还没有进行重构,代码非常冗余,等待所有的接口都写完之后,确保功能没有问题之后就进行重构,争取最大限度的减少一个方法中的代码量.


二.明天计划完成的事情

1.继续按照计划完成接口的编写


三.遇到的问题

暂无


四.收获

对接口中日志怎么打有更加深入的理解


五.项目进度情况

目前正常,暂无延期风险


返回列表 返回列表
评论

    分享到