package com.yunzhi.marketing.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.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.entity.TaExtendContent;
import com.yunzhi.marketing.service.ITaBuildingService;
import com.yunzhi.marketing.service.ITaExtendContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
    * 辅助内容表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-07-25
 */
@RestController
@RequestMapping("/api")
public class TaExtendContentController extends BaseController {

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

    @Autowired
    public ITaExtendContentService iTaExtendContentService;

    @Autowired
    public ITaBuildingService iTaBuildingService;

    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/taExtendContent",method= RequestMethod.GET)
    public ResponseBean taExtendContentList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                            @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaExtendContent> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaExtendContent> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_date");

            IPage<TaExtendContent> result = iTaExtendContentService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taExtendContentList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param taExtendContent 实体对象
     * @return
     */
    @RequestMapping(value="/taExtendContent",method= RequestMethod.POST)
    public ResponseBean taExtendContentAdd(@RequestBody TaExtendContent taExtendContent){
        ResponseBean responseBean = new ResponseBean();
        try {
            Integer cityId = iTaBuildingService.getCityById(taExtendContent.getBuildingId());
            taExtendContent.setCityId(cityId);

            if (iTaExtendContentService.save(taExtendContent)){
                responseBean.addSuccess(taExtendContent);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taExtendContentAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

    /**
     * 修改对象
     * @param id  实体ID
     * @param taExtendContent 实体对象
     * @return
     */
    @RequestMapping(value="/taExtendContent/{id}",method= RequestMethod.PUT)
    public ResponseBean taExtendContentUpdate(@PathVariable Integer id,
                                        @RequestBody TaExtendContent taExtendContent){
        ResponseBean responseBean = new ResponseBean();
        try {
            Integer cityId = iTaBuildingService.getCityById(taExtendContent.getBuildingId());
            taExtendContent.setCityId(cityId);

            if (iTaExtendContentService.updateById(taExtendContent)){
                responseBean.addSuccess(taExtendContent);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taExtendContentUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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