package com.yunzhi.marketing.xlk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.CustomerPreparatory;
import com.yunzhi.marketing.xlk.service.ICustomerPreparatoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.RequestHeader;
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 javax.servlet.http.HttpServletRequest;

/**
 * <p>
    * 客户认筹单  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2021-07-30
 */
@RestController
@RequestMapping("/api")
@Api(value = "", tags = "xlk-")
public class CustomerPreparatoryController extends BaseController {

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

    @Autowired
    public ICustomerPreparatoryService iCustomerPreparatoryService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/customerPreparatory",method= RequestMethod.GET)
    public ResponseBean customerPreparatoryList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                                @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                                @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<CustomerPreparatory> pg = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<CustomerPreparatory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CustomerPreparatory::getOrgId,getOrgId(request));
            queryWrapper.orderByDesc(CustomerPreparatory::getCreateDate);

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

    /**
     * 保存对象
     * @param customerPreparatory 实体对象
     * @return
     */
    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/customerPreparatory",method= RequestMethod.POST)
    public ResponseBean customerPreparatoryAdd(@RequestBody CustomerPreparatory customerPreparatory, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iCustomerPreparatoryService.save(customerPreparatory)){
                responseBean.addSuccess(customerPreparatory);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("customerPreparatoryAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ApiOperation(value = "", notes = "")
    @ResponseBody
    @RequestMapping(value="/customerPreparatory/{id}", method= RequestMethod.DELETE)
    public ResponseBean customerPreparatoryDelete(@PathVariable String id, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iCustomerPreparatoryService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("customerPreparatoryDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param customerPreparatory 实体对象
     * @return
     */
    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/customerPreparatory/{id}",method= RequestMethod.PUT)
    public ResponseBean customerPreparatoryUpdate(@PathVariable String id,
                                                  @RequestBody CustomerPreparatory customerPreparatory, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iCustomerPreparatoryService.updateById(customerPreparatory)){
                responseBean.addSuccess(customerPreparatory);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("customerPreparatoryUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/customerPreparatory/{id}",method= RequestMethod.GET)
    public ResponseBean customerPreparatoryGet(@PathVariable String id, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iCustomerPreparatoryService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("customerPreparatoryDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
