package com.huimv.manage.eartag.controller;

import com.alibaba.fastjson.JSONArray;
import com.huimv.manage.eartag.service.IApplyService;
import com.huimv.manage.util.Const;
import com.huimv.manage.util.DataUtil;
import com.huimv.manage.util.Result;
import com.huimv.manage.webservice.Soap;
import com.huimv.manage.webservice.task.ProduceMissionTask;
import com.sun.istack.Nullable;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@CrossOrigin
@RestController
@Slf4j
@RequestMapping(value = "/eartag/apply")
public class ApplyController {
    @Value("${webservice.url}")
    private String webServiceUrl;
    @Value("${webservice.producerId}")
    private String producerId;
    @Autowired
    private IApplyService applyService;
    @Autowired
    private Soap soap;
    @Autowired
    private ProduceMissionTask produceMissionTask;
    @Autowired
    private DataUtil dataUtil;

    /**
     * @Method : listTask
     * @Description :
     * @Params : []
     * @Return : java.lang.String
     * @Author : ZhuoNing
     * @Date : 2021/10/27
     * @Time : 20:28
     */
    @RequestMapping(value = "/listApply", method = RequestMethod.GET)
    public Result listApply(@RequestParam(value = "applyId", required = false) Integer applyId,
                            @RequestParam(value = "startDate", required = false) Date startDate,
                            @RequestParam(value = "endDate", required = false) Date endDate,
                            @RequestParam(value = "returnState", required = false) Integer returnState,
                            @RequestParam(value = "deliverState", required = false) Integer deliverState,
                            @RequestParam(value = "pageSize", required = true) Integer pageSize,
                            @RequestParam(value = "pageNum", required = true) Integer pageNo){
        //
        return applyService.listApply(applyId, startDate, endDate, returnState, deliverState, pageSize, pageNo);
    }

    /**
     * @Method : getNewTask
     * @Description :
     * @Params : []
     * @Return : java.lang.String
     * @Author : ZhuoNing
     * @Date : 2021/10/27
     * @Time : 20:29
     */
    @RequestMapping(value = "/getNewApply", method = RequestMethod.GET)
    public Result getNewTask(HttpServletRequest request) throws UnsupportedEncodingException {
        log.info("producerId>>" + producerId);
        log.info("webServiceUrl>>" + webServiceUrl);
        // 从远程接口下载新任务号数据
        JSONArray downloadApplyJa = produceMissionTask.parseReturnNewMissionXML(soap.callSoap(produceMissionTask.getNewMissionXML(producerId), webServiceUrl));
        // test
//        JSONArray downloadApplyJa = newProduceMission.parseApplyXML(newProduceMission.getNewMessionResult_test());
        if (downloadApplyJa == null) {
            return new Result(Const.CODE_APPLY_PARSE, Const.APPLY_PARSE_ERROR, false);
        }
        // 过滤已经入库的任务号
        JSONArray newApplyJa = applyService.filterExistApply(downloadApplyJa);
        if (newApplyJa.size() > 0) {
            // test
//            System.out.println("获取任务号："+newApplyJa.size());
//            for(int a=0;a<newApplyJa.size();a++){
//                JSONObject applyJo = newApplyJa.getJSONObject(a);
//                System.out.println(applyJo);
//            }
            // 保存新任务（号）
            return applyService.saveNewMission(newApplyJa);
        } else {
            return new Result(Const.CODE_APPLY_NO_NEW, Const.APPLY_NO_NEW, false);
        }
    }

    /**
     * @Method : setReturnDownloadState
     * @Description : 设置返回任务已下载
     * @Params : [applyId]
     * @Return : com.huimv.manage.util.Result
     * @Author : ZhuoNing
     * @Date : 2021/10/29
     * @Time : 11:08
     */
    @RequestMapping(value = "/setReturnDownloadState", method = RequestMethod.GET)
    public Result setReturnDownloadState(@RequestParam(value = "applyId") int applyId, HttpServletRequest request) throws UnsupportedEncodingException {
        // Step1.构造请求字串
        // Step2.发送请求传
        // Step3.请求结果入库
        String stateBln = produceMissionTask.parseReturnSetMissionHasDownStateXML(soap.callSoap(produceMissionTask.getSetMissionHasDownloadXML(applyId), webServiceUrl));
        if (stateBln.trim().equalsIgnoreCase("true")) {
            // 修改状态
            return applyService.setMissionHasDownloadState(applyId, 1);
        } else {
            return new Result(Const.CODE_UPDATE_STATE_FAIL, Const.UPDATE_STATE_FAIL, false);
        }
    }


}
