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

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.dao.ICRUDDao;
import com.mortals.framework.model.BaseEntityLong;
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.code.PushTypeEnum;
import com.mortals.xhx.common.pdu.ApiRespPdu;
import com.mortals.xhx.modules.implementlist.dao.ImplementlistDao;
import com.mortals.xhx.modules.implementlist.dao.RybMatterDao;
import com.mortals.xhx.modules.implementlist.dao.RybWorkDao;
import com.mortals.xhx.modules.implementlist.listener.ImplementImportListener;
import com.mortals.xhx.modules.implementlist.listener.RybMatterImportListener;
import com.mortals.xhx.modules.implementlist.listener.RybWorkImportListener;
import com.mortals.xhx.modules.implementlist.model.ImplementlistEntity;
import com.mortals.xhx.modules.implementlist.model.RybMatterListEntry;
import com.mortals.xhx.modules.implementlist.model.RybWorkEntry;
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.io.IOException;
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;

    @Autowired
    private RybWorkDao rybWorkDao;

    @Autowired
    private ImplementlistDao implementlistDao;

    @Autowired
    private RybMatterDao matterDao;

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

    /*
     *
     * 蓉易办对接入口,excel上传,工作人员上传
     * */
    @RequestMapping("rybImportWork")
    public ApiRespPdu<Object> rybImportWork(@RequestPart("file") MultipartFile file){
        ApiRespPdu respPdu=new ApiRespPdu();
        try {
            EasyExcel.read(file.getInputStream(), RybWorkEntry.class, new RybWorkImportListener(rybWorkDao))
                    .sheet(0)
                    .headRowNumber(3)
                    .doRead();
            respPdu.setCode(200);
            respPdu.setMsg("上传文件成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return respPdu;
    }

    /*
     * excel上传,事项清单
     * */
    @RequestMapping("importMatter")
    public ApiRespPdu<Object> importMatter(@RequestPart("file") MultipartFile file, Integer type){
        ApiRespPdu respPdu=new ApiRespPdu();
        try {
            if(PushTypeEnum.SYTH.getValue() == type){
                EasyExcel.read(file.getInputStream(), ImplementlistEntity.class, new ImplementImportListener(implementlistDao))
                        .sheet(0)
                        .headRowNumber(1)
                        .doRead();
            }else if(PushTypeEnum.RYB.getValue() == type){
                EasyExcel.read(file.getInputStream(), RybMatterListEntry.class, new RybMatterImportListener(matterDao))
                        .sheet(0)
                        .headRowNumber(1)
                        .doRead();
            }
            respPdu.setCode(200);
            respPdu.setMsg("上传文件成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        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;
    }


}