package com.yunzhi.marketing.xlk.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.xlk.entity.CustomerInfoModifyRecord;
import com.yunzhi.marketing.xlk.service.ICustomerInfoModifyRecordService;
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 2021-07-29
 */
@RestController
@RequestMapping("/")
public class CustomerInfoModifyRecordController extends BaseController {

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

    @Autowired
    public ICustomerInfoModifyRecordService iCustomerInfoModifyRecordService;


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

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

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

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

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

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