package com.tofly.watermeter.custom.controller.meterRead;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.watermeter.custom.dto.WaterAuditDto;
import com.tofly.watermeter.custom.entity.dto.UpdateClientDto;
import com.tofly.watermeter.custom.entity.meterRead.AppRead;
import com.tofly.watermeter.custom.entity.meterRead.ReadRecord;
import com.tofly.watermeter.custom.entity.meterRead.ReadSearch;
import com.tofly.watermeter.custom.entity.meterRead.readstatic.AuditMap;
import com.tofly.watermeter.custom.entity.meterRead.readstatic.ReadCollect;
import com.tofly.watermeter.custom.service.ReadDataCheckService;
import com.tofly.watermeter.custom.service.ReadDataCheckThreeService;
import com.tofly.watermeter.entity.Yhforeverinfo;
import com.tofly.watermeter.service.CbqydwaterService;
import com.tofly.watermeter.service.QfcbplandetailsService;
import com.tofly.watermeter.service.YhforeverinfoService;
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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @author FlashWJL
 * @date 2021/11/9 14:10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/appRead")
@Api(tags = "抄表管理(APP)")
public class AppReadController {

    @Autowired
    private QfcbplandetailsService qfcbplandetailsService;
    @Autowired
    private ReadDataCheckThreeService checkThreeService;
    @Autowired
    private CbqydwaterService cbqydwaterService;
    @Autowired
    private ReadDataCheckService readDataCheckService;
    @Autowired
    private YhforeverinfoService yhforeverinfoService;

    @GetMapping("/downloadUserInfo")
    @ApiOperation("下载客户用户码信息信息")
    public ResultRespone downloadUserInfo() {
        return ResultRespone.success(qfcbplandetailsService.downloadUserInfo());
    }

    @ApiOperation(value = "抄表计划--区域任务汇总")
    @GetMapping("/getQyCollect")
    public ResultRespone getQyCollect(String isGeneral) {
        return ResultRespone.success(qfcbplandetailsService.getQyCollect(isGeneral));
    }

    @ApiOperation(value = "抄表计划--区域段任务汇总")
    @GetMapping("/qydPlanCollect")
    public ResultRespone qydPlanCollect(String isGeneral, String qybm) {
        return ResultRespone.success(qfcbplandetailsService.qydPlanCollect(isGeneral, qybm));
    }

    @ApiOperation(value = "app汇总统计")
    @GetMapping("/appCollect")
    public ResultRespone appCollect(String isGeneral, String yearMonth) {
        return ResultRespone.success(qfcbplandetailsService.appCollect(isGeneral, yearMonth));
    }

    @ApiOperation(value = "抄表计划--app下载抄表数据")
    @GetMapping("/appDwonData")
    public ResultRespone<IPage<AppRead>> appDwonData(Page page, String isGeneral,String type,String zone) {
        return ResultRespone.success(qfcbplandetailsService.appDwonData(page, isGeneral,type,zone));
    }

    @GetMapping("/getUserZone")
    @ApiOperation("获取抄表用户抄表区段")
    public ResultRespone getUserZone(String isGeneral){
        return ResultRespone.success(qfcbplandetailsService.getUserZone(isGeneral));
    }

    @ApiOperation(value = "抄表计划--手动生成固定月份的抄表数据")
    @GetMapping("/initCbDataByMonth")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "month", value = "月份；如：12", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "monthPlan", value = "年月份；如：202112", required = true, dataType = "String")
    })
    public void initCbDataByMonth(Integer month, String monthPlan) {
        qfcbplandetailsService.initCbDataByMonth(month, monthPlan);
    }

    @ApiOperation(value = "抄表计划--手动生成换表信息")
    @GetMapping("/initCbDataByHbByMonth")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "monthPlan", value = "年月份；如：202112", required = true, dataType = "String")
    })
    public void initCbDataByHbByMonth(String monthPlan) {
        qfcbplandetailsService.initCbDataByHbByMonth(monthPlan);
    }

    @ApiOperation(value = "抄表计划--手动初始化前三年同比水量")
    @GetMapping("/initFrontThreeYearDataByMonth")
    public void initFrontThreeYearDataByMonth(String monthPlan) {
        qfcbplandetailsService.initFrontThreeYearDataByMonth(monthPlan);
    }
//    @ApiOperation(value = "抄表计划--手动生成前三次抄表水量")
//    @GetMapping("/initFrontThreeDataByMonth")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "monthPlan", value = "年月份；如：202112", required = true, dataType = "String")
//    })
//    public void initFrontThreeDataByMonth(String monthPlan) {
//        qfcbplandetailsService.initFrontThreeDataByMonth(monthPlan);
//    }


    @ApiOperation(value = "抄表计划--每月28号凌晨初始化抄表数据")
    @GetMapping("/initCbData")
    public void initCbData() {
        qfcbplandetailsService.initCbData();
    }

    @ApiOperation(value = "抄表计划--每日根据客户档案信息更新后，更新抄表用户中的水量信息")
    @GetMapping("/initCbslEveyDay")
    public void initCbslEveyDay() {
        qfcbplandetailsService.initCbslEveyDay();
    }

    @ApiOperation(value = "抄表计划--每月28号凌晨初始化前三年同比水量")
    @GetMapping("/initFrontThreeYearData")
    public void initFrontThreeYearData() {
        qfcbplandetailsService.initFrontThreeYearData();
    }

    @ApiOperation(value = "抄表计划--每月28初始化抄表计划中的换表信息")
    @GetMapping("/initCbDataByHb")
    public void initCbDataByHb() {
        qfcbplandetailsService.initCbDataByHb();
    }

    @ApiOperation(value = "抄表水量--每天晚上9点统计汇总相加")
    @GetMapping("/waterAdding")
    public void waterAdding() {
        cbqydwaterService.waterAdding();
    }


    @PostMapping(value = "/readRecord", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "APP抄表数据填写")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "yhbh", value = "用户编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "bcds", value = "(本次读数)止度", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "cbrq", value = "抄表日期", required = true, dataType = "Date"),
            @ApiImplicitParam(name = "cbfsCode", value = "抄表方式编码", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "ysxzBm", value = "用水性质", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sbkj", value = "水表口径；传名称：DN15", required = true, dataType = "String"),
            @ApiImplicitParam(name = "bczl", value = "本次增量，本次与上次读数之差", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "scds", value = "(上次读数)起度", required = true, dataType = "Long")
    })
    public ResultRespone readRecord(ReadRecord record, @RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {
        //抄表数据验证第三种方法
        return ResultRespone.success(checkThreeService.readRecord(record, fileList));
    }

    @ApiOperation(value = "抄表汇总数据")
    @GetMapping("/getOffline")
    public ResultRespone<List<ReadCollect>> getOffline(String yearMonth, String cbry, String qybm, String qydbm, String isGeneral,
                                                       HttpServletResponse respons, String templateFlag) {
        return ResultRespone.success(qfcbplandetailsService.getOffline(yearMonth, cbry, qybm, qydbm, isGeneral, respons, templateFlag));
    }

    @ApiOperation(value = "查询客户信息")
    @GetMapping("/getOfflineByYhbh")
    public ResultRespone<AppRead> getOfflineByYhbh(String yearMonth, String yhbh, String isGeneral, Long cbryId, String soleSign) {
        return ResultRespone.success(qfcbplandetailsService.getOfflineByYhbh(yearMonth, yhbh, isGeneral, cbryId, soleSign));
    }

    @GetMapping("/queryOfflineByYhbh")
    @ApiOperation("查询客户信息-更改")
    public ResultRespone queryOfflineByYhbh(String cbrq, String yhbh) {
        return ResultRespone.success(qfcbplandetailsService.queryOfflineByYhbh(cbrq, yhbh));
    }

    @ApiOperation(value = "抄表--抄表详情数据")
    @GetMapping("/getOfflineInfo")
    public ResultRespone<IPage<AuditMap>> getOfflineInfo(Page page, ReadSearch search, HttpServletResponse response) {
        return ResultRespone.success(qfcbplandetailsService.getOfflineInfo(page, search, response));
    }

    @ApiOperation(value = "离线抄表--异常提醒")
    @GetMapping("/abnormalRemind")
    public ResultRespone<String> abnormalRemind(ReadRecord record, String isGeneral) {
        return ResultRespone.success(checkThreeService.readDataCheck(record, isGeneral));
    }

    @ApiOperation(value = "抄表--修改抄表水量")
    @PutMapping("/updateCbsl")
    public ResultRespone updateCbsl(@RequestBody ReadRecord record) {
        return checkThreeService.updateCbsl(record);
    }

    @ApiOperation(value = "离线抄表--同步抄表水量")
    @PutMapping("/synCbsl")
    public ResultRespone synCbsl(String yhbhS, String monthPlan, String isGeneral) {
        return checkThreeService.synCbsl(yhbhS, monthPlan, isGeneral);
    }

    @ApiOperation(value = "远传表算法")
    @GetMapping("/eastone")
    public ResultRespone eastone() {
        return readDataCheckService.eastone();
    }

    @ApiOperation(value = "算法重跑计算")
    @GetMapping("/cbsf")
    public ResultRespone cbsf(String yearMonth) {
        return checkThreeService.cbsf(yearMonth);
    }


    @ApiOperation(value = "重新计算抄表唯一标识")
    @GetMapping("/soleSign")
    public ResultRespone soleSign(String yearMonth) {
        return checkThreeService.soleSign(yearMonth);
    }

    @ApiOperation(value = "客户备注、附件")
    @PostMapping("/clientInfo")
    public ResultRespone clientInfo(Yhforeverinfo yhforeverinfo, List<MultipartFile> fileList) {
        return ResultRespone.success(yhforeverinfoService.clientInfo(yhforeverinfo, fileList));
    }


    @ApiOperation(value = "测试redis")
    @GetMapping("/getRedis")
    public ResultRespone getRedis() {
        checkThreeService.testRead();
        return null;
    }

    @GetMapping("/queryInfoByCode")
    @ApiOperation("查询客户卡号和上次读数")
    public ResultRespone queryInfoByCode() {
        return ResultRespone.success(checkThreeService.queryInfoByCode());
    }


    @PostMapping("/initData")
    @ApiOperation("初始化抄表数据")
    public ResultRespone initData(){
        return ResultRespone.success(qfcbplandetailsService.initData());
    }

    @PostMapping("/testUploadFile")
    @ApiOperation("测试文件上传")
    public ResultRespone testUploadFile(List<MultipartFile> fileList, String filePathBean, String tableName){
        return ResultRespone.success(checkThreeService.saveFileList(fileList,filePathBean,tableName));
    }

    @GetMapping("/getClientMeterReading")
    @ApiOperation("获取用户抄表数据")
    public ResultRespone getClientMeterReading(Page page, String client, String startDate, String endDate){
        return ResultRespone.success(qfcbplandetailsService.getClientMeterReading(page, client, startDate, endDate));
    }

    @GetMapping("/getUserBalance")
    @ApiOperation("获取用户余额信息")
    public ResultRespone getUserBalance(String clientCode){
        return ResultRespone.success(qfcbplandetailsService.getUserBalance(clientCode));
    }

    @GetMapping("/getPaymentInfo")
    @ApiOperation("获取用户缴费信息")
    @ApiImplicitParam(name = "type", value = "0:缴费信息 1:扣费信息 不传默认缴费", dataType = "Integer")
    public ResultRespone getPaymentInfo(Page page, String client,Integer type){
        return ResultRespone.success(qfcbplandetailsService.getPaymentInfo(page, client,type));
    }

    @PutMapping("/updateUserPhone")
    @ApiOperation("修改用户电话")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientCode", value = "用户编号", dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "联系电话", dataType = "String"),
            @ApiImplicitParam(name = "waterAddress", value = "水表地址", dataType = "String")
    })
    public ResultRespone updateUserPhone(UpdateClientDto dto){
        return ResultRespone.success(qfcbplandetailsService.updateUserPhone(dto));
    }

    @GetMapping("/getVendorInfo")
    @ApiOperation("获取水表厂家")
    public ResultRespone getVendorInfo(){
        return ResultRespone.success(qfcbplandetailsService.getVendorInfo());
    }

    /**
     * 获取用户基本信息
     * @param clientCode 用户编号
     * @return list
     */
    @GetMapping("/getBasicInformation")
    @ApiOperation("用户基本信息查询")
    public ResultRespone getBasicInformation(String clientCode){
        return ResultRespone.success(qfcbplandetailsService.getBasicInformation(clientCode));
    }

    /**
     * 获取用户账单
     * @param clientCode 用户编号
     * @return page
     */
    @GetMapping("/getBillInfo")
    @ApiOperation("用户账单查询")
    @ApiImplicitParam(name = "type", value = "0:用户账单 1:欠费记录 2:资金流水", dataType = "Integer")
    public ResultRespone getBillInfo(Page page,String clientCode,Integer type){
        return ResultRespone.success(qfcbplandetailsService.getBillInfo(page,clientCode,type));
    }

    @GetMapping("/getBillTotal")
    @ApiOperation("获取账单合计")
    @ApiImplicitParam(name = "type", value = "0:用户账单 1:欠费记录 2:资金流水", dataType = "Integer")
    public ResultRespone getBillTotal(String code,Integer type){
        return ResultRespone.success(qfcbplandetailsService.getBillTotal(code,type));
    }

    @GetMapping("/waterAuditCountByZone")
    @ApiOperation("水量审核抄表员统计")
    public ResultRespone waterAuditCountByZone(){
        return ResultRespone.success(checkThreeService.waterAuditCountByZone());
    }

    @GetMapping("/waterAuditStatusCount")
    @ApiOperation("水量审核状态统计")
    public ResultRespone waterAuditStatusCount(String readBookCode){
        return ResultRespone.success(checkThreeService.waterAuditStatusCount(readBookCode));
    }

    @GetMapping("/waterAuditInfo")
    @ApiOperation("水量审核信息")
    @ApiImplicitParam(name = "hasException", value = "0:正常 1:异常 2:已复核 6:已停水", dataType = "Integer")
    public ResultRespone waterAuditInfo(Integer hasException,String readBookCode){
        return ResultRespone.success(checkThreeService.waterAuditInfo(hasException,readBookCode));
    }

    @GetMapping("/waterAuditUser")
    @ApiOperation("水量审核用户获取接口")
    public ResultRespone waterAuditUser(){
        return ResultRespone.success(checkThreeService.waterAuditUser());
    }

    @ApiOperation("水量审核")
    @PostMapping("/waterAudit")
    public ResultRespone waterAudit(@RequestBody WaterAuditDto dto){
        return ResultRespone.success(checkThreeService.waterAudit(dto));
    }

}
