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

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.common.oauth.exception.ToflyDeniedException;
import com.tofly.watermeter.custom.entity.meterRead.AbnormalBig;
import com.tofly.watermeter.custom.entity.meterRead.BingYsxz;
import com.tofly.watermeter.entity.QfmidysxzbmE;
import com.tofly.watermeter.entity.Readbigabnormal;
import com.tofly.watermeter.entity.Readbigysxz;
import com.tofly.watermeter.service.ReadbigabnormalService;
import com.tofly.watermeter.service.ReadbigysxzService;
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 java.util.Arrays;
import java.util.List;

/**
 * @author FlashWJL
 * @date 2022/1/10 10:47
 */
@RestController
@AllArgsConstructor
@RequestMapping("/abnormal")
@Api(tags = "抄表--抄表算法配置二")
public class ReadBigAbnormalController {

    private final ReadbigysxzService readbigysxzService;
    private final ReadbigabnormalService readbigabnormalService;
    /**
     * 分页查询
     * @param page 分页对象
     * @param readbigysxz 抄表模型算法--用水性质大类
     * @return 分页数据
     */
    @GetMapping("/readbigysxz" )
    @ApiOperation(value = "用水性质大类--分页查询")
    public ResultRespone<IPage<Readbigysxz>> getReadbigysxzPage(Page<Readbigysxz> page, Readbigysxz readbigysxz) {
        return ResultRespone.success(readbigysxzService.page(page, Wrappers.query(readbigysxz)));
    }
    /**
     * 新增抄表模型算法--用水性质大类
     * @param readbigysxz 抄表模型算法--用水性质大类
     * @return ResultRespone
     */
    @ApiOperation(value = "用水性质大类--新增")
    @PostMapping("/readbigysxz")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeName", value = "用水性质大类名称", required = true, dataType = "String")
    })
    public ResultRespone<Boolean> saveReadbigysxz(@RequestBody Readbigysxz readbigysxz) {
        String typeName = readbigysxz.getTypeName();
        if (StringUtil.isEmpty(typeName)){
            throw new ToflyDeniedException("用水性质大类名称必填");
        }
        Readbigysxz isReadbigysxz = readbigysxzService.getOne(Wrappers.<Readbigysxz>lambdaQuery().eq(Readbigysxz::getTypeName , typeName));
        if (StringUtil.isNotNull(isReadbigysxz)){
            throw new ToflyDeniedException("已存在用水性质："+typeName);
        }
        return ResultRespone.success(readbigysxzService.save(readbigysxz));
    }
    /**
     * 修改抄表模型算法--用水性质大类
     * @param readbigysxz 抄表模型算法--用水性质大类
     * @return ResultRespone
     */
    @ApiOperation(value = "用水性质大类--修改")
    @PutMapping("/readbigysxz")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeName", value = "用水性质大类名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone<Boolean> updateReadbigysxz(@RequestBody Readbigysxz readbigysxz) {
        return ResultRespone.success(readbigysxzService.updateById(readbigysxz));
    }

    /**
     * 通过id批量删除抄表模型算法--用水性质大类
     * @param ids id
     * @return ResultRespone
     */
    @ApiOperation(value = "用水性质大类--批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/readbigysxz" )
    public ResultRespone<Boolean> deleteReadbigysxz(String ids) {
        //删除用水性质大类，还需要删除算法配置
        return ResultRespone.success(readbigysxzService.deleteReadBig(ids));
    }

    @GetMapping("/getNoBingYsxz" )
    @ApiOperation(value = "获取未归类的用水性质")
    public ResultRespone<IPage<QfmidysxzbmE>> getNoBingYsxz(Page<QfmidysxzbmE> page) {
        return ResultRespone.success(readbigysxzService.getNoBingYsxz(page));
    }

    @PostMapping("/saveBingYsxz" )
    @ApiOperation(value = "分类管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ysxzBm", value = "需要归类的用水性质编码；多个逗号隔开", required = true, dataType = "String"),
            @ApiImplicitParam(name = "classifyType", value = "分类管理类型；0：归类；1：移除归类", required = true, dataType = "String"),
            @ApiImplicitParam(name = "bigYsxz", value = "大类用水性质ID", required = true, dataType = "String"),
    })
    public ResultRespone<Object> saveBingYsxz(@RequestBody BingYsxz bingYsxz) {
        return readbigysxzService.saveBingYsxz(bingYsxz);
    }

    @PostMapping("/saveModel" )
    @ApiOperation(value = "保存归类模型参数配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bigYsxzId", value = "用水性质大类ID 必传", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sbkjName", value = "水表口径名称 必传", required = true, dataType = "String")
    })
    public ResultRespone<Object> saveModel(@RequestBody Readbigabnormal operateAbnomal) {
        return readbigysxzService.saveModel(operateAbnomal);
    }

    @GetMapping("/getModel" )
    @ApiOperation(value = "获取归类模型参数配置")
    public ResultRespone<List<Readbigabnormal>> getModel() {
        return ResultRespone.success(readbigabnormalService.list(Wrappers.<Readbigabnormal>lambdaQuery().orderByAsc(Readbigabnormal::getSbkjName)));
    }

}
