package com.njyunzhi.pet_identity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njyunzhi.pet_identity.common.BaseController;
import com.njyunzhi.pet_identity.common.Constants;
import com.njyunzhi.pet_identity.common.ResponseBean;
import com.njyunzhi.pet_identity.entity.TaPerson;
import com.njyunzhi.pet_identity.entity.TaPetIdentity;
import com.njyunzhi.pet_identity.service.ITaPetIdentityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.njyunzhi.pet_identity.service.ITaPetService;
import com.njyunzhi.pet_identity.entity.TaPet;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
    * 宠物表 前端控制器
    * </p>
 *
 * @author yansen
 * @since 2022-05-25
 */

@Api(tags = "宠物表")
@RestController
@RequestMapping("/")
public class TaPetController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaPetController.class);

    @Autowired
    public ITaPetService iTaPetService;

    @Autowired
    public ITaPetIdentityService iTaPetIdentityService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/wx/{clientId}/pet",method= RequestMethod.GET)
    @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean taPetList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
									 @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception {
        TaPerson taPerson = currentPerson();

        IPage<TaPet> pg = new Page<>(pageNum, pageSize);
        QueryWrapper<TaPet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("person_id", taPerson.getPersonId());
        queryWrapper.gt("status", Constants.STATUS_DELETE);
        queryWrapper.orderByDesc("create_date");

        IPage<TaPet> result = iTaPetService.page(pg, queryWrapper);
        return ResponseBean.success(result);
    }

    /**
     * 保存对象
     * @param taPet 实体对象
     * @return
     */
    @RequestMapping(value="/wx/{clientId}/pet",method= RequestMethod.POST)
    @ApiOperation(value="保存", notes = "保存", httpMethod = "POST", response = ResponseBean.class)
    public ResponseBean taPetAdd(@ApiParam("保存内容") @RequestBody TaPet taPet) throws Exception {
        TaPerson taPerson = currentPerson();
        taPet.setPersonId(taPerson.getPersonId());
        taPet.setStatus(Constants.STATUS_NORMAL);
        taPet.setCreateDate(LocalDateTime.now());

        if (iTaPetService.save(taPet)){
            return ResponseBean.success(taPet);
        }else {
            return ResponseBean.error("保存失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

//    /**
//     * 根据id删除对象
//     * @param id  实体ID
//     */
//    @RequestMapping(value="/pet/{id}", method= RequestMethod.DELETE)
//    @ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
//    public ResponseBean taPetDelete(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
//        if(iTaPetService.removeById(id)){
//            return ResponseBean.success("success");
//        }else {
//            return ResponseBean.error("删除失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
//        }
//    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param taPet 实体对象
     * @return
     */
    @RequestMapping(value="/wx/{clientId}/pet/{id}",method= RequestMethod.PUT)
    @ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
    public ResponseBean taPetUpdate(@ApiParam("对象ID") @PathVariable String id,
                                        @ApiParam("更新内容") @RequestBody TaPet taPet) throws Exception{
        TaPerson taPerson = currentPerson();
        taPet.setPetId(id);
        taPet.setPersonId(taPerson.getPersonId());

        if (iTaPetService.updateById(taPet)){
            return ResponseBean.success(taPet);
        }else {
            return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/wx/{clientId}/pet/{id}",method= RequestMethod.GET)
    @ApiOperation(value="详情", notes = "详情", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean taPetGet(@ApiParam("对象ID") @PathVariable String id) throws Exception{
        return ResponseBean.success(iTaPetService.getById(id));
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/wx/{clientId}/pet/{id}/card",method= RequestMethod.GET)
    @ApiOperation(value="狗子证件", notes = "狗子证件", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean getCard(@ApiParam("对象ID") @PathVariable String id) throws Exception{

        TaPetIdentity taPetIdentity = iTaPetIdentityService.getExistBy("pet_id", id, false, true);

        return ResponseBean.success(taPetIdentity);
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/admin/pet/{id}/card",method= RequestMethod.GET)
    @ApiOperation(value="狗子证件", notes = "狗子证件", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean getPetCard(@ApiParam("对象ID") @PathVariable String id) throws Exception{

        TaPetIdentity taPetIdentity = iTaPetIdentityService.getExistBy("pet_id", id, false, true);

        return ResponseBean.success(taPetIdentity);
    }
}
