package com.yuchen.saas.park.service.controller.property;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.dto.property.*;
import com.yuchen.saas.park.api.vo.property.*;
import com.yuchen.saas.park.service.service.property.IPropertyCustomerArchivesService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.nest.springwrap.core.excel.util.ExcelUtil;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author 蔡文强
 * @Date 2023/11/28 10:56
 * @Description 客户档案表 前端控制器
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/propertyCustomerArchives")
public class PropertyCustomerArchivesController {

    @Resource
    private IPropertyCustomerArchivesService baseService;

    /**
     * 添加客户档案
     * @param customerArchivesDto 客户档案Dto
     * @return TRUE/FALSE
     */
    @PostMapping("/saveCustomerArchives")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "添加客户档案", notes = "添加客户档案")
    public R<Boolean> saveCustomerArchives(@RequestBody @Validated PropertyCustomerArchivesDto customerArchivesDto) {
        return R.data(baseService.saveCustomerArchives(customerArchivesDto));
    }

    /**
     * 编辑客户档案
     * @param customerArchivesDto 客户档案Dto
     * @return TRUE/FALSE
     */
    @PostMapping("/updateCustomerArchives")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "编辑客户档案", notes = "编辑客户档案")
    public R<Boolean> updateCustomerArchives(@RequestBody @Validated PropertyCustomerArchivesDto customerArchivesDto) {
        return R.data(baseService.updateCustomerArchives(customerArchivesDto));
    }

    /**
     * 合并客户档案
     * @param
     * @return
     */
    @PostMapping("/mergeCustomerArchives")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "合并客户档案", notes = "合并客户档案")
    public R<Boolean> mergeCustomerArchives(@RequestBody List<PropertyCustomerArchivesMergeDto> mergeDtoList) {
        return R.data(baseService.mergeCustomerArchives(mergeDtoList));
    }

    /**
     * 删除客户档案
     * @param id 客户档案Dto
     * @return TRUE/FALSE
     */
    @DeleteMapping("/deleteCustomerArchives/{id}")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "删除客户档案", notes = "删除客户档案")
    public R<Boolean> deleteCustomerArchives(@ApiParam(name = "id", value = "主键ID", required = true) @PathVariable("id") Long id) {
        return R.data(baseService.deleteCustomerArchives(id));
    }

    /**
     * 客户档案详情
     * @param id 客户档案ID
     * @return 客户档案详情Vo
     */
    @GetMapping("/getCustomerArchivesById/{id}")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "客户档案详情", notes = "客户档案详情")
    public R<PropertyCustomerArchivesVo> getCustomerArchivesById(@ApiParam(name = "id", value = "主键ID", required = true) @PathVariable("id") Long id) {
        return R.data(baseService.getCustomerArchivesById(id));
    }

    /**
     * 客户档案列表(分页)
     * @param queryDto 客户档案列表查询参数
     * @return 客户档案列表Vo
     */
    @PostMapping("/getCustomerArchivesPageList")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "客户档案列表(分页)", notes = "客户档案列表(分页)")
    public R<NestPage<PropertyCustomerArchivesListVo>> getCustomerArchivesPageList(@RequestBody PropertyCustomerArchivesQueryDto queryDto) {
        return R.data(baseService.getCustomerArchivesPageList(queryDto));
    }

    /**
     * 客户档案列表
     * @param queryDto 客户档案列表查询参数
     * @return 客户档案列表Vo
     */
    @PostMapping("/getCustomerArchivesList")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "客户档案列表", notes = "客户档案列表")
    public R<List<PropertyCustomerArchivesVo>> getCustomerArchivesList(@RequestBody PropertyCustomerArchivesQueryDto queryDto) {
        return R.data(baseService.getCustomerArchivesList(queryDto));
    }

    /**
     * 客户档案重名列表
     * @param queryDto 客户档案重名列表查询参数
     * @return 客户档案重名列表Vo
     */
    @PostMapping("/getDupNameList")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "客户档案重名列表", notes = "客户档案重名列表")
    public R<List<PropertyCustomerArchivesDupNameListVo>> getDupNameList(@RequestBody PropertyCustomerArchivesDupNameListQueryDto queryDto) {
        return R.data(baseService.getDupNameList(queryDto));
    }

    /**
     * 导入客户档案
     * @return page 分页数据
     */
    @PostMapping("/importCustomerArchives")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "导入客户档案", notes = "导入客户档案")
    public R<PropertyCustomerArchivesImportResVo> importCustomerArchives(@RequestParam("file") MultipartFile file, Map map) {
        return R.data(baseService.importCustomerArchives(MapUtils.getInteger(map, "type"), file));
    }

    /**
     * 导出企业客户档案模板
     * @return page 分页数据
     */
    @GetMapping("/exportTemplateV1")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "导出企业客户档案模板", notes = "导出企业客户档案模板")
    public void exportTemplateV1(HttpServletResponse response) {
        List<PropertyCustomerArchivesOrgImportExcelVo> list = new ArrayList<>();
        ExcelUtil.export(response, "企业客户档案模板", "客户档案模板", list, PropertyCustomerArchivesOrgImportExcelVo.class);
    }

    /**
     * 导出人员客户档案模板
     * @return page 分页数据
     */
    @GetMapping("/exportTemplateV2")
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "导出人员客户档案模板", notes = "导出人员客户档案模板")
    public void exportTemplateV2(HttpServletResponse response) {
        List<PropertyCustomerArchivesPerImportExcelVo> list = new ArrayList<>();
        ExcelUtil.export(response, "人员客户档案模板", "人员客户档案模板", list, PropertyCustomerArchivesPerImportExcelVo.class);
    }

    /**
     * 导出客户档案
     * @return page 分页数据
     */
    @PostMapping("/exportCustomerArchives")
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "导出客户档案", notes = "导出客户档案")
    public void exportCustomerArchives(@RequestBody PropertyCustomerArchivesQueryDto queryDto, HttpServletResponse response) {
        baseService.exportCustomerArchives(queryDto, response);
    }


    /**
     * 客户绑定的房源列表
     * @param id 客户档案ID
     * @return 房源列表Vo
     */
    @GetMapping("/getBindHousesByCustomerArchivesId/{id}")
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "客户档案详情", notes = "客户档案详情")
    public R<List<PropertyHousesVo>> getBindHousesByCustomerArchivesId(@ApiParam(name = "id", value = "主键ID", required = true) @PathVariable("id") Long id) {
        return R.data(baseService.getBindHousesByCustomerArchivesId(id));
    }


    /**
     * 获取房源的租客或者业主客户档案信息
     * @param houseId 房源ID
     * @return 房源列表Vo
     */
    @GetMapping("/getCustomerInfoByHouseId/{houseId}")
    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "获取房源的租客或者业主客户档案信息", notes = "获取房源的租客或者业主客户档案信息")
    public R<PropertyCustomerArchivesVo> getCustomerInfoByHouseId(@ApiParam(name = "houseId", value = "房源ID", required = true) @PathVariable("houseId") Long houseId) {
        return R.data(baseService.getCustomerInfoByHouseId(houseId));
    }

    /**
     * 获取房源的租客或者业主客户档案列表
     * @param houseId 房源ID
     * @return 房源列表Vo
     */
    @GetMapping("/getCustomerListByHouseId/{houseId}")
    @ApiOperationSupport(order = 15)
    @ApiOperation(value = "获取房源的租客或者业主客户档案列表", notes = "获取房源的租客或者业主客户档案列表")
    public R<List<PropertyCustomerArchivesVo>> getCustomerListByHouseId(@ApiParam(name = "houseId", value = "房源ID", required = true) @PathVariable("houseId") Long houseId) {
        return R.data(baseService.getCustomerListByHouseId(houseId));
    }

}
