package com.chinaweal.aiccs.aiccs.nonIocalCirculation.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.entity.EOtCase;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.entity.EOtPermit;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.entity.dto.EOTCaseDto;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.entity.dto.EOTPermitDto;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.excelListener.EOTCaseExcelListener;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.excelListener.EOTPermitExcelListener;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.service.EOtCaseService;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.service.EOtPermitService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.page.PageRequestDto;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.Map;


/**
 * <p>
 * 行政许可信息(临时) 前端控制器
 * </p>
 *
 * @author Yweirdo
 * @since 2021-08-18
 */
@RestController
@RequestMapping("/eOtPermit")
public class EOtPermitController extends BaseController {

    @Autowired
    EOtPermitService eOtPermitService;

    /**
     * 行政许可信息查询
     * @param pageRequestDto 分页参数
     * @param request
     * @return
     */
    @RequestMapping("queryAll")
    public RestResult<?> queryAll(@RequestBody PageRequestDto pageRequestDto, HttpServletRequest request){
        Map<String, Object> customParamMap = pageRequestDto.getCustomParamMap();
        String entname = "";
        String uniscid = "";
        String regno = "";

        if(customParamMap != null){
            entname = StringUtils.tranObject(customParamMap.get("entname"));
            uniscid = StringUtils.tranObject(customParamMap.get("uniscid"));
            regno = StringUtils.tranObject(customParamMap.get("regno"));
        }

        LambdaQueryWrapper<EOtPermit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(regno), EOtPermit::getRegno,regno)
                .like(StringUtils.isNotBlank(entname),EOtPermit::getEntname,entname)
                .eq(StringUtils.isNotBlank(uniscid),EOtPermit::getUniscid,uniscid);

        Page<EOtPermit> page = eOtPermitService.page(new Page<>(pageRequestDto.getCurPage(),pageRequestDto.getPageSize()),wrapper);
        return RestResult.ok(page);
    }

    /**
     * 行政处罚信息上传并保存至数据库
     * @param httpServletRequest
     * @param file
     * @return
     */
    @PostMapping("uploadAndSave")
    public RestResult<?> uploadAndSave(HttpServletRequest httpServletRequest, MultipartFile file){
        AssertUtils.isNotNull(file);
        String filename = file.getOriginalFilename();
        if (filename == null || (!filename.toLowerCase().endsWith(".xls") && !filename.toLowerCase().endsWith(".xlsx"))) {
            return RestResult.error(ResultCode.BUSINESS_LOGIC_ERROR,"文件类型错误");
        }
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            EasyExcel.read(inputStream, EOTPermitDto.class, new EOTPermitExcelListener(eOtPermitService)).sheet().doRead();
            return RestResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RestResult.error(ResultCode.MYSTERIOUS_ERROR);
    }

}

