package com.dhcc.finance.main.controller;

import com.dhcc.common.constant.SysConstants;
import com.dhcc.common.constant.SysStatusCode;
import com.dhcc.common.model.CurrencyMeta;
import com.dhcc.common.model.response.RestfulResponse;
import com.dhcc.common.util.ExcelUtils;
import com.dhcc.common.util.FileUtil;
import com.dhcc.finance.constant.MsConfigConsts;
import com.dhcc.finance.main.domain.CwBdManualSerial;
import com.dhcc.finance.main.model.CwBdManualSerialWeb;
import com.dhcc.finance.main.model.CwGlVoucherWeb;
import com.dhcc.finance.main.service.CwBdManualSerialService;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = MsConfigConsts.MS_API_PREFIX + CwBdManualSerialController.API_PREFIX,
        produces = {MediaType.APPLICATION_JSON_UTF8_VALUE }, consumes = { MediaType.APPLICATION_JSON_UTF8_VALUE })
@Api(value = "批量凭证controller", tags = { "批量管理凭证操作接口" })
public class CwBdManualSerialController {
    //批量导入凭证的前缀
    public static final String API_PREFIX = "/cw_bd_manual_serial";

    @Autowired
    CwBdManualSerialService cwBdManualSerialService;

    //获取简略四项信息
    @ApiOperation(value = "获取所需凭证信息", notes = "", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.ALL_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", required = true, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "navigatePages", value = "需要连续显示的页码数量", required = true, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "voucherType", value = "凭证类型", required = false, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "importDate", value = "导入时间", required = false, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "createUser", value = "录入人", required = false, dataType = "String", paramType="query"),
//            @ApiImplicitParam(name = "batchNo", value = "凭证批次号", required = false, dataType = "String", paramType="query"),
//            @ApiImplicitParam(name = "subjcode", value = "科目编号", required = false, dataType = "String", paramType="query"),
//            @ApiImplicitParam(name = "projectName", value = "子项目名称", required = false, dataType = "String", paramType="query")
    })
    @RequestMapping(value = { "/selectAll" }, method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
    public RestfulResponse<PageInfo<CwBdManualSerial>, CurrencyMeta> selectAll(
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize,
            @RequestParam("navigatePages") int navigatePages,
            @RequestParam(value="voucherType",required=false) String voucherType,
            @RequestParam(value="importDate",required=false) String importDate,
            @RequestParam(value="createUser",required=false) String createUser) throws ParseException {
        return cwBdManualSerialService.selectAll(pageNum,pageSize,navigatePages,voucherType,importDate,createUser);
    }

    //获取详情
    @ApiOperation(value = "获取同批次凭证信息详情", notes = "", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.ALL_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", required = true, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "navigatePages", value = "需要连续显示的页码数量", required = true, dataType = "String", paramType="query"),
//            @ApiImplicitParam(name = "voucherType", value = "凭证类型", required = false, dataType = "String", paramType="query"),
//            @ApiImplicitParam(name = "importDate", value = "导入时间", required = false, dataType = "String", paramType="query"),
//            @ApiImplicitParam(name = "createUser", value = "录入人", required = false, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "batchNo", value = "凭证批次号", required = false, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "subjcode", value = "科目编号", required = false, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "projectCode", value = "项目编号", required = false, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "subProjectCode", value = "子项目编号", required = false, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "pkVoucher", value = "凭证编号", required = false, dataType = "String", paramType="query")
    })
    @RequestMapping(value = { "/selectDataDetail" }, method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
    public RestfulResponse<PageInfo<CwBdManualSerial>, CurrencyMeta> selectDataDetail(
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize,
            @RequestParam("navigatePages") int navigatePages,
//            @RequestParam(value="voucherType",required=false) String voucherType,
//            @RequestParam(value="importDate",required=false) String importDate,
//            @RequestParam(value="createUser",required=false) String createUser,
            @RequestParam(value="batchNo",required=false) String batchNo,
            @RequestParam(value="subjcode",required=false) String subjcode,
            @RequestParam(value="projectCode",required=false) String projectCode,
            @RequestParam(value="subProjectCode",required=false) String subProjectCode,
            @RequestParam(value="pkVoucher",required=false) String pkVoucher) throws ParseException {
        return cwBdManualSerialService.selectDataDetail(pageNum,pageSize,navigatePages,batchNo,subjcode,projectCode,subProjectCode,pkVoucher);
    }

    //导入且查看文件
    @ApiOperation(value = "导入批量凭证信息", notes = "", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @RequestMapping(value = "/importBatchReportInfo", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public RestfulResponse<Object, CurrencyMeta> importBatchReportInfo(@RequestBody MultipartFile file) throws Exception {
        return cwBdManualSerialService.importBatchReportInfo(file);
    }

    //保存导入的文件
    @ApiOperation(value = "保存批量凭证信息", notes = "", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(name = "list", value = "", required = true, dataType = "CwBdManualSerialWeb")
    @RequestMapping(value = "/importBatchVoucherInfo", method = RequestMethod.POST)
    public RestfulResponse<Object, CurrencyMeta> importBatchVoucherInfo(@RequestBody List<CwBdManualSerialWeb> list){
        return cwBdManualSerialService.importBatchVoucherInfo(list);
    }

//    //删除查看列表文件（未用）
//    @ApiOperation(value = "删除导入显示的文件表", notes = "", produces = MediaType.ALL_VALUE, consumes = MediaType.ALL_VALUE)
//    @RequestMapping(value = "/deleteImportReportInfo", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
//    public RestfulResponse<Object, CurrencyMeta> deleteImportReportInfo(){
//        return cwBdManualSerialService.deleteImportReportInfo();
//    }

    /**
     * 功能描述：查询制单详情
     *
     * @param pkVoucher
     * @return RestfulResponse<CwGlVoucherWeb, CurrencyMeta>
     * @author dml
     * @date 2019年4月15日 下午1:48:08
     * @修改日志：
     */
    @ApiOperation(value = "查询凭证详情", notes = "查询凭证详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.ALL_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkVoucher", value = "凭证主键", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "useState", value = "使用状态", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = {"/selectPLVoucherDetil"}, method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
    public RestfulResponse<CwGlVoucherWeb, CurrencyMeta> selectOrderMakingDetil(
            @RequestParam(value = "pkVoucher", required = true) String pkVoucher, @RequestParam(value = "useState", required = true) String useState) {
        return cwBdManualSerialService.selectPLVoucherDetil(pkVoucher, useState);
    }

}
