package com.huiju.estateagents.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.common.CommConstant;
import com.huiju.estateagents.entity.TaMiniappIcon;
import com.huiju.estateagents.service.ITaMiniappIconService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
    * 小程序图标表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2020-02-03
 */
@RestController
@RequestMapping("/api")
public class TaMiniappIconController extends BaseController {

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

    @Autowired
    public ITaMiniappIconService iTaMiniappIconService;


    /**
     * 查询列表
     * @return
     */
    @RequestMapping(value="/admin/taMiniappIcon",method= RequestMethod.GET)
    public ResponseBean taMiniappIconList(){
        ResponseBean responseBean = new ResponseBean();
        try {
            QueryWrapper<TaMiniappIcon> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", CommConstant.STATUS_NORMAL);
            queryWrapper.orderByDesc("create_date");

            List<TaMiniappIcon> list = iTaMiniappIconService.list(queryWrapper);
            responseBean.addSuccess(list);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taMiniappIconList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

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

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