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.CustomerSignatory;
import com.yunzhi.marketing.xlk.service.ICustomerSignatoryService;
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 CustomerSignatoryController extends BaseController {

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

    @Autowired
    public ICustomerSignatoryService iCustomerSignatoryService;


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

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

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

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

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

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