首页>代码>基于SSM框架的动物救助中心管理信息系统>/adopt/src/main/java/com/ecjtu/controller/AdoptAnimalController.java
package com.ecjtu.controller;

import com.ecjtu.entity.AdoptAnimal;
import com.ecjtu.entity.Pet;
import com.ecjtu.entity.Users;
import com.ecjtu.service.AdoptAnimalService;
import com.ecjtu.service.PetService;
import com.ecjtu.service.UsersService;
import com.ecjtu.util.MailUtil;
import com.ecjtu.util.Message;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.soap.SOAPBinding;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 */
@Controller
@RequestMapping("adopt")
public class AdoptAnimalController {

    @Autowired
    private AdoptAnimalService animalService;

    @Autowired
    private PetService petService;

    @Autowired
    private UsersService usersService;

    @RequestMapping("adopts.action")
    @ResponseBody
    public Message getAdoptAnimals(@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        PageHelper.startPage(pn,4);
        AdoptAnimal animal=new AdoptAnimal();
        List<AdoptAnimal> adoptAnimals = animalService.findByState(1);
        PageInfo page=new PageInfo(adoptAnimals,2);
        return Message.success().add("pageInfo",page);
    }


    @RequestMapping("ByAgree.action")
    @ResponseBody
    public Message getAdoptStates(@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        PageHelper.startPage(pn,4);
        int state=2;
        List<AdoptAnimal> adoptAnimals = animalService.findByState(state);
        PageInfo page=new PageInfo(adoptAnimals,2);
        return Message.success().add("pageInfo",page);
    }

    @RequestMapping("ByDisAgree.action")
    @ResponseBody
    public Message getAdoptDisagress(@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        PageHelper.startPage(pn,4);
        int state=0;
        List<AdoptAnimal> adoptAnimals = animalService.findByState(state);
        PageInfo page=new PageInfo(adoptAnimals,2);
        return Message.success().add("pageInfo",page);
    }

    @RequestMapping("create.action")
    @ResponseBody
    public Message createAdopt(HttpServletRequest request){
        Pet pet = (Pet)request.getSession().getAttribute("pet");
        Users user = (Users)request.getSession().getAttribute("user");
        pet.setState(1);
        AdoptAnimal animal=new AdoptAnimal();
        animal.setUser(user);
        animal.setPet(pet);
        animal.setState(1);
        animal.setAdoptTime(new Date());
        int i = animalService.addAdoptAnimal(animal);
        int t=petService.updateState(pet);
        if(i>0&&t>0){
            return Message.success();
        }else{
            return Message.fail();
        }
    }

    @RequestMapping("delete.action")
    @ResponseBody
    public Message deleteAdopt(Integer id){
        if(animalService.deleteAdoptAnimal(id)>0){
            return Message.success();
        }else{
            return Message.fail();
        }
    }

    @RequestMapping("update.action")
    @ResponseBody
    public Message updateAdoptAnimal(AdoptAnimal animal){
        if(animalService.updateAdoptAnimal(animal)>0){
            return Message.success();
        }else{
            return Message.fail();
        }
    }

    @RequestMapping(value = "disAgree.action", method = RequestMethod.GET)
    @ResponseBody
    public Message updateAdoptState(Integer id) throws MessagingException {
        AdoptAnimal animal = animalService.findById(id);
        animal.setState(0);
        Pet pet = animal.getPet();
        pet.setState(0);
        int i = animalService.updateAdoptState(animal);
        int t=petService.updateState(pet);
        if(i>0&&t>0){
            return Message.success();
        }else{
            return Message.fail();
        }
    }

    @RequestMapping(value = "agree.action",method = RequestMethod.GET)
    @ResponseBody
    public Message updateAdoptStates(Integer id) throws MessagingException {
        AdoptAnimal animal = animalService.findById(id);
        animal.setState(2);
        Pet pet = animal.getPet();
        pet.setState(2);
        int a = animalService.updateAdoptState(animal);
        int b=petService.updateState(pet);
        if(a>0&&b>0){
            return Message.success();
        }else{
            return Message.fail();
        }
    }

    @RequestMapping("findById.action")
    @ResponseBody
    public Message findById(Integer id){
        AdoptAnimal animal = animalService.findById(id);
        if(animal!=null){
            return Message.success().add("animal",animal);
        }else{
            return Message.fail();
        }
    }


    @RequestMapping("findByAdoptTime.action")
    @ResponseBody
    public Message findByAdoptTime(String adoptTime) throws ParseException {
        PageHelper.startPage(1,4);
        List<AdoptAnimal> adoptAnimals = animalService.findByAdoptTime(adoptTime);
        if(adoptAnimals!=null){
            PageInfo page=new PageInfo(adoptAnimals,3);
            return Message.success().add("pageInfo",page);
        }else{
            return Message.fail();
        }

    }

    @RequestMapping("findByName.action")
    @ResponseBody
    public Message findByName(@RequestParam(value = "pn",defaultValue = "1")Integer pn,@RequestParam(value = "userName") String userName,@RequestParam(value = "state") Integer state) throws ParseException {
        PageHelper.startPage(1,4);
        List<Users> users = usersService.findByName(userName);
        List<AdoptAnimal> adoptAnimals = animalService.findByName(users,state);
        if(adoptAnimals!=null){
            PageInfo page=new PageInfo(adoptAnimals,3);
            return Message.success().add("pageInfo",page);
        }else{
            return Message.fail();
        }

    }
}
最近下载更多
860421 7小时前
暂无贡献等级
zxc123zdq  LV14 4月15日
嘛花藤  LV5 4月3日
婴儿早教打猫猫菇  LV2 2月26日
FF加菲猫  LV4 2月11日
qq2901732871  LV9 1月5日
xiaomii  LV3 2023年11月13日
被殇过的内心  LV2 2023年10月20日
txf123  LV4 2023年9月21日
芽哇哇棉花堂  LV2 2023年6月27日
最近浏览更多
860421 7小时前
暂无贡献等级
yimrrrrr 昨天
暂无贡献等级
zxc123zdq  LV14 4月15日
Charismatic 4月14日
暂无贡献等级
Luckyxiaoyi  LV1 4月14日
嘛花藤  LV5 4月3日
WBelong  LV7 3月29日
escape1023 3月22日
暂无贡献等级
2994754858 3月21日
暂无贡献等级
wanyou 3月7日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友