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.CustomerVisit;
import com.yunzhi.marketing.xlk.service.ICustomerVisitService;
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 CustomerVisitController extends BaseController {

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

    @Autowired
    public ICustomerVisitService iCustomerVisitService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/customerVisit",method= RequestMethod.GET)
    public ResponseBean customerVisitList(@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<CustomerVisit> pg = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<CustomerVisit> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CustomerVisit::getOrgId,getOrgId(request));
            queryWrapper.orderByDesc(CustomerVisit::getCreateDate);

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

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

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

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

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