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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.log.annotation.ToFlyAppLog;
import com.tofly.xrtygis.entity.LeakAssessment;
import com.tofly.xrtygis.service.LeakAssessmentService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 分期漏损评估配置表
 *
 * @author admin
 * @date Thu Apr 28 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/leakassessment")
@Api(tags="分期漏损评估配置表接口")
public class LeakAssessmentController {

    private final LeakAssessmentService leakAssessmentService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param leakAssessment 分期漏损评估配置表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getLeakAssessmentPage(Page page, @ModelAttribute LeakAssessment leakAssessment) {
      return ResultRespone.success(leakAssessmentService.page(page, Wrappers.query(leakAssessment)));
    }


    /**
     * 通过id查询分期漏损评估配置表
     * @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(leakAssessmentService.getById(id));
    }

    /**
     * 新增分期漏损评估配置表
     * @param leakAssessment 分期漏损评估配置表
     * @return ResultRespone
     */
    @ApiOperation(value = "新增分期漏损评估配置表")
    @PostMapping
    public ResultRespone save(@RequestBody @Valid LeakAssessment leakAssessment) {
        if(leakAssessment.getLeakUp() < leakAssessment.getLeakDown()){
            return ResultRespone.failed("上限值不能小于下限值");
        }
        if(leakAssessment.getLeakUp() < 0 || leakAssessment.getLeakUp() >100 || leakAssessment.getLeakDown() < 0 || leakAssessment.getLeakDown() >100){
            return ResultRespone.failed("上下限值设置有误，请重新设置");
        }
        LeakAssessment leakAssessmentQ = leakAssessmentService.getOne(new QueryWrapper<LeakAssessment>()
                .eq("DMA_LEVLE", leakAssessment.getDmaLevle())
                .eq("LEAK_ASSESSMENT", leakAssessment.getLeakAssessment()));
        if(leakAssessmentQ != null){
            return ResultRespone.failed("此分区级别评估类型已经存在");
        }
        List<LeakAssessment> leakAssessments = leakAssessmentService.list(new QueryWrapper<LeakAssessment>()
                .eq("DMA_LEVLE",leakAssessment.getDmaLevle())
                .between("LEAK_UP",leakAssessment.getLeakDown(),leakAssessment.getLeakUp())
                .or()
                .between("LEAK_DOWN",leakAssessment.getLeakDown(),leakAssessment.getLeakUp()));
        List<LeakAssessment> leakAssessments2 = leakAssessmentService.list(new QueryWrapper<LeakAssessment>()
                .eq("DMA_LEVLE",leakAssessment.getDmaLevle())
                .ge("LEAK_UP",leakAssessment.getLeakUp())
                .le("LEAK_DOWN",leakAssessment.getLeakDown()));
        if(leakAssessments.size() != 0 || leakAssessments2.size() != 0){
            return ResultRespone.failed("上下限值设置有误，请重新设置");
        }
      return ResultRespone.success(leakAssessmentService.save(leakAssessment));
    }

    /**
     * 修改分期漏损评估配置表
     * @param leakAssessment 分期漏损评估配置表
     * @return ResultRespone
     */
    @ApiOperation(value = "修改分期漏损评估配置表")
    @PutMapping
    public ResultRespone updateById(@RequestBody LeakAssessment leakAssessment) {
      return ResultRespone.success(leakAssessmentService.updateById(leakAssessment));
    }


    /**
     * 通过id批量删除分期漏损评估配置表
     * @param ids id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过ID批量删除分期漏损评估配置表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(leakAssessmentService.removeByIds(Arrays.asList(ids.split(","))));
    }

    @GetMapping("/getRecordInfo")
    @ApiOperation("查询或导出坐席人员详细信息")
    public ResultRespone exportLeakAssessment(HttpServletResponse response) {
        return ResultRespone.success(leakAssessmentService.exportLeakAssessment(response));
    }
}
