/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.unis.basic.info.modular.reportpricemonitor.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.unis.basic.info.modular.pricemonitor.service.PriceMonitorService;
import com.unis.basic.info.modular.unReporCity.controller.UnReporCityController;
import com.unis.basic.info.modular.unReporCity.entity.UnReporCity;
import com.unis.common.util.CommonTimeFormatUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.unis.common.annotation.CommonLog;
import com.unis.common.pojo.CommonResult;
import com.unis.common.pojo.CommonValidList;
import com.unis.basic.info.modular.reportpricemonitor.entity.ReportPriceMonitor;
import com.unis.basic.info.modular.reportpricemonitor.param.ReportPriceMonitorAddParam;
import com.unis.basic.info.modular.reportpricemonitor.param.ReportPriceMonitorEditParam;
import com.unis.basic.info.modular.reportpricemonitor.param.ReportPriceMonitorIdParam;
import com.unis.basic.info.modular.reportpricemonitor.param.ReportPriceMonitorPageParam;
import com.unis.basic.info.modular.reportpricemonitor.service.ReportPriceMonitorService;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

/**
 * 漏报统计站点控制器
 *
 * @author shine
 * @date  2024/04/08 20:24
 */
@Api(tags = "漏报统计站点控制器")
@ApiSupport(author = "UNIS_TEAM", order = 1)
@RestController
@Validated
public class ReportPriceMonitorController {

    @Resource
    private ReportPriceMonitorService reportPriceMonitorService;

    @Resource
    private UnReporCityController unReporCityController;

    @Resource
    private PriceMonitorService priceMonitorService;



    /**
     * 获取漏报统计站点分页
     *
     * @author shine
     * @date  2024/04/08 20:24
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取漏报统计站点分页")
    //@SaCheckPermission("/basic.info/reportpricemonitor/page")
    @GetMapping("/basic.info/reportpricemonitor/page")
    public CommonResult<Page<ReportPriceMonitor>> page(ReportPriceMonitorPageParam reportPriceMonitorPageParam) {
        Page<ReportPriceMonitor> pageData = reportPriceMonitorService.page(reportPriceMonitorPageParam);
        List<ReportPriceMonitor> list = pageData.getRecords();
        for (ReportPriceMonitor ReportPriceMonitor:list){
            String stateName = priceMonitorService.getById(ReportPriceMonitor.getPriceMonitorId()).getName();
            ReportPriceMonitor.setStateName(stateName);
        }
        return CommonResult.data(pageData);
    }

    /**
     * 添加漏报统计站点
     *
     * @author shine
     * @date  2024/04/08 20:24
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("添加漏报统计站点")
    @CommonLog("添加漏报统计站点")
    //@SaCheckPermission("/basic.info/reportpricemonitor/add")
    @PostMapping("/basic.info/reportpricemonitor/add")
    public CommonResult<String> add(@RequestBody @Valid ReportPriceMonitorAddParam reportPriceMonitorAddParam) {
        reportPriceMonitorService.add(reportPriceMonitorAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑漏报统计站点
     *
     * @author shine
     * @date  2024/04/08 20:24
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("编辑漏报统计站点")
    @CommonLog("编辑漏报统计站点")
    //@SaCheckPermission("/basic.info/reportpricemonitor/edit")
    @PostMapping("/basic.info/reportpricemonitor/edit")
    public CommonResult<String> edit(@RequestBody @Valid ReportPriceMonitorEditParam reportPriceMonitorEditParam) {
        reportPriceMonitorService.edit(reportPriceMonitorEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除漏报统计站点
     *
     * @author shine
     * @date  2024/04/08 20:24
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("删除漏报统计站点")
    @CommonLog("删除漏报统计站点")
    //@SaCheckPermission("/basic.info/reportpricemonitor/delete")
    @PostMapping("/basic.info/reportpricemonitor/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   CommonValidList<ReportPriceMonitorIdParam> reportPriceMonitorIdParamList) {
        reportPriceMonitorService.delete(reportPriceMonitorIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取漏报统计站点详情
     *
     * @author shine
     * @date  2024/04/08 20:24
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取漏报统计站点详情")
    //@SaCheckPermission("/basic.info/reportpricemonitor/detail")
    @GetMapping("/basic.info/reportpricemonitor/detail")
    public CommonResult<ReportPriceMonitor> detail(@Valid ReportPriceMonitorIdParam reportPriceMonitorIdParam) {
        return CommonResult.data(reportPriceMonitorService.detail(reportPriceMonitorIdParam));
    }



    @ApiOperationSupport(order = 6)
    @ApiOperation("站点导出")
    //@SaCheckPermission("/basic.info/reportpricemonitor/exportByEntity")
    @PostMapping("/basic.info/reportpricemonitor/exportByEntity")
    public void exportByEntity(
            @RequestParam(value = "unReportCityId",required = true) String unReportCityId,
            @RequestParam(value = "priceMonitorId",required = true) String priceMonitorId,
            @RequestParam(value = "sysOrgName",required = true) String sysOrgName
            , HttpServletResponse response) {

        try {
            // 查询list
            ReportPriceMonitor reportPriceMonitor =new ReportPriceMonitor();

            if(priceMonitorId != null && priceMonitorId != ""){
                reportPriceMonitor.setPriceMonitorId(Long.valueOf(priceMonitorId));
            }

            if(unReportCityId != null && unReportCityId != ""){
                reportPriceMonitor.setUnReportCityId(Long.valueOf(unReportCityId));
            }

            List<Map<String, Object>> data= reportPriceMonitorService.exportByEntityResult(reportPriceMonitor,sysOrgName);

            LinkedHashMap<String, String> columnName = new LinkedHashMap<>();

            columnName.put("number","序号");
            columnName.put("sysOrgName","市州");
            columnName.put("siteName","站点名称");
            columnName.put("un_report_count","漏报次数");
            columnName.put("false_report_count","错报次数");
            columnName.put("un_self_price_monitor_count","未在实地采集次数");

            // 输出Excel文件

            response.reset();
            String fileName = "漏报站点统计表";
            response.setHeader("content-disposition", "attachment;filename="
                    + new String(CommonTimeFormatUtil.getDate()) + ".xls");

            response.setContentType("application/vnd.ms-excel;charset=gb2312");
            ServletOutputStream output = response.getOutputStream();
            unReporCityController.pojo3Excel(data, output, columnName,fileName);

            output.close();

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

}
