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.watermeter.entity.Qfcbarithmetic;
import com.tofly.watermeter.entity.Readabnormal;
import com.tofly.watermeter.entity.TfReadabnomaldes;
import com.tofly.watermeter.service.QfcbarithmeticService;
import com.tofly.watermeter.service.ReadabnormalService;
import com.tofly.watermeter.service.TfReadabnomaldesService;
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 2021/10/21 16:37
 */
@RestController
@AllArgsConstructor
@RequestMapping("/arithmetic")
@Api(tags = "抄表--抄表算法配置")
public class ReadArithmeticController {

    private final QfcbarithmeticService qfcbarithmeticService;
    private final ReadabnormalService readabnormalService;
    private final TfReadabnomaldesService tfReadabnomaldesService;

    @GetMapping("/getArith")
    @ApiOperation(value = "异常提醒--获取算法提醒配置")
    public List<Qfcbarithmetic> getArith() {
        return qfcbarithmeticService.list(Wrappers.<Qfcbarithmetic>lambdaQuery().orderByAsc(Qfcbarithmetic::getId));
    }


    @PostMapping("/getArith")
    @ApiOperation(value = "异常提醒--更改算法提醒配置")
    public ResultRespone updateArith(@RequestBody Qfcbarithmetic qfcbarithmetic) {
        return ResultRespone.success(qfcbarithmeticService.updateArith(qfcbarithmetic));
    }

    /**
     *
     * @param page
     * @param inputcontent
     * @param ysxz
     * @param sbkj
     * @return
     */
    @GetMapping("/getAbnormalArith")
    @ApiOperation(value = "异常模型--获取异常算法配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inputcontent", value = "输入框", required = false, dataType = "String"),
            @ApiImplicitParam(name = "ysxz", value = "用水性质编码，多个逗号隔开", required = false,  dataType = "String"),
            @ApiImplicitParam(name = "sbkj", value = "水表口径名称，多个逗号隔开", required = false,  dataType = "String"),
    })
    public ResultRespone<IPage<Readabnormal>> getAbnormalArith(Page<Readabnormal> page , String inputcontent , String ysxz , String sbkj) {
        List<String> ysxzList = null;
        List<String> sbkjList = null;
        if (StringUtil.isNotEmpty(ysxz)){
            ysxzList = Arrays.asList(ysxz.split(","));
        }
        if (StringUtil.isNotEmpty(sbkj)){
            sbkjList = Arrays.asList(sbkj.split(","));
        }
        return ResultRespone.success(readabnormalService.page(page , Wrappers.<Readabnormal>lambdaQuery().and(StringUtil.isNotEmpty(inputcontent),
                v-> v.like(Readabnormal::getDescribe , inputcontent).or().like(Readabnormal::getModelName , inputcontent))
                .in(StringUtil.isNotNull(ysxzList) , Readabnormal::getYsxzBm , ysxzList)
                .in(StringUtil.isNotNull(sbkjList) , Readabnormal::getSbkjBm , sbkjList)
                .orderByAsc(Readabnormal::getCreateDate)));
    }

    @PostMapping("/saveAbnormalArith")
    @ApiOperation(value = "异常模型--新增异常算法配置")
    public ResultRespone saveAbnormalArith(@RequestBody Readabnormal readabnormal) {
        return readabnormalService.saveAbnormalArith(readabnormal);
    }

    @PutMapping("/updateAbnormalArith")
    @ApiOperation(value = "异常模型--修改异常算法配置")
    public ResultRespone updateAbnormalArith(@RequestBody Readabnormal readabnormal) {
        return readabnormalService.updateAbnormalArith(readabnormal);
    }

    @DeleteMapping("/deleteAbnormalArith")
    @ApiOperation(value = "异常模型--删除异常算法配置")
    public ResultRespone deleteAbnormalArith(String ids) {
        if (StringUtil.isNotEmpty(ids)){
            readabnormalService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultRespone.success(true , "成功");
    }


    /**
     * 分页查询
     * @param page 分页对象
     * @param tfReadabnomaldes 模型参数说明
     * @return
     */
    @GetMapping("/page" )
    @ApiOperation(value = "模型参数说明--分页查询")
    public ResultRespone<IPage<TfReadabnomaldes>> getTfReadabnomaldesPage(Page<TfReadabnomaldes> page, @ModelAttribute TfReadabnomaldes tfReadabnomaldes) {
        return ResultRespone.success(tfReadabnomaldesService.page(page, Wrappers.query(tfReadabnomaldes)));
    }


    /**
     * 通过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(tfReadabnomaldesService.getById(id));
    }

    /**
     * 新增模型参数说明
     * @param tfReadabnomaldes 模型参数说明
     * @return ResultRespone
     */
    @ApiOperation(value = "模型参数说明--新增模型参数说明")
    @PostMapping
    public ResultRespone save(@RequestBody TfReadabnomaldes tfReadabnomaldes) {
        return ResultRespone.success(tfReadabnomaldesService.save(tfReadabnomaldes));
    }

    /**
     * 修改模型参数说明
     * @param tfReadabnomaldes 模型参数说明
     * @return ResultRespone
     */
    @ApiOperation(value = "模型参数说明--修改模型参数说明")
    @PutMapping
    public ResultRespone updateById(@RequestBody TfReadabnomaldes tfReadabnomaldes) {
        return ResultRespone.success(tfReadabnomaldesService.updateById(tfReadabnomaldes));
    }

    /**
     * 通过id删除模型参数说明
     * @param id id
     * @return ResultRespone
     */
    @ApiOperation(value = "模型参数说明--通过ID删除模型参数说明")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable Long id) {
        return ResultRespone.success(tfReadabnomaldesService.removeById(id));
    }

}
