package com.cool.store.controller.webb;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.cool.store.context.CurrentUserHolder;
import com.cool.store.dto.BatchStatusRefreshDTO;
import com.cool.store.entity.ImportTaskDO;
import com.cool.store.enums.FileTypeEnum;
import com.cool.store.enums.ImportTaskStatusEnum;
import com.cool.store.mapper.ImportTaskMapper;
import com.cool.store.response.ResponseResult;
import com.cool.store.response.bigdata.ApiResponse;
import com.cool.store.service.DataHandlerServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author: WangShuo
 * @Date: 2025/05/12/14:40
 * @Version 1.0
 * @注释:
 */
@RequestMapping("/pc/dataHandler")
@RestController
@Api("数据处理")
@Slf4j
public class DataHandlerController {

    @Resource
    private DataHandlerServer dataHandlerServer;
    @Resource
    private ImportTaskMapper importTaskMapper;


    @Value("${mybatis.configuration.variables.enterpriseId}")
    private String eid;

    @PostMapping("/importOaOldShopData")
    @ApiOperation("导入OA旧数据")
    public ResponseResult<Boolean> importOaOldShopData(MultipartFile file) {
        ExcelReader reader = null;
        try {
            reader = ExcelUtil.getReader(file.getInputStream());
        } catch (IOException e) {
            log.error("read file error:", e);
        }
        assert reader != null;
        log.info("----------------------:{}", file.getOriginalFilename());
        List<Map<String, Object>> dataMapList = reader.read(0, 1, Integer.MAX_VALUE);
        ImportTaskDO importTaskDO = new ImportTaskDO();
        importTaskDO.setFileName(file.getOriginalFilename());
        importTaskDO.setFileType(FileTypeEnum.IMPORT_OA_OLD_SHOP_DATA.getFileType());
        importTaskDO.setIsImport(true);
        importTaskDO.setStatus(ImportTaskStatusEnum.PROGRESS.getCode());
        importTaskDO.setCreateUserId(CurrentUserHolder.getUserId());
        importTaskDO.setCreateName(CurrentUserHolder.getUser().getName());
        importTaskDO.setCreateTime(System.currentTimeMillis());
        importTaskMapper.insert(eid, importTaskDO);
        dataHandlerServer.importOaOldShopData(dataMapList, file.getOriginalFilename(), CurrentUserHolder.getUser(), importTaskDO);
        return ResponseResult.success(true);
    }

    @PostMapping("/dataStageHandler")
    @ApiOperation("阶段处理")
    public ResponseResult<Boolean> dataStageHandler(MultipartFile file, @RequestParam("flag") Boolean flag) {
        ExcelReader reader = null;
        try {
            reader = ExcelUtil.getReader(file.getInputStream());
        } catch (IOException e) {
            log.error("read file error:", e);
        }
        assert reader != null;
        log.info("----------------------:{}", file.getOriginalFilename());
        List<Map<String, Object>> dataMapList = reader.read(0, 1, Integer.MAX_VALUE);
        ImportTaskDO importTaskDO = new ImportTaskDO();
        importTaskDO.setFileName(file.getOriginalFilename());
        importTaskDO.setFileType(FileTypeEnum.IMPORT_OA_OLD_SHOP_STAGE_DATA.getFileType());
        importTaskDO.setIsImport(true);
        importTaskDO.setStatus(ImportTaskStatusEnum.PROGRESS.getCode());
        importTaskDO.setCreateUserId(CurrentUserHolder.getUserId());
        importTaskDO.setCreateName(CurrentUserHolder.getUser().getName());
        importTaskDO.setCreateTime(System.currentTimeMillis());
        importTaskMapper.insert(eid, importTaskDO);
        dataHandlerServer.dataStageHandler(dataMapList, file.getOriginalFilename(), CurrentUserHolder.getUser(), importTaskDO, flag);
        return ResponseResult.success(true);
    }


    @GetMapping("/decorationDataInit")
    @ApiOperation("装修数据初始化")
    public ResponseResult decorationDataInit() {
        dataHandlerServer.decorationDataInit();
        return ResponseResult.success(true);
    }

    @PostMapping("/dataHandlerV20241012")
    @ApiOperation("导入OA202410-12旧数据")
    public ResponseResult<Boolean> dataHandlerV20241012(MultipartFile file) {
        ExcelReader reader = null;
        try {
            reader = ExcelUtil.getReader(file.getInputStream());
        } catch (IOException e) {
            log.error("read file error:", e);
        }
        assert reader != null;
        List<Map<String, Object>> dataMapList = reader.read(0, 1, Integer.MAX_VALUE);
        ImportTaskDO importTaskDO = new ImportTaskDO();
        importTaskDO.setFileName(file.getOriginalFilename());
        importTaskDO.setFileType(FileTypeEnum.IMPORT_OA_OLD_SHOP_DATA.getFileType());
        importTaskDO.setIsImport(true);
        importTaskDO.setStatus(ImportTaskStatusEnum.PROGRESS.getCode());
        importTaskDO.setCreateUserId(CurrentUserHolder.getUserId());
        importTaskDO.setCreateName(CurrentUserHolder.getUser().getName());
        importTaskDO.setCreateTime(System.currentTimeMillis());
        importTaskMapper.insert(eid, importTaskDO);
        dataHandlerServer.dataHandlerV20241012(dataMapList, file.getOriginalFilename(), CurrentUserHolder.getUser(), importTaskDO);
        return ResponseResult.success(true);
    }
    @GetMapping("/JingDongStageHandler")
    @ApiOperation("京东阶段处理")
    public ResponseResult<Integer> JingDongStageHandler() {
        return ResponseResult.success(dataHandlerServer.JingDongStageHandler());
    }

    @PostMapping("/dataHandlerV2025029")
    @ApiOperation("/处理测量 设计 施工 验收 完成")
    public ResponseResult<Boolean> dataHandlerV2025029(MultipartFile file) {
        ExcelReader reader = null;
        try {
            reader = ExcelUtil.getReader(file.getInputStream());
        } catch (IOException e) {
            log.error("read file error:", e);
        }
        assert reader != null;
        List<Map<String, Object>> dataMapList = reader.read(0, 1, Integer.MAX_VALUE);
        ImportTaskDO importTaskDO = new ImportTaskDO();
        importTaskDO.setFileName(file.getOriginalFilename());
        importTaskDO.setFileType(FileTypeEnum.IMPORT_FITMENT_SHOP_STAGE.getFileType());
        importTaskDO.setIsImport(true);
        importTaskDO.setStatus(ImportTaskStatusEnum.PROGRESS.getCode());
        importTaskDO.setCreateUserId(CurrentUserHolder.getUserId());
        importTaskDO.setCreateName(CurrentUserHolder.getUser().getName());
        importTaskDO.setCreateTime(System.currentTimeMillis());
        importTaskMapper.insert(eid, importTaskDO);
        dataHandlerServer.dataHandlerV2025029(dataMapList, file.getOriginalFilename(), CurrentUserHolder.getUser(), importTaskDO);
        return ResponseResult.success(true);
    }

    /**
     * @Auther: wangshuo
     * @Date: 2025/5/23
     * @description:不是对外回调接口，针对于已经在oa走完数据的，我们手动调用接口批量修改数据！！！只做云流水别的别用！！！
     */
    @ApiOperation("批量修改门店云流水开通状态")
    @PostMapping("/batchStatusRefreshYls")
    public ApiResponse<Boolean> batchStatusRefreshYls(@RequestBody @Validated BatchStatusRefreshDTO batchStatusRefreshDTO) {
        return ApiResponse.success(dataHandlerServer.batchStatusRefreshYls(batchStatusRefreshDTO));
    }

    @ApiOperation("处理加盟费阶段")
    @GetMapping("/franchiseFeeStageDateHandler")
    public  ResponseResult<Boolean> franchiseFeeStageDateHandler(){
        return ResponseResult.success(dataHandlerServer.franchiseFeeStageDateHandler());
    }
    @ApiOperation("处理加盟费缴费信息历史数据")
    @GetMapping("/franchiseFeeDateHandler")
    public ResponseResult<Boolean> franchiseFeeDateHandler(@RequestParam("pageSize") Integer pageSize, @RequestParam("pageNum") Integer pageNum){
        return ResponseResult.success(dataHandlerServer.franchiseFeeDateHandler(pageSize,pageNum));
    }

    @ApiOperation("门店数据初始化")
    @GetMapping("/dataHandlerCreateStore")
    public ResponseResult<Boolean> dataHandlerCreateStore(){
        dataHandlerServer.dataHandlerCreateStore();
        return ResponseResult.success(true);
    }

    @ApiOperation("红圈通数据处理")
    @GetMapping("/dataHandlerPushHqt")
    public ResponseResult<Boolean> dataHandlerPushHqt(){
        dataHandlerServer.dataHandlerPushHqt();
        return ResponseResult.success(true);
    }
}
