package com.tofly.feesapi.sfgl.controller;

import com.tofly.feesapi.common.Excel.ExcleFileUtil;
import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.sfgl.entity.ZnbCsjsLog;
import com.tofly.feesapi.sfgl.entity.dto.ZnbCsjsFilter;
import com.tofly.feesapi.sfgl.entity.dto.ZnbCsjsListFilter;
import com.tofly.feesapi.sfgl.entity.vo.ZnbCsjsSelect;
import com.tofly.feesapi.sfgl.service.IZnbCsjsLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/api/sfgl/znbCsjsLog")
@Api(tags = "智能表结算数据控制")
@Slf4j
public class ZnbCsjsLogController {

    @Autowired
    private IZnbCsjsLogService znbCsjsLogService;

    @GetMapping("/page")
    @ApiOperation(value = "智能表抄表统计列表")
    public ResultRespone<PageResult<ZnbCsjsSelect>> getPage(@ModelAttribute PageDto pageDto, @ModelAttribute ZnbCsjsFilter filter) {
        return ResultRespone.success(znbCsjsLogService.getPage(pageDto, filter));
    }

    @GetMapping("/yhysmx")
    @ApiOperation(value = "智能表用户用水明细")
    public ResultRespone<PageResult<ZnbCsjsLog>> getYhysmxList(@ModelAttribute PageDto pageDto, @ModelAttribute ZnbCsjsListFilter filter) {

        return ResultRespone.success(znbCsjsLogService.getYhysmxList2(pageDto, filter));
    }

    @GetMapping("/exportcsmx")
    @ApiOperation(value = "export导出-导出抄收结算",tags = {"智能表"})
    public void export(HttpServletResponse response, String sscbpq, String sscbb, String ksrq, String jsrq, String exportFields) throws IOException {
        ZnbCsjsListFilter csjsFilter = new ZnbCsjsListFilter();
        csjsFilter.setSscbb(sscbb);
        csjsFilter.setSscbpq(sscbpq);

        if (!StringUtils.isEmpty(ksrq) && !StringUtils.isEmpty(jsrq)) {
            try {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 定义日期格式
                csjsFilter.setStartDate(formatter.parse(ksrq));
                csjsFilter.setEndDate(formatter.parse(jsrq));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        List<ZnbCsjsLog> znbCsjsLogsList = znbCsjsLogService.getYhysmxList(csjsFilter);
        ExcleFileUtil.exportExcel(response, znbCsjsLogsList, ZnbCsjsLog.class, "智能表抄收结算.xlsx", exportFields);
    }

    @GetMapping("/znbzdsc")
    @ApiOperation(value = "智能表账单生成测试",tags = {"智能表"})
    public void test() throws IOException {
        znbCsjsLogService.znbZdsc("202503");
    }
}
