package com.mortals.xhx.modules.supplement.web;

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.common.code.ExcelUploadEnum;
import com.mortals.xhx.common.pdu.ApiRespPdu;
import com.mortals.xhx.modules.supplement.model.SupplementEntity;
import com.mortals.xhx.modules.supplement.service.SupplementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


/**
* <p>Title: 办件补录表</p>
* <p>Description: SupplementController  </p>
* <p>Copyright: Copyright &reg;  </p>
* <p>Company: </p>
* @author zxfei
* @version 1.0.0
*/
@RestController
@RequestMapping("supplement")
@Slf4j
public class SupplementController extends BaseCRUDJsonMappingController<SupplementService,SupplementForm, SupplementEntity,Long> {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private ICacheService iCacheService;


    /**
     * 导入办件信息，一阶段通过excel表格导入,放入磁盘中，在通过文件地址去获取文件内容进行数据清洗
     */
    @PostMapping(value="importOfficeInformation")
    public ApiRespPdu importOfficeInformation(@RequestPart("file") MultipartFile file,String sysCode) throws Exception{
        ApiRespPdu respPdu=new ApiRespPdu();
        if (StringUtils.isNotEmpty(sysCode)){
            String filePath = uploadService.saveFileUpload(file, null, null);
            Map<String, String> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("sysCode",sysCode);
            objectObjectHashMap.put("status",ExcelUploadEnum.未处理的文件路径.getCode());
            iCacheService.hset(ExcelUploadEnum.UPLOAD_PATH.getCode(),filePath,objectObjectHashMap);
            respPdu.setCode(200);
            respPdu.setMsg("上传文件成功");
        }else {
            respPdu.setCode(-1);
            respPdu.setMsg("缺少系统编码");
        }
        return respPdu;
    }



    public SupplementController(){
        super.setFormClass(SupplementForm.class);
        super.setModuleDesc("办件补录表");
    }



    /**
     *
     */
    @PostMapping(value="/test/importOfficeInformation")
    public ApiRespPdu test(@RequestPart("file") MultipartFile file) throws Exception{
//        Long id = getContext().getUser().getId();
        Long id = 0L;
        ApiRespPdu respPdu=new ApiRespPdu();
        String filePath = uploadService.saveFileUpload(file, null, null);
        Map<String, String> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("sysCode","000L");
        objectObjectHashMap.put("status",ExcelUploadEnum.未处理的文件路径.getCode());
        iCacheService.hset(ExcelUploadEnum.UPLOAD_PATH.getCode(),filePath,objectObjectHashMap);
        log.info("测试地址========"+filePath);
        return respPdu;
    }


    @Value("${upload.path}")
    private String filePath;
    /**
     *
     */
    @PostMapping(value="/test/inforte02")
    public ApiRespPdu inforte02() throws Exception{
        Map<String, String> map = iCacheService.hgetAll(ExcelUploadEnum.UPLOAD_PATH.getCode());
        String file="";
        for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
            JSONObject jsonObject = JSONObject.parseObject(stringStringEntry.getValue());
            System.out.println(jsonObject.getString("sysCode"));
            System.out.println(jsonObject.getString("status"));
        }
        log.info("测试读取后的内容========"+map);
        ApiRespPdu respPdu=new ApiRespPdu();
        return respPdu;
    }


}