package com.huimv.manage.eartag.controller;

import com.huimv.manage.eartag.service.IEarmarkService;
import com.huimv.manage.util.Result;
import com.huimv.manage.webservice.task.EarmarkTask;
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.IOException;
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/earmark")
public class EarmarkController {
    @Value("${webservice.url}")
    private String webServiceUrl;
//    @Value("${webservice.producerId}")
//    private String producerId;
//    @Autowired
//    private Soap soap;

    @Autowired
    private EarmarkTask earmarkTask;
    @Autowired
    private IEarmarkService earmarkService;

    @RequestMapping(value = "/listEarmark",method = RequestMethod.GET)
    public Result listEarmark(
            @RequestParam(value = "applyId",required = false) Integer applyId,
            @RequestParam(value = "packageId",required = false) Integer packageId,
            @RequestParam(value = "earMarkId",required = false) Integer earMarkId,
            @RequestParam(value = "earMarkNumber",required = false) Integer earMarkNumber,
            @RequestParam(value = "returnState",required = false) Integer returnState,
            @RequestParam(value = "writeState",required = false) Integer writeState,
            @RequestParam(value = "printState",required = false) Integer printState,
            @RequestParam(value = "startDate",required = false) Date startDate,
            @RequestParam(value = "endDate",required = false) Date endDate,
            @RequestParam(value = "pageSize",required = true) Integer pageSize,
            @RequestParam(value = "pageNum", required = true) Integer pageNo){
        //
        return earmarkService.listEarmark(applyId,packageId,earMarkId,earMarkNumber,returnState,writeState,printState,startDate,endDate,pageSize,pageNo);
    }

    /**
     * @Method      : getEarmark
     * @Description : 下载新耳标号
     * @Params      : [applyId]
     * @Return      : com.huimv.manage.util.Result
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/10/29       
     * @Time        : 22:58
     */
    @RequestMapping(value = "/getEarmark",method = RequestMethod.GET)
    public Result getEarmark(@RequestParam(value = "applyId") int applyId, HttpServletRequest request) throws IOException {
        //
        return earmarkService.getEarmark(applyId);
    }

    /**
     * @Method      : setUpdateDownState
     * @Description : 设置耳标号以下载状态
     * @Params      : [earmarkIDs]
     * @Return      : com.huimv.manage.util.Result
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/10/29       
     * @Time        : 22:59
     */
    @RequestMapping(value = "/setUpdateDownState",method = RequestMethod.GET)
    public Result setUpdateDownState(@RequestParam(value = "earMarkIDs") String earmarkIDs, HttpServletRequest request) throws UnsupportedEncodingException {
        //
        return earmarkService.setUpdateDownState(earmarkIDs);
    }

    /**
     * @Method      : setProduceState
     * @Description : 设置生产状态
     * @Params      : [earmarkIDs]
     * @Return      : com.huimv.manage.util.Result
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/10/29       
     * @Time        : 23:55
     */
    @RequestMapping(value = "/setProduceState",method = RequestMethod.GET)
    public Result setProduceState(@RequestParam(value = "ids") String ids,@RequestParam(value = "state") int state, HttpServletRequest request){
        //
        return earmarkService.setProduceState(ids,state);
    }

    /**
     * @Method      : setWriteState
     * @Description : 设置写入状态
     * @Params      : [ids, state]
     * @Return      : com.huimv.manage.util.Result
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/10/30       
     * @Time        : 0:02
     */
    @RequestMapping(value = "/setWriteState",method = RequestMethod.GET)
    public Result setWriteState(@RequestParam(value = "ids") String ids,@RequestParam(value = "state") int state, HttpServletRequest request){
        //
        return earmarkService.setWriteState(ids,state);
    }

    /**
     * @Method      : setPrintState
     * @Description : 设置打码状态
     * @Params      : [ids, state]
     * @Return      : com.huimv.manage.util.Result
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/10/30       
     * @Time        : 0:03
     */
    @RequestMapping(value = "/setPrintState",method = RequestMethod.GET)
    public Result setPrintState(@RequestParam(value = "ids") String ids,@RequestParam(value = "state") int state, HttpServletRequest request){
        //
        return earmarkService.setPrintState(ids,state);
    }

    /**
     * @Method      : exportEarmarkByApply
     * @Description : 通过任务号导出耳标号（不分页导出）
     * @Params      : [applyId, printState, response]
     * @Return      : void
     *
     * @Author      : ZhuoNing
     * @Date        : 2021/11/9
     * @Time        : 14:37
     */
    @RequestMapping(value = "/exportEarmarkByApply",method = RequestMethod.GET)
    public void exportEarmarkByApply(@RequestParam(value = "applyId",required = true) Integer applyId,
                                     @RequestParam(value = "printState",required = false) Integer printState,
                                     @RequestParam(value = "quantity",required = true) Integer quantity,
                                     HttpServletResponse response) throws IOException {
        log.info("applyId="+applyId);
        log.info("printState="+printState);
        log.info("quantity="+quantity);
        //
//        earmarkService.exportEarmarkByApply(applyId,printState,quantity,response);
        //导出数据到文本文件中
//        earmarkService.exportEarmarkByApplyToTxt(applyId,printState,quantity,response);
        //test1
        earmarkService.exportTest1(applyId,printState,quantity,response);
        //test2
//        earmarkService.exportTest2(applyId,printState,quantity,response);
    }

    /**
     * @Method      : exportPageEarmarkByApply
     * @Description : 通过任务号导出耳标号（分页导出）
     * @Params      : [applyId, printState, quantity, response]
     * @Return      : void
     * 
     * @Author      : ZhuoNing
     * @Date        : 2021/11/10       
     * @Time        : 17:23
     */
    @RequestMapping(value = "/exportPageEarmarkByApply",method = RequestMethod.GET)
    public void exportPageEarmarkByApply(@RequestParam(value = "applyId",required = true) Integer applyId,
                                     @RequestParam(value = "printState",required = false) Integer printState,
                                     @RequestParam(value = "quantity",required = true) Integer quantity,
                                     HttpServletResponse response) throws IOException {
        log.info("applyId="+applyId);
        log.info("printState="+printState);
        log.info("quantity="+quantity);
        //导出数据到excel格式
        earmarkService.exportEarmarkByApply(applyId,printState,quantity,response);
    }

    /**
     * @Method      : exportEarmarkByEarmark
     * @Description : 通过耳标号到处耳标数据
     * @Params      : [ids, printState, quantity, response]
     * @Return      : void
     * 
     * @Author      : ZhuoNingc
     * @Date        : 2021/11/9       
     * @Time        : 19:05
     */
    @RequestMapping(value = "/exportEarmarkByEarmark",method = RequestMethod.GET)
    public void exportEarmarkByEarmark(@RequestParam(value = "ids",required = true) String ids,
                                     @RequestParam(value = "printState",required = false) Integer printState,
                                     @RequestParam(value = "quantity",required = true) Integer quantity,
                                     HttpServletResponse response) throws IOException {
        log.info("ids="+ids);
        log.info("printState="+printState);
        log.info("quantity="+quantity);
        //
        earmarkService.exportEarmarkByEarmark(ids,printState,quantity,response);
    }
}
