Java tutorial
/* * Copyright 2015-2020 Fengduo.com All right reserved. This software is the confidential and proprietary information of * Fengduo.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only * in accordance with the terms of the license agreement you entered into with Fengduo.com. */ package com.fengduo.bee.web.controller.product; import java.util.List; import javax.validation.Valid; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.json.MappingJackson2JsonView; import com.fengduo.bee.commons.core.lang.Argument; import com.fengduo.bee.commons.persistence.Parameter; import com.fengduo.bee.commons.util.NumberParser; import com.fengduo.bee.commons.util.ObjectUtils; import com.fengduo.bee.model.cons.ProgressEnum; import com.fengduo.bee.model.cons.VerifyStatusEnum; import com.fengduo.bee.model.entity.Item; import com.fengduo.bee.model.entity.ItemFinance; import com.fengduo.bee.model.entity.ItemMember; import com.fengduo.bee.web.controller.BaseController; /** * ??(??,???,?) * * @author zxc May 28, 2015 11:54:13 PM */ @Controller @RequestMapping(value = "/item") public class ItemController extends BaseController { /** * ? * * @return */ @RequestMapping(value = "/create", method = RequestMethod.GET) public ModelAndView create(ModelAndView mav) { mav.setViewName("item/add"); mav.addObject("postUrl", "/item/create"); return mav; } /** * ??? * * @return */ @RequestMapping(value = "/create", method = RequestMethod.POST) public String create(@Valid Item item, BindingResult result, Model model) { model.addAttribute("item", item); // ? if (result.hasErrors()) { model.addAttribute("errMsg", showFirstErrors(result)); return "item/add"; } ObjectUtils.trim(item); // ??? Long currentUserId = getCurrentUserId(); if (Argument.isNotPositive(currentUserId)) { model.addAttribute("errMsg", ",!"); return "account/login"; } // ?,? item.setUserId(currentUserId); item.setVerifyStatus(VerifyStatusEnum.UN_COMPLETELY.getValue()); item.setProgress(ProgressEnum.UNSTART.getValue()); // ???? Parameter query = Parameter.newParameter()// .pu("userId", currentUserId)// .pu("name", item.getName()); Item findItem = itemService.findItem(query); if (findItem != null) { model.addAttribute("errMsg", "?,?!"); return "redirect:/item/" + findItem.getId() + "/edit"; } // ?? itemService.add(item); // ??? return "redirect:/item/finance/" + item.getId(); } /** * ?? * * @return */ @RequestMapping(value = "/finance/{id}", method = RequestMethod.GET) public ModelAndView finance(@PathVariable("id") Long id) { ModelAndView mav = new ModelAndView("item/finance"); // ??ID if (Argument.isNotPositive(id)) { mav.addObject("errMsg", "??,?ID!"); mav.setViewName("error/404"); return mav; } Item findItem = itemService.getItemById(id); // ??? if (findItem == null || !NumberParser.isEqual(getCurrentUserId(), findItem.getUserId())) { mav.addObject("errMsg", "?id?,!"); mav.setViewName("error/404"); return mav; } mav.addObject("itemId", id); mav.addObject("postUrl", "/item/finance"); return mav; } @RequestMapping(value = "/finance", method = RequestMethod.POST) public String finance(@Valid ItemFinance itemFinance, BindingResult result, Long itemId, Model model) { model.addAttribute("itemFinance", itemFinance); ObjectUtils.trim(itemFinance); // ??ID if (Argument.isNotPositive(itemId)) { model.addAttribute("errMsg", "??,?ID!"); return "item/finance"; } // ? if (result.hasErrors()) { model.addAttribute("errMsg", showFirstErrors(result)); return "item/finance"; } // ?? Long currentUserId = getCurrentUserId(); if (Argument.isNotPositive(currentUserId)) { model.addAttribute("errMsg", ",!"); return "item/finance"; } // ????,?? Parameter query = Parameter.newParameter()// .pu("userId", currentUserId)// .pu("id", itemId); Item findItem = itemService.findItem(query); if (findItem == null) { model.addAttribute("errMsg", "?!"); return "item/finance"; } // ??? if (findItem == null || !NumberParser.isEqual(getCurrentUserId(), findItem.getUserId())) { model.addAttribute("errMsg", "?id?,!"); return "error/404"; } // ??ID itemFinance.setItemId(itemId); // ??? itemService.add(itemFinance); // ?? return "redirect:/item/member/" + itemId; } /** * ? * * @return */ @RequestMapping(value = "/member/{id}", method = RequestMethod.GET) public ModelAndView member(@PathVariable("id") Long id) { ModelAndView mav = new ModelAndView("item/member"); // ??ID if (Argument.isNotPositive(id)) { mav.addObject("errMsg", "??,?ID!"); mav.setViewName("error/404"); return mav; } Item findItem = itemService.getItemById(id); if (findItem == null) { mav.addObject("errMsg", "?!"); mav.setViewName("error/404"); return mav; } // ??? if (!NumberParser.isEqual(getCurrentUserId(), findItem.getUserId())) { mav.addObject("errMsg", "?id?,!"); mav.setViewName("error/404"); return mav; } mav.addObject("itemId", id); mav.addObject("postUrl", "/item/success"); return mav; } /** * @param projectId ID * @param teamImg ?? * @param teamName ??? * @param teamTitle ??? * @param teamIntroduce ?? * @param ptId ?ID * @return */ @RequestMapping(value = "/member", method = RequestMethod.POST) public ModelAndView member(Long projectId, String teamImg, String teamName, String teamTitle, String teamIntroduce, Long ptId) { if (Argument.isNotPositive(projectId)) { return createJsonMav(false, 0l, "ID,??!"); } if (StringUtils.isEmpty(teamTitle)) { return createJsonMav(false, 0l, "????!"); } if (StringUtils.isEmpty(teamIntroduce)) { return createJsonMav(false, 0l, "??!"); } if (StringUtils.isEmpty(teamName)) { return createJsonMav(false, 0l, "????!"); } if (StringUtils.isEmpty(teamImg)) { return createJsonMav(false, 0l, "???!"); } // ??? Item findItem = itemService.getItemById(projectId); if (findItem == null) { return createJsonMav(false, 0l, "?!"); } if (!NumberParser.isEqual(getCurrentUserId(), findItem.getUserId())) { return createJsonMav(false, 0l, "?id?,!"); } ItemMember itemMember = new ItemMember(projectId, teamName, teamImg, teamTitle, teamIntroduce); ObjectUtils.trim(itemMember); if (Argument.isNotPositive(ptId)) { itemService.add(itemMember); } else { itemMember.setId(ptId); itemService.update(itemMember); } return createJsonMav(true, itemMember.getId(), "???!"); } @RequestMapping(value = "/member/delete", method = RequestMethod.POST) public ModelAndView member(Long itemId, Long ptId) { Parameter query = Parameter.newParameter()// .pu("itemId", itemId)// .pu("id", ptId); ItemMember findItemMember = itemService.findItemMember(query); if (findItemMember == null) { return createJsonMav(false, 0l, "!"); } // ??? Item findItem = itemService.getItemById(itemId); if (findItem == null) { return createJsonMav(false, 0l, "?!"); } if (!NumberParser.isEqual(getCurrentUserId(), findItem.getUserId())) { return createJsonMav(false, 0l, "?id?,!"); } itemService.deleteItem(ptId); return createJsonMav(true, 1l, "???!"); } /** * ?? * * @return */ @RequestMapping(value = "/success") public ModelAndView success(Long itemId) { ModelAndView mav = new ModelAndView("item/success"); if (itemId == null) { mav.addObject("errMsg", "?id?,??!"); return mav; } Item itemById = itemService.getItemById(itemId); if (itemById == null) { mav.addObject("errMsg", "??,??!"); return mav; } // ??? if (!NumberParser.isEqual(getCurrentUserId(), itemById.getUserId())) { mav.addObject("errMsg", "?id?,!"); mav.setViewName("error/404"); return mav; } ItemFinance itemFinanceByItemId = itemService.getItemFinanceByItemId(itemId); if (itemFinanceByItemId == null) { mav.addObject("errMsg", "???,??!"); return mav; } List<ItemMember> itemMemberList = itemService.getItemMemberByItemId(itemId); if (Argument.isEmpty(itemMemberList)) { mav.addObject("errMsg", "??,??!"); return mav; } // ??,???? itemService.update(new Item(itemId, VerifyStatusEnum.UNAUDITED)); return mav; } // {"isSuccess":true,"code":"2200","info":"????"} private ModelAndView createJsonMav(boolean isSuccess, Long code, String info) { ModelAndView mav = new ModelAndView(); mav.setView(new MappingJackson2JsonView()); mav.addObject("isSuccess", isSuccess); mav.addObject("code", code); mav.addObject("info", info); return mav; } }