/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.xrtymis.custom.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.xrtymis.custom.dto.TfYwpnGatherrecorDataDTO;
import com.tofly.xrtymis.custom.service.TfYwpnGatherrecordService;
import com.tofly.xrtymis.custom.service.TfYwpnPipenodeService;
import com.tofly.xrtymis.custom.vo.GatherRecordVO;
import com.tofly.xrtymis.custom.vo.TfYwpnGatherrecordVO;
import com.tofly.xrtymis.em.Constant;
import com.tofly.xrtymis.em.ExcelExportFileTypeEnum;
import com.tofly.xrtymis.strategy.ExcelStrategyContext;
import com.tofly.xrtymis.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 数据采集记录表：TF_YWPN_GATHERRECORD_W
 *
 * @author fcl
 * @date Fri Apr 16 00:00:00 CST 2021
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tfywpngatherrecord")
@Api(tags="数据采集记录表：TF_YWPN_GATHERRECORD_W接口")
public class TfYwpnGatherrecordController {

    private final TfYwpnGatherrecordService tfYwpnGatherrecordService;
    private final ExcelStrategyContext excelStrategyContext;
    private final TfYwpnPipenodeService tfYwpnPipenodeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param dto 查询条件
     * @return
     */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams(
            @ApiImplicitParam
    )
    public ResultRespone getTfYwpnGatherrecordPage(Page page, @ModelAttribute TfYwpnGatherrecorDataDTO dto) {
        Page<TfYwpnGatherrecordVO> queryPage = tfYwpnGatherrecordService.getPageVO(page,dto);
        return ResultRespone.success(queryPage);
    }

    /**
     * 查询当前用户未提交的采集信息
     *
     */
    @GetMapping("/getNoAudit" )
    @ApiOperation(value = "查询当前用户未提交的采集信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "types", value = "查询多个采集类型（gatherType），多个type以逗号隔开", required = true, dataType = "String")
    })
    public ResultRespone getNoAudit(String types) {
        List<GatherRecordVO> list =tfYwpnGatherrecordService.getNoAudit(types);
        return ResultRespone.success(list);
    }

    /**
     * 通过id查询数据采集记录表：TF_YWPN_GATHERRECORD_W
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
        return ResultRespone.success(tfYwpnGatherrecordService.getById(id));
    }

    /**
     * 提交采集记录数据：TF_YWPN_GATHERRECORD_W
     * @return ResultRespone
     */
    @GetMapping("/submit")
    @ToFlyAppLog(title="提交采集记录数据：TF_YWPN_GATHERRECORD_W" )
    @ApiOperation(value = "提交采集记录数据：TF_YWPN_GATHERRECORD_W")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids" ,value = "提交的记录id，多个id以逗号隔开",required = true, dataType = "String"),
            @ApiImplicitParam(name = "reviewId" ,value = "审核人id",required = false, dataType = "String")
    })
    public ResultRespone submit(@RequestParam("ids") String ids,@RequestParam(value = "reviewId",required = false)String reviewId) {
        //当改变状态的时候
        return ResultRespone.success(tfYwpnGatherrecordService.submitRecord(ids,reviewId));
    }

    /**
     * 通过id删除数据采集记录表：TF_YWPN_GATHERRECORD_W
     * @param id id
     * @return ResultRespone
     */
    @DeleteMapping("/{id}" )
    @ToFlyAppLog(title="通过id删除数据采集记录表：TF_YWPN_GATHERRECORD_W" )
    @ApiOperation(value = "通过ID删除数据采集记录表：TF_YWPN_GATHERRECORD_W")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone removeById(@PathVariable Long id) {
        return tfYwpnGatherrecordService.deleteById(id);
    }


    @ApiOperation(value = "采集数据分类并以EXCEL导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateServiceType", value = "选择导出的类型策略: 管线采集(609)-exportPipeService , 管点采集(629)-exportNodeService , " +
                    "水表采集(610)-exportMeterService, 监测站采集(611)-exportStationService , 阀门采集(612)-exportValveService , 非控制阀采集(613)-exportNoValveService" +
                    "消防栓采集(614)-exportHydrantService , 检查井采集(615)-exportManholeService ", required = true, dataType = "String"),
            @ApiImplicitParam(name = "templateFlag", value = "templateFlag 默认为1", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dto", value = "这里为记录页面返回的gatherId(采集数据ID),多个以','分隔 ", required = true, dataType = "String")
    })
    @GetMapping("/export")
    public void exportHistory(HttpServletResponse response, String templateServiceType, String templateFlag, String dto) {
        // 根据templateServiceType,templateFlag获取对应模板数据
        ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum(templateServiceType, templateFlag);
        if (null == excelExportFileTypeEnum) {
            return;
        }
        // 从模型类型枚举根据templateServiceType获取策略对应模板数据
        List<Map<String, Object>> dataList = excelStrategyContext.getExportData(templateServiceType, templateFlag, dto);
        ExcelUtil.export(dataList.get(0), excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
    }
    @ApiOperation(value = "根据管线ids导出管点数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateFlag", value = "templateFlag 默认为1", required = true, dataType = "String"),
            @ApiImplicitParam(name = "ids", value = "这里为记录页面返回的gatherId(采集数据ID),多个以','分隔 ", required = true, dataType = "String")
    })
    @GetMapping("/exportPipeNode")
    public Object exportPipeNode(HttpServletResponse response,String templateFlag, String ids){
//        if(StringUtils.isEmpty(ids)){
//            return ResultRespone.failed("ids 不能为空");
//        }
//        List<String> strs = Arrays.asList(ids.split(","));
//        List<Long> pids = strs.stream().map(st->Long.parseLong(st)).collect(Collectors.toList());
//        List<TfYwpnPipenode> nodes = tfYwpnPipenodeService.getPipenode(pids);
//
//        if(nodes!=null&&!nodes.isEmpty()) {
//            ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum("exportPipeToNodeService", templateFlag);
//            if (null == excelExportFileTypeEnum) {
//                return null;
//            }
//            String resultListToString = JSONArray.toJSONStringWithDateFormat(nodes,"yyyy-MM-dd HH:mm:ss");
//            List<Map<String, Object>> dataList = excelStrategyContext.getExportData("exportPipeToNodeService", templateFlag, resultListToString);
//            ExcelUtil.export(dataList.get(0), excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
//        }
        return ResultRespone.success("");

    }




}
