package com.crm.manager.controller;

import com.crm.manager.dto.export.CardNumberExportDto;
import com.crm.manager.entity.excel.CardNumberExcelEntity;
import com.crm.manager.service.ucard.UcardNumberFeignService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.ucard.CardNumberDto;
import com.crm.rely.backend.core.entity.base.BaseDeleteEntities;
import com.crm.rely.backend.core.entity.ucard.CardNumberAddEntity;
import com.crm.rely.backend.core.entity.ucard.CardNumberPageEntity;
import com.crm.rely.backend.core.entity.ucard.CardNumberUpdateEntity;
import com.crm.rely.backend.core.entity.ucard.CardNumberVerifyEntity;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.EasyExcelUti;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.FileProcessUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 实体卡卡号服务接口
 */
@Slf4j
@RestController
@RequestMapping("/ucard/number")
public class UcardNumberController {

    @Autowired
    private UcardNumberFeignService ucardNumberFeignService;

    /**
     * 校验实体卡卡号是否使用过
     */
    @PostMapping("/verify")
    public BaseResultDto cardNumberVerify(@RequestBody @Validated CardNumberVerifyEntity entity) throws ServiceException {
        return ucardNumberFeignService.cardNumberVerify(entity);
    }

    /**
     * 实体卡卡号导入
     */
    @PostMapping("/import")
    public BaseResultDto cardNumberImport(@RequestParam("file") MultipartFile file) throws Exception {
        // 文件类型校验
        String fileName = file.getOriginalFilename();
        if (fileName != null && !fileName.toLowerCase().endsWith(".xlsx") && !fileName.toLowerCase().endsWith(".xls")) {
            return BaseResultDto.error("请上传Excel文件");
        }
        List<CardNumberExcelEntity> entities = EasyExcelUti.readExcel(file,
                new CardNumberExcelEntity(), 1, CardNumberExcelEntity.class);

        if (CollectionUtils.isEmpty(entities)) {
            return BaseResultDto.success();
        }
        // 验证是否为纯数字格式
        List<CardNumberAddEntity> entityList = entities.stream()
                .map(entity -> {
                    // 修剪空格
                    String cardNumber = entity.getCardNumber().trim();
                    return cardNumber;
                }).filter(StringUtils::isNumeric).map(cardNumber -> {
                    CardNumberAddEntity addEntity = new CardNumberAddEntity();
                    addEntity.setCardNumber(cardNumber);
                    return addEntity;
                }).collect(Collectors.toList());
        return ucardNumberFeignService.cardNumberImport(entityList);
    }

    /**
     * 卡号模版导出
     */
    @PostMapping("/template/export")
    public void cardNumberTemplateExport(HttpServletResponse response) throws Exception {
        List<CardNumberExportDto> dtos = Lists.newArrayList();
        ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("CARD_NUMBER_TEMPLATE"), dtos, CardNumberExportDto.class, response);
    }

    /**
     * 卡号列表
     */
    @PostMapping("/list")
    ResultWithPagerDto<CardNumberDto> queryCardNumberList(@RequestBody CardNumberPageEntity entity) throws ServiceException {
        return ucardNumberFeignService.queryCardNumberList(entity);
    }

    /**
     * 卡号下拉列表
     */
    @PostMapping("/dropdown")
    BaseResultDto queryCardNumberDropdown(@RequestBody CardNumberPageEntity entity) throws ServiceException {
        // 默认查询未使用的卡号
        entity.setStatus(1);
        return ucardNumberFeignService.queryCardNumberDropdown(entity);
    }

    /**
     * 修改卡号状态
     */
    @PostMapping("/update/status")
    BaseResultDto saveCardNumberStatus(@RequestBody CardNumberUpdateEntity entity) throws ServiceException {
        return ucardNumberFeignService.saveCardNumberStatus(entity);
    }

    /**
     * 删除实体卡卡号
     */
    @PostMapping("/delete")
    public BaseResultDto deleteCardNumber(@RequestBody BaseDeleteEntities entity) throws ServiceException {
        return ucardNumberFeignService.deleteCardNumber(entity);
    }

}
