package com.zfire.mall.manager.controller.config;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.config.*;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.logic.config.FirstPartyCustomerManagementLogic;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.Collections;
import java.util.List;

/**
 * @author qinrongjun
 * @description
 * @date 2023/7/26 11:14 星期三
 */

@RequiredArgsConstructor
@Validated
@RestController
@Api(value = "甲方客户管理", tags = "甲方客户管理")
@RequestMapping(value = "config/firstPartyCustomerManagement")
public class FirstPartyCustomerManagementController {
    private final FirstPartyCustomerManagementLogic firstPartyCustomerManagementLogic;
    private final CommonLogic commonLogic;

    @ZfireList
    @PostMapping("list")
    @ApiOperation(value = "列表")
    public ResponseHelper<IPage<FirstPartyCustomerManagementListVO>> list(@RequestBody ZfireParamBean param) {
        IPage<FirstPartyCustomerManagementListVO> page = firstPartyCustomerManagementLogic.list(FieldUtils.supplyParam(param));
        return ResponseHelper.success(page, new TypeReference<FirstPartyCustomerManagementListVO>() {});
    }

    @PostMapping("detail")
    @ApiOperation(value = "详情")
    public ResponseHelper<FirstPartyCustomerManagementDetailVO> detail(
            @ApiParam("商家登录名额限制Id")
            @NotBlank(message = "id不能为空")
            @Length(max = 64, message = "id最大长度为64")
            @RequestParam String id) {
        return ResponseHelper.success(firstPartyCustomerManagementLogic.detail(id));
    }

    @PostMapping("edit")
    @ApiOperation(value = "编辑")
    public ResponseHelper edit(@RequestBody @Valid FirstPartyCustomerManagementEditParam param) {
        return ResponseHelper.toResult(firstPartyCustomerManagementLogic.edit(param, commonLogic.getAdminUser()));
    }

    @PostMapping("add")
    @ApiOperation(value = "新增")
    public ResponseHelper add(@RequestBody @Valid FirstPartyCustomerManagementAddParam param) {
        return ResponseHelper.toResult(firstPartyCustomerManagementLogic.add(param, commonLogic.getAdminUser()));
    }

    @PostMapping("del")
    @ApiOperation(value = "删除")
    public ResponseHelper del(
            @ApiParam("商家登录名额限制Id")
            @NotEmpty(message = "id不能为空")
            @RequestParam @Valid List<@Length(max = 64, message = "id最大长度为64")String> idList) {
        return ResponseHelper.success(firstPartyCustomerManagementLogic.del(idList));
    }

    @PostMapping("export")
    @ApiOperation(value = "导出")
    public void export(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<FirstPartyCustomerManagementListVO> page = firstPartyCustomerManagementLogic.list(zfireParam);
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }
}
