package com.miaxis.pc.controller.customer;

import com.miaxis.common.annotation.Log;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.page.ResponsePageInfo;
import com.miaxis.common.enums.BusinessTypeEnum;
import com.miaxis.common.utils.poi.ExcelUtil;
import com.miaxis.customer.domain.CustomerInfo;
import com.miaxis.customer.service.ICustomerInfoService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【客户信息】Controller
 *
 * @author miaxis
 * @date 2021-03-10
 */
@RestController
// @RequiredArgsConstructor 代替@AutoWired注解，注意在注入时需要用final定义，或者使用@notnull注解
@RequiredArgsConstructor
@RequestMapping("/pc/customer")
@Api(tags = {"【pc-客户信息】"})
public class CustomerInfoController extends BaseController {

    private final ICustomerInfoService customerInfoService;

    /**
     * 查询客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:list')")
    @GetMapping("/list")
    @ApiOperation("查询客户信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<CustomerInfo> list(@ModelAttribute CustomerInfo customerInfo) {
        startPage();
        List<CustomerInfo> list = customerInfoService.selectCustomerInfoList(customerInfo);
        return toResponsePageInfo(list);
    }

    /**
     * 导出客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:export')")
    @Log(title = "客户信息", businessType = BusinessTypeEnum.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出客户信息列表Excel")
    public Response<String> export(@ModelAttribute CustomerInfo customerInfo) {
        List<CustomerInfo> list = customerInfoService.selectCustomerInfoList(customerInfo);
        ExcelUtil<CustomerInfo> util = new ExcelUtil<CustomerInfo>(CustomerInfo.class);
        return util.exportExcel(list, "customer");
    }

    /**
     * 获取客户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取客户信息详细信息")
    public Response<CustomerInfo> getInfo(
            @ApiParam(name = "id", value = "客户信息参数", required = true)
            @PathVariable("id") Long id
    ) {
        return Response.success(customerInfoService.getById(id));
    }

    /**
     * 新增客户信息
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:add')")
    @Log(title = "客户信息", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation("新增客户信息")
    public Response<Integer> add(@RequestBody CustomerInfo customerInfo) {
        return toResponse(customerInfoService.save(customerInfo) ? 1 : 0);
    }

    /**
     * 修改客户信息
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:edit')")
    @Log(title = "客户信息", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    @ApiOperation("修改客户信息")
    public Response<Integer> edit(@RequestBody CustomerInfo customerInfo) {
        return toResponse(customerInfoService.updateById(customerInfo) ? 1 : 0);
    }

    /**
     * 更新 0：上架 / 1：下架
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:updateShelfStatus')")
    @Log(title = "客户信息", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/updateShelfStatus")
    @ApiOperation("上架、下架")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "客户信息ids参数", dataType = "List", required = true),
            @ApiImplicitParam(name = "shelfStatus", value = "上架状态  0：上架、1：下架", dataType = "int", required = true),
    })
    public Response updateShelfStatus(Long[] ids,Integer shelfStatus) {
        return customerInfoService.updateShelfStatus(ids,shelfStatus);
    }


    /**
     * 删除客户信息(伪删除)
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:remove')")
    @Log(title = "客户信息", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/{ids}")
    @ApiOperation("删除客户信息")
    public Response remove(
            @ApiParam(name = "ids", value = "客户信息ids参数", required = true)
            @PathVariable Long[] ids
    ) {
        return customerInfoService.removeCustomerByIds(ids);
    }

}
