package com.unis.base.server.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.CcsbtzjsmxVO;
import com.unis.base.common.vo.CcsstzjsmxVO;
import com.unis.base.server.entity.CcsbtzjsmxDO;
import com.unis.base.server.entity.CcsstzjsmxDO;
import com.unis.base.server.service.CcsbtzjsmxService;
import com.unis.base.server.service.CcsstzjsmxService;
import com.unis.common.utils.CurrentUtil;
import com.unis.common.utils.ResponseWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Classname CcsstzjsmxController
 * @Description 仓储设施投资建设明细
 * @Date 2024/1/2 10:49
 * @Created by rising123sun
 */
@RestController
@Api(value = "仓储设施投资建设明细", tags = "仓储设施投资建设明细")
@Slf4j
@RequestMapping("/ccsstzjsmx")
public class CcsstzjsmxController {
    @Resource
    private CcsstzjsmxService ccsstzjsmxService;

    @Value("${excel.template-directory-path}")
    private String EXCEL_TEMPLATE_PATH;

    // 新增
    @PostMapping("/add")
    @ApiOperation(value = "新增", notes = "新增")
    public ResponseWrapper add(@RequestBody CcsstzjsmxDO ccsstzjsmxDO) {
        try {
            return ccsstzjsmxService.add(ccsstzjsmxDO);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseWrapper.error("新增失败!");
        }
    }

    // 修改
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改")
    public ResponseWrapper update(@RequestBody CcsstzjsmxDO ccsstzjsmxDO) {
        try {
            return ccsstzjsmxService.update(ccsstzjsmxDO);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseWrapper.error("修改失败!");
        }
    }

    // 根据给定年份以及组织机构编码查询上一年的数据（新增或修改时打开页面的时候使用，直接传上一年的年份）
    @PostMapping("/queryLastYearData")
    @ApiOperation(value = "根据给定年份以及组织机构编码查询上一年的数据（新增或修改时打开页面的时候使用，直接传上一年的年份）", notes = "根据给定年份以及组织机构编码查询上一年的数据（新增或修改时打开页面的时候使用，直接传上一年的年份）")
    public ResponseWrapper queryLastYearData(@RequestParam(value = "sbnf") String sbnf,
                                             @RequestParam(value = "orgCode") String orgCode) {
        try {
            CcsstzjsmxDO ccsstzjsmxDO = ccsstzjsmxService.queryLastYearData(sbnf, orgCode);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), ccsstzjsmxDO);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseWrapper.error("查询失败!");
        }
    }

    // 分页列表查询
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @PostMapping("/queryPageList")
    public ResponseWrapper queryPageList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestBody CcsstzjsmxVO ccsstzjsmxVO) {
        try {
            PageInfo<CcsstzjsmxVO> pageInfos = ccsstzjsmxService.queryPageList(pageSize, pageNum, ccsstzjsmxVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageInfos);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseWrapper.error("获取信息列表失败");
        }
    }

    // 详情查询
    @GetMapping("/queryDetail/{id}")
    @ApiOperation(value = "详情查询", notes = "详情查询")
    public ResponseWrapper queryDetail(@PathVariable String id) {
        try {
            return ccsstzjsmxService.queryDetail(id);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseWrapper.error("获取信息失败");
        }
    }

    // 仓储设施投资建设明细统计表管理端或购销公司查看
    @PostMapping("/queryHzForGlOrGxgs")
    @ApiOperation(value = "仓储设施投资建设明细统计表管理端或购销公司查看", notes = "仓储设施投资建设明细统计表管理端或购销公司查看")
    public ResponseWrapper queryHzForGlOrGxgs(@RequestBody CcsstzjsmxVO ccsstzjsmxVO) {
        try {
            Map<String, CcsstzjsmxDO> result = ccsstzjsmxService.queryHzForGlOrGxgs(ccsstzjsmxVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), result);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseWrapper.error("获取信息列表失败");
        }
    }

    // 仓储设施投资建设明细统计表管理端或购销公司导出
    @PostMapping("/exportHzForGlOrGxgs")
    @ApiOperation(value = "仓储设施投资建设明细统计表管理端或购销公司导出", notes = "仓储设施投资建设明细统计表管理端或购销公司导出")
    public ResponseWrapper exportHzForGlOrGxgs(@RequestBody CcsstzjsmxVO ccsstzjsmxVO, HttpServletResponse response) {
        try {
            Map<String, CcsstzjsmxDO> result = ccsstzjsmxService.queryHzForGlOrGxgs(ccsstzjsmxVO);
            response.setContentType("application/msexcel");
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("仓储设施投资建设统计表.xlsx", "utf-8"));
            String templateName = "ccsstzjsmxtjb.xlsx";
            URI resolve = URI.create(EXCEL_TEMPLATE_PATH).resolve(templateName);
            try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(resolve.toString()).build()) {
                WriteSheet writeSheet = EasyExcel.writerSheet().build();
                FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
                excelWriter.fill(result, fillConfig, writeSheet);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "导出成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseWrapper.error("导出失败");
        }
    }

    // 仓储设施投资建设明细表区粮办或购销公司查看
    @PostMapping("/queryMxForQlbOrGxgs")
    @ApiOperation(value = "仓储设施投资建设明细表区粮办或购销公司查看", notes = "仓储设施投资建设明细表区粮办或购销公司查看")
    public ResponseWrapper queryMxForQlbOrGxgs(@RequestBody CcsstzjsmxVO ccsstzjsmxVO) {
        try {
            List<CcsstzjsmxDO> result = ccsstzjsmxService.queryMxForQlbOrGxgs(ccsstzjsmxVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), result);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseWrapper.error("获取信息列表失败");
        }
    }

    // 仓储设施投资建设明细表市局查看
    @PostMapping("/queryMxForSj")
    @ApiOperation(value = "仓储设施投资建设明细表市局查看", notes = "仓储设施投资建设明细表市局查看")
    public ResponseWrapper queryMxForSj(@RequestBody CcsstzjsmxVO ccsstzjsmxVO) {
        try {
            Map<String, List<CcsstzjsmxDO>> result = ccsstzjsmxService.queryMxForSj(ccsstzjsmxVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), result);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseWrapper.error("获取信息列表失败");
        }
    }


}
