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 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 com.huiju.estateagents.service.ITaCustomerFromService;
import com.huiju.estateagents.entity.TaCustomerFrom;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
    * 客户来源  前端控制器
    * </p>
 *
 * @author yansen
 * @since 2020-04-27
 */
@RestController
@RequestMapping("/")
public class TaCustomerFromController extends BaseController {

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

    @Autowired
    public ITaCustomerFromService iTaCustomerFromService;


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

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

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

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

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

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