package com.huiju.estateagents.center.taUser.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.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaTags;
import com.huiju.estateagents.center.taUser.service.ITaTagsService;
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 org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
    *   前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-09-24
 */
@RestController
@RequestMapping("/api")
public class TaTagsController extends BaseController {

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

    @Autowired
    public ITaTagsService iTaTagsService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/taTags",method= RequestMethod.GET)
    public ResponseBean taTagsList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                   @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaTags> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaTags> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id",getOrgId(request));
            IPage<TaTags> result = iTaTagsService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taTagsList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param taTags 实体对象
     * @return
     */
    @RequestMapping(value="/taTags",method= RequestMethod.POST)
    public ResponseBean taTagsAdd(@RequestBody TaTags taTags){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaTagsService.save(taTags)){
                responseBean.addSuccess(taTags);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taTagsAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ResponseBody
    @RequestMapping(value="/taTags/{id}", method= RequestMethod.DELETE)
    public ResponseBean taTagsDelete(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iTaTagsService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taTagsDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param taTags 实体对象
     * @return
     */
    @RequestMapping(value="/taTags/{id}",method= RequestMethod.PUT)
    public ResponseBean taTagsUpdate(@PathVariable Integer id,
                                        @RequestBody TaTags taTags){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaTagsService.updateById(taTags)){
                responseBean.addSuccess(taTags);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taTagsUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/taTags/{id}",method= RequestMethod.GET)
    public ResponseBean taTagsGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaTagsService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taTagsDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
