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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.watermeter.custom.entity.SearchSb;
import com.tofly.watermeter.custom.entity.Statistic;
import com.tofly.watermeter.custom.service.StatisticService;
import com.tofly.watermeter.entity.Bwrksbdatail;
import com.tofly.watermeter.entity.Bwzysbjddatail;
import com.tofly.watermeter.service.BwrksbdatailService;
import com.tofly.watermeter.service.BwzysbjddatailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author FlashWJL
 * @date 2021/3/24 17:12
 */
@RestController
@AllArgsConstructor
@RequestMapping("/statistic")
@Api(tags = "表务--统计接口")
public class StatisticsController {

    private final StatisticService statisticService;
    private final BwrksbdatailService bwrksbdatailService;
    private final BwzysbjddatailService bwzysbjddatailService;

    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param searchSb 搜索内容
     * @return
     */
    @GetMapping("/getAllSb")
    @ApiOperation(value = "查看所有水表")
    public ResultRespone getAlSb(Page page, SearchSb searchSb) {
        Long bwSbkj=searchSb.getBwSbkj();
        Long bwSbcj=searchSb.getBwSbcj();
        Long bwSblx=searchSb.getBwSblx();
        String inputContent = searchSb.getInputContent();
        String kcInfo = searchSb.getKcInfo();

        IPage info = bwrksbdatailService.page(page,Wrappers.<Bwrksbdatail>lambdaQuery()
                .eq(StringUtil.isNotNull(bwSbkj),Bwrksbdatail::getBwSbkj,bwSbkj)
                .eq(StringUtil.isNotNull(bwSbcj),Bwrksbdatail::getBwSbcj,bwSbcj)
                .eq(StringUtil.isNotNull(bwSblx),Bwrksbdatail::getBwSblx,bwSblx)
                .and("1".equals(kcInfo),k->k.eq(Bwrksbdatail::getBwSbzt , "55").or().eq(Bwrksbdatail::getBwSbzt , "62"))
                .and(StringUtil.isNotEmpty(inputContent),k->k.like(Bwrksbdatail::getBwKhbh,inputContent).or()
                .like(Bwrksbdatail::getBwKhxm,inputContent).or()
                .like(Bwrksbdatail::getBwKhdz,inputContent).or()
                .like(Bwrksbdatail::getBwKhdh,inputContent)));
        if (StringUtil.isNull(info)) {
            return ResultRespone.failed("查询失败");
        }
        return ResultRespone.success(info);
    }

    /**
     *查看争议水表
     * @param statistic 查询条件
     * @return 返回结果
     */
    @GetMapping("/getAllZySb")
    @ApiOperation(value = "查看争议水表统计")
    public ResultRespone getAllZySb(Statistic statistic , HttpServletResponse response) {


        return ResultRespone.success(bwzysbjddatailService.getAllZySb(statistic,response));
//        return ResultRespone.success(bwzysbjddatailService.page(page,Wrappers.<Bwzysbjddatail>lambdaQuery()
//                .eq(StringUtil.isNotNull(bwSbkj),Bwzysbjddatail::getBwSbkj,bwSbkj)
//                .eq(StringUtil.isNotNull(bwSbcj),Bwzysbjddatail::getBwSbcj,bwSbcj)
//                .eq(StringUtil.isNotNull(bwSblx),Bwzysbjddatail::getBwSblx,bwSblx)
//                .like(StringUtil.isNotEmpty(inputContent),Bwzysbjddatail::getBwYhbh,inputContent).or()
//                .like(StringUtil.isNotEmpty(inputContent),Bwzysbjddatail::getBwYhxm,inputContent).or()
//                .like(StringUtil.isNotEmpty(inputContent),Bwzysbjddatail::getBwYhdz,inputContent).or()
//                .like(StringUtil.isNotEmpty(inputContent),Bwzysbjddatail::getBwLxdh,inputContent).or()
//                .eq(StringUtil.isNotNull(time),Bwzysbjddatail::getBwJyrq,time)));
    }

    @GetMapping("/getZySbInfo")
    @ApiOperation(value = "查看争议水表统计详情")
    public ResultRespone getZySbInfo(Page page, Statistic statistic) {
        Long bwSbkj=statistic.getBwSbkj();
        Long bwSbcj=statistic.getBwSbcj();
        Long bwSblx=statistic.getBwSblx();
        Date start = statistic.getStartTime();
        Date end = statistic.getEndTime();
        return ResultRespone.success(bwzysbjddatailService.page(page,Wrappers.<Bwzysbjddatail>lambdaQuery()
                .eq(StringUtil.isNotNull(bwSbkj),Bwzysbjddatail::getBwSbkj,bwSbkj)
                .eq(StringUtil.isNotNull(bwSbcj),Bwzysbjddatail::getBwSbcj,bwSbcj)
                .eq(StringUtil.isNotNull(bwSblx),Bwzysbjddatail::getBwSblx,bwSblx)
                .between(StringUtil.isNotNull(start) && StringUtil.isNotNull(end)  , Bwzysbjddatail::getBwJyrq , start , end)));
    }



    /**
     * 根据水表厂家、类型、口径等查看水表报废情况
     * 报废统计需要是表已经移交过后的，才算是真正的报废了
     * 换表后水表处于报废状态，即使没有移交也不能算到报废统计里面
     * @param statistic
     * @return
     */
    @ApiOperation(value = "报废统计")
    @GetMapping("/scrap")
    public ResultRespone getScrap(Page page , Statistic statistic) {
        return ResultRespone.success(statisticService.getScrap(page,statistic));
    }

    @ApiOperation(value = "查看报废统计详情")
    @GetMapping("/scrapInfo")
    public ResultRespone getScrapInfo(Page page , Statistic statistic) {
        return ResultRespone.success(statisticService.getScrapInfo(page , statistic));
    }

    @ApiOperation(value = "新装统计")
    @GetMapping("/getInstall")
    public ResultRespone getInstall(Page page , Statistic statistic) {
        return ResultRespone.success(statisticService.getInstall(page,statistic));
    }

    @ApiOperation(value = "查看新装统计详情")
    @GetMapping("/getInstallInfo")
    public ResultRespone getInstallInfo(Page page ,  Statistic statistic) {
        return ResultRespone.success(statisticService.getInstallInfo(page,statistic));
    }

    @ApiOperation(value = "出库水表统计")
    @GetMapping("/getCkSb")
    public ResultRespone getCkSb(Page page , Statistic statistic) {
        return ResultRespone.success(statisticService.getCkSb(page,statistic));
    }

    @ApiOperation(value = "出库详情水表")
    @GetMapping("/getCkSbInfo")
    public ResultRespone getCkSbInfo(Page page , Statistic statistic) {
        return ResultRespone.success(statisticService.getCkSbInfo(page,statistic));
    }

    @ApiOperation(value = "换表统计")
    @GetMapping("/getHb")
    public ResultRespone getHb(Statistic statistic , HttpServletResponse response) {
        return ResultRespone.success(statisticService.getHb(statistic , response));
    }
    @ApiOperation(value = "换表详情统计查看")
    @GetMapping("/getHbInfo")
    public ResultRespone getHbInfo(Page page , Statistic statistic) {
        return ResultRespone.success(statisticService.getHbInfo(page , statistic));
    }

    @ApiOperation(value = "所有水表流程展示")
    @GetMapping("/getMeterFlow")
    public ResultRespone getMeterFlow(Page page , Statistic statistic , HttpServletResponse response) {
        return ResultRespone.success(statisticService.getMeterFlow(page , statistic , response));
    }

    @ApiOperation(value = "水表流程详情展示")
    @GetMapping("/getFlowDetails")
    public ResultRespone getFlowDetails(Statistic statistic) {
        return ResultRespone.success(statisticService.getFlowDetails( statistic));
    }

    @ApiOperation(value = "厂家柱状图统计")
    @GetMapping("/getCjSta")
    public ResultRespone getCjSta(Statistic statistic) {
        return ResultRespone.success(statisticService.getCjSta( statistic));
    }

    @ApiOperation(value = "水表使用情况汇总")
    @GetMapping("/getSyDate")
    public ResultRespone getSyDate(Page page , Statistic statistic) {
        return ResultRespone.success(statisticService.getSyDate(page , statistic));
    }

    @ApiOperation(value = "水表使用情况详情")
    @GetMapping("/getSyDateDetails")
    public ResultRespone getSyDateDetails(Page page , Statistic statistic , HttpServletResponse response) {
        return ResultRespone.success(statisticService.getSyDateDetails(page , statistic , response));
    }









}
