package com.cm.central.control.controller.apis.customer;

import com.cm.common.annotation.CheckRequestBodyAnnotation;
import com.cm.common.base.AbstractController;
import com.cm.common.component.SecurityComponent;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.pojo.dtos.CurrentUserIdInfoDTO;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData;
import com.cm.common.result.SuccessResultList;
import com.cm.central.control.pojo.dtos.customer.CustomerDTO;
import com.cm.central.control.pojo.vos.customer.CustomerVO;
import com.cm.central.control.service.customer.ICustomerService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: CustomerController
 * @Description: 客户管理
 * @Author: WenG
 * @Date: 2020-07-30 19:00
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "客户管理接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/customer")
public class CustomerController extends AbstractController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private SecurityComponent securityComponent;

    @ApiOperation(value = "新增客户管理", notes = "新增客户管理接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savecustomer")
    @CheckRequestBodyAnnotation
    public SuccessResult saveCustomer(@RequestBody CustomerVO customerVO) throws Exception {
        return customerService.saveCustomer(customerVO);
    }

    @ApiOperation(value = "删除客户管理(id列表)", notes = "删除客户管理(id列表)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("removecustomer/{ids}")
    public SuccessResult removeCustomer(@PathVariable("ids") String ids) throws RemoveException {
        return customerService.removeCustomer(ids);
    }

    @ApiOperation(value = "修改客户管理", notes = "修改客户管理接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId", value = "客户管理ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatecustomer/{customerId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateCustomer(@PathVariable("customerId") String customerId, @RequestBody CustomerVO customerVO) throws Exception {
        return customerService.updateCustomer(customerId, customerVO);
    }

    @ApiOperation(value = "客户管理详情(通过ID)", notes = "客户管理详情(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId", value = "客户管理ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getcustomerbyid/{customerId}")
    public CustomerDTO getCustomerById(@PathVariable("customerId") String customerId) throws SearchException {
        return customerService.getCustomerById(customerId);
    }

    @ApiOperation(value = "客户管理列表", notes = "客户管理列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listcustomer")
    public List<CustomerDTO> listCustomer() throws SearchException {
        Map<String, Object> params = requestParams();
        return customerService.listCustomer(params);
    }

    @ApiOperation(value = "客户管理分页列表", notes = "客户管理分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpagecustomer")
    public SuccessResultList<List<CustomerDTO>> listPageCustomer(ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return customerService.listPageCustomer(page);
    }

    @ApiOperation(value = "客户管理统计", notes = "客户管理统计接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("countcustomer")
    SuccessResultData<Integer> countCustomer() throws SearchException {
        Map<String, Object> params = requestParams();
        return customerService.countCustomer(params);
    }

    @ApiOperation(value = "当前用户id信息", notes = "当前用户id信息接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getcurrentuseridinfo")
    public CurrentUserIdInfoDTO getCurrentUserIdInfo() {
        return securityComponent.getCurrentUserIdInfo();
    }

}