package com.ho.filecenter.controller;


import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.ho.business.vo.req.point.PointReq;
import com.ho.business.vo.resp.point.PointRespVO;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.util.BigDecimalUtil;
import com.ho.common.tools.util.EasyExcelUtil;
import com.ho.common.tools.util.PageUtils;
import com.ho.filecenter.constant.FileConstant;
import com.ho.filecenter.entity.StandingBook;
import com.ho.filecenter.service.StandingBookService;
import com.ho.filecenter.vo.StandingBookVo;
import com.ho.filecenter.vo.standingbook.StandingBookImport;
import com.ho.filecenter.vo.standingbook.StandingBookOut;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.ho.common.tools.util.PageResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author gyan
 * @DateTime: 2023/3/15 14:19
 */

@RestController
@RequestMapping(ContextConstant.MEDIA_CONTEXT + "file")
@Api(tags = "多媒体-资产管理")
@Slf4j
public class StandingBookController {

    @Autowired
    StandingBookService standingBookService;


    // 查
    @PostMapping("/queryList")
    @ApiOperation(value = "查询资产管理列表")

    public DataResult<PageResult<List<StandingBook>>> queryList(@RequestBody @Valid StandingBookVo vo) {
        List<StandingBook> standingBookList = standingBookService.queryList(vo);

        //java内存分页
        PageResult pageResult = new PageResult<>();
        if (!standingBookList.isEmpty()) {
            List list = PageUtils.dealList(standingBookList, vo.getPageNum(), vo.getPageSize());
            pageResult = PageUtils.getPageResult(new PageInfo<>(list));
            pageResult.setTotalRows(standingBookList.size());
            pageResult.setTotalPages(standingBookList.size() / vo.getPageSize() + 1);
        } else {
            pageResult = PageUtils.getPageResult(new PageInfo<>(new ArrayList<>()));
        }
        return DataResult.success(pageResult);
    }

    // 改(都可以修改)

    @PostMapping("update")
    @ApiOperation(value = "资产管理列表修改")
    public DataResult update(@RequestBody @Valid StandingBook vo) {
        standingBookService.update(vo);
        return DataResult.success();
    }

    // 删(根据id删除)
    @PostMapping("/delete")
    @ApiOperation(value = "根据id删除")
    public DataResult deleteById(@RequestBody @Valid StandingBookVo vo) {
        standingBookService.deleteById(vo);
        return DataResult.success();
    }

    @PostMapping("/import")
    @ApiOperation("导入资产台账")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "站id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "file", value = "导入的Excel文件", dataType = "File", paramType = "form", required = true)
    })

    public DataResult importCustomer(@RequestParam(value = "stationId") Integer stationId,
                                     @RequestParam(value = "file") MultipartFile file) {
        List<StandingBookImport> list = null;
        try {
            InputStream inputStream = file.getInputStream();
            list = EasyExcel.read(inputStream)
                    .headRowNumber(0).head(StandingBookImport.class).sheet().doReadSync();
        } catch (Exception e) {
            log.error(e.getMessage());
            //请核对excel格式
            throw new BusinessException(BaseResponseCode.PLEASE_CHECK_THE_EXCEL_FORMAT);
        }
        if (list.isEmpty()) {
            throw new BusinessException(BaseResponseCode.EXCEL_DATA_TABLE_EMPTY);
        }
        standingBookService.importCustomer(stationId, list);
        return DataResult.success();
    }


    @PostMapping("/exportExcel")
    @ApiOperation("导出资产台账")
    public void exportExcel(HttpServletResponse response) {

        String fileName = "资产台账模板";
        String sheetName = "资产台账模板";
        try {
            EasyExcelUtil.writeExcel(response, new ArrayList<>(), fileName, sheetName, StandingBookOut.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
