package com.jeeplus.finance.projectRecords.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.aop.demo.annotation.DemoMode;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.finance.invoice.service.dto.CwFinanceInvoiceDetailDTO;
import com.jeeplus.finance.invoice.util.EasyPoiUtil;
import com.jeeplus.finance.projectRecords.service.CwFillingbatchService;
import com.jeeplus.finance.projectRecords.service.dto.CwFillingbatchInfoDTO;
import com.jeeplus.finance.projectRecords.service.dto.CwFillingbatchProjectDTO;
import com.jeeplus.finance.projectRecords.service.dto.CwProjectRecordsDTO;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@Api("项目批量归档信息")
@RequestMapping(value = "/cwFillingbatch")
public class CwFillingbatchController {
    @Resource
    private CwFillingbatchService cwFillingbatchService;



    /**
     * 列表查询
     * @param info
     * @return
     */
    @ApiOperation(value = "列表查询")
    @GetMapping("/list")
    public ResponseEntity<IPage<CwFillingbatchInfoDTO>> list(CwFillingbatchInfoDTO info, Page<CwFillingbatchInfoDTO> page) {
        IPage<CwFillingbatchInfoDTO> list = cwFillingbatchService.list(info,page);
        return ResponseEntity.ok(list);
    }

    /**
     * 删除批量归档信息
     * @param ids
     * @return
     */
    @ApiLog(value = "删除批量归档信息")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        return cwFillingbatchService.deleteByIds(ids);
    }


    /**
     * 查询批量归档详情
     * @param id
     * @return
     */
    @ApiLog("查询批量归档详情")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        CwFillingbatchInfoDTO cwFillingbatchInfoDTO = cwFillingbatchService.queryById ( id );
        return ResponseEntity.ok (cwFillingbatchInfoDTO);
    }

    /**
     *当前登录人未归档的B类项目,归档已完成但项目被驳回的B类项目
     * @param info
     * @param page
     * @return
     */
    @ApiOperation(value = "当前登录人未归档的B类项目,归档已完成但项目被驳回的B类项目")
    @GetMapping("/getProjectList")
    public ResponseEntity<IPage<CwFillingbatchProjectDTO>> getProjectList(CwFillingbatchProjectDTO info, Page<CwFillingbatchProjectDTO> page) {
        IPage<CwFillingbatchProjectDTO> list = cwFillingbatchService.getProjectList(info,page);
        return ResponseEntity.ok(list);
    }

    /**
     * 保存财务项目批量归档信息-流程
     * @param info
     * @return
     */
    @ApiLog(value = "保存财务项目批量归档信息-流程", type = LogTypeEnum.SAVE)
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody CwFillingbatchInfoDTO info) throws Exception {
        String id = cwFillingbatchService.saveForm(info);
        return ResponseUtil.newInstance().add("businessTable", "cw_fillingbatch_info").add("businessId", id).ok ("操作成功");
    }

    /**
     * 根据id修改状态值status
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据id修改状态值status")
    @PostMapping(value = "/updateStatusById")
    public ResponseEntity<String> updateStatusById(@RequestBody CwFillingbatchInfoDTO dto) {
        String s = cwFillingbatchService.updateStatusById(dto);
        return ResponseEntity.ok(s);
    }

    /**
     * 下载项目归档导入模板
     *
     * @param response
     * @return
     */
    @GetMapping("/template")
    @ApiOperation(value = "下载模板")
    public void importFileTemplate(HttpServletResponse response, HttpServletRequest request) {
        try {
            InputStream inputStream = this.getClass().getResourceAsStream("/dot/批量归档导入模板.xlsx");
            //强制下载不打开
            response.setContentType("application/force-download");
            OutputStream out = response.getOutputStream();
            //使用URLEncoder来防止文件名乱码或者读取错误
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("批量归档导入模板.xlsx", "UTF-8"));
            int b = 0;
            byte[] buffer = new byte[1000000];
            while (b != -1) {
                b = inputStream.read(buffer);
                if (b != -1) out.write(buffer, 0, b);
            }
            inputStream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入批量归档数据
     *
     * @return
     */
    @DemoMode
    @PostMapping("/importDetail")
    @ApiLog(value = "导入批量归档数据excel", type = LogTypeEnum.IMPORT)
    public ResponseEntity importDetail(MultipartFile file, HttpServletRequest request) throws IOException {

        ArrayList<CwFillingbatchProjectDTO> arrayList = new ArrayList<>();
        HashMap<String,String> hashMap = new HashMap<>();

        List<CwFillingbatchProjectDTO> listA = new ArrayList<>();
        //获取sheet
        listA = EasyPoiUtil.importExcel(file, 1, 1, CwFillingbatchProjectDTO.class);
        //导入前检测数据
        String resultA = cwFillingbatchService.importDecide(listA, arrayList, hashMap);
        if(StringUtils.isNotBlank(resultA)){
            //有返回值，说明导入的数据不正确。向用户抛提示
            return ResponseEntity.badRequest().body  (resultA);
        }

        return ResponseEntity.ok(arrayList);
    }









}
