package com.huiju.estateagents.redpack.controller;

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 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.redpack.entity.TaOrgOrderDetail;
import com.huiju.estateagents.redpack.service.ITaOrgOrderDetailService;

/**
 * <p>
    * 组织订单明细  前端控制器
    * </p>
 *
 * @author fxf
 * @since 2020-06-03
 */
@RestController
@RequestMapping("/")
public class TaOrgOrderDetailController extends BaseController {

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

    @Autowired
    public ITaOrgOrderDetailService iTaOrgOrderDetailService;


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

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

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

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

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

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