package org.jeecg.modules.api.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.ai.entity.PreReviewRecords;
import org.jeecg.modules.ai.entity.ReasonEvaluation;
import org.jeecg.modules.ai.entity.YsDirectoryMaterialTemplate;
import org.jeecg.modules.ai.qo.PreExaminationMattersQuery;
import org.jeecg.modules.ai.qo.PreReviewMaterialQuery;
import org.jeecg.modules.ai.qo.PreReviewRecordsApiQuery;
import org.jeecg.modules.ai.service.IPreExaminationMattersFieldService;
import org.jeecg.modules.ai.service.IPreExaminationMattersService;
import org.jeecg.modules.ai.service.IPreReviewMaterialService;
import org.jeecg.modules.ai.service.IPreReviewRecordsDetailService;
import org.jeecg.modules.ai.service.IPreReviewRecordsService;
import org.jeecg.modules.ai.service.IReasonEvaluationService;
import org.jeecg.modules.ai.service.IRoomService;
import org.jeecg.modules.ai.service.IYsDirectoryMaterialService;
import org.jeecg.modules.ai.service.IYsDirectoryMaterialTemplateService;
import org.jeecg.modules.ai.vo.PreExaminationMattersVo;
import org.jeecg.modules.ai.vo.PreReviewMaterialVo;
import org.jeecg.modules.ai.vo.PreReviewRecordsDetailVo;
import org.jeecg.modules.system.entity.SysAnnouncement;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.jeecg.modules.system.util.RecognizeGeneralTextSolution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;

/**
 * 预审 对外接口请求类
 */
@Api(tags="前端接口")
@RestController
@RequestMapping("/ai/api")
public class AiAPIController {
	
	@Autowired
	private IPreExaminationMattersService preExaminationMattersService;
	@Autowired
	private IPreReviewMaterialService preReviewMaterialService;
	@Autowired
	private IPreReviewRecordsDetailService preReviewRecordsDetailService;
	@Autowired
	private IPreReviewRecordsService preReviewRecordsService;
	@Autowired
	private IReasonEvaluationService reasonEvaluationService;
	@Autowired
	private IRoomService iRoomService;
	@Autowired
	private ISysAnnouncementService iSysAnnouncementService;
	@Autowired
	private IPreExaminationMattersFieldService iPreExaminationMattersFieldService;
	@Autowired
	private IYsDirectoryMaterialTemplateService iYsDirectoryMaterialTemplateService;
	@Autowired
	private IYsDirectoryMaterialService iYsDirectoryMaterialService;
	
	/**
	 * 获取已授权预审事项
	 * @param preExaminationMattersQuery
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
    @GetMapping("/getPreExaminationMattersPage")
    public Result<?>  getPreExaminationMattersPage(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			   HttpServletRequest req){
    	PreExaminationMattersQuery preExaminationMattersQuery = new PreExaminationMattersQuery();
    	preExaminationMattersQuery.setDelFlag(0);
    	preExaminationMattersQuery.setAuthorStatus(1);
    	Page<PreExaminationMattersVo> page = new Page<PreExaminationMattersVo>(pageNo, pageSize);
		IPage<PreExaminationMattersVo> pageList = preExaminationMattersService.getPreExaminationMattersList(page, preExaminationMattersQuery);
		return Result.OK(pageList);
    }
   
    /**
     * 获取当前申请事项所需提交材料
     * @param eventCode 事项编码
     * @return
     */
    @GetMapping("/getPreReviewMaterialList")
    public Result<?>  getPreReviewMaterialList(@RequestParam(name = "eventCode", required = true) String eventCode){
    	return Result.OK(preReviewMaterialService.getPreReviewMaterialList(eventCode));
    }
    
    /**
     * 获取已提交材料
     * @param eventCode
     * @param userId
     * @return
     */
    @GetMapping("/getSubmittedMaterialList")
    public Result<?>  getSubmittedMaterialList(@RequestParam(name = "eventCode", required = true) String eventCode,
    		@RequestParam(name = "userId", required = true) String userId){
    	return Result.OK(preReviewRecordsDetailService.getSubmittedMaterialList(eventCode, userId));
    }
    
    /**
     * 提交预审数据
     * @param reviewInfo
     * @return
     */
    @PostMapping(value = "/addReviewRecord")
	public Result<?> addReviewRecord(@RequestBody String reviewInfo) {
		return Result.OK(preReviewRecordsService.saveInfo(reviewInfo));
	}
    
    /**
     * 评价状态修改
     * @param reviewInfo
     * @return
     */
    @PostMapping(value = "/updateEvaluateStatue")
    public Result<?> updateEvaluateStatue(@RequestBody PreReviewRecordsApiQuery query) {
    	preReviewRecordsService.updateEvaluateStatue(query);
    	return Result.OK("评价成功");
    }
    
    
    /**
     * 获取资料上传模板信息
     * @param reviewInfo
     * @return
     */
    @GetMapping(value = "/getMaterialTemplate")
    public Result<?> getMaterialTemplate(@RequestParam(name = "materialCode", required = true) String materialCode) {
    	LambdaQueryWrapper<YsDirectoryMaterialTemplate> queryWrapper = new LambdaQueryWrapper<>();
    	queryWrapper.eq(YsDirectoryMaterialTemplate::getMaterialCode, materialCode);
    	List<YsDirectoryMaterialTemplate> listYsDirectoryMaterialTemplate = iYsDirectoryMaterialTemplateService.list(queryWrapper);
    	return Result.OK(listYsDirectoryMaterialTemplate);
    }
    
    
    /**
     * 获取OCR识别结果
     * @param reviewInfo
     * @return
     */
    @GetMapping(value = "/getOCRResult")
    public Result<?> getOCRResult(HttpServletRequest request, HttpServletResponse response) {
    	JSONObject json = new JSONObject();
    	String templateId = request.getParameter("templateId");
    	String materialCode = request.getParameter("materialCode");
    	
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象
        String savePath = preReviewRecordsDetailService.uploadLocal(file, "upload");
        if(oConvertUtils.isNotEmpty(savePath)){
            //调用接口识别
        	String ocrResult = RecognizeGeneralTextSolution.recognizeGeneralTextSolution(savePath, templateId);
        	//根据规则判断预审结果
        	String reviewResult = iYsDirectoryMaterialService.getReviewResult(materialCode, ocrResult);
        	json.put("imagePath", savePath);
        	json.put("ocrResult", ocrResult);
        	json.put("reviewResult", reviewResult);
        	return Result.OK(json.toString());
        }else {
        	return Result.error("上传失败");
        }
    }
    
    /**
     * 回显表单
     * @param reviewInfo
     * @return
     */
    @PostMapping(value = "/echoForm")
    public Result<?> echoForm(@RequestParam(name = "reviewRecordId", required = true) String reviewRecordId) {
    	return Result.OK(iPreExaminationMattersFieldService.echoForm(reviewRecordId));
    }
    
    /**
     * 创建会议室并返回链接
     * @param reviewInfo
     * @return
     */
    @GetMapping(value = "/createRoom")
    public Result<?> createRoom(@RequestParam(name = "userId", required = true) String userId) {
    	JSONObject json = iRoomService.saveRoom(userId);
    	//发送系统消息
    	iSysAnnouncementService.saveRoomAnnouncement(json.getLong("roomId"));
    	return Result.OK(json.getString("roomUrl"));
    }
    
    /**
     * 评价理由
     * @param req
     * @return
     */
    @GetMapping(value = "/listReasonEvaluation")
	public Result<?> listReasonEvaluation(HttpServletRequest req) {
		LambdaQueryWrapper<ReasonEvaluation> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(ReasonEvaluation::getStatus, 1);
		List<ReasonEvaluation> list = reasonEvaluationService.list(queryWrapper);
		return Result.OK(list);
	}

}
