package com.yunzhi.marketing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.entity.TaMiniappOrgIcon;
import com.yunzhi.marketing.service.ITaMiniappOrgIconService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

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

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

    @Autowired
    public ITaMiniappOrgIconService iTaMiniappOrgIconService;


    /**
     * 查询列表
     * @return
     */
    @RequestMapping(value="/wx/taMiniappOrgIcon",method= RequestMethod.GET)
    public ResponseBean taMiniappOrgIconList(HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            QueryWrapper<TaMiniappOrgIcon> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id",getOrgId(request));
            queryWrapper.orderByDesc("sort");
            queryWrapper.orderByDesc("create_date");

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

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

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

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

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