package com.ho.business.controller;

import com.ho.business.service.ColCountService;
import com.ho.business.vo.req.colCount.ColCountDel;
import com.ho.business.vo.req.colCount.ColCountReq;
import com.ho.business.vo.resp.colCount.ColCountResp;
import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.util.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Description 指标偏差值
 * @Author xwz
 * @Date 2023/7/5
 */
@RestController
@RequestMapping(ContextConstant.BUSINESS + "colCount")
@Api(tags = "指标偏差值")
public class ColCountController {

    @Autowired
    ColCountService colCountService;

    @PostMapping("pageList")
    @ApiOperation(value = "查询指标偏差值")
    public DataResult<ColCountResp> pageList(@RequestBody ColCountReq vo) {
        PageResult<ColCountResp> pageList = colCountService.getPageList(vo);
        return DataResult.success(pageList);
    }

    @PostMapping("add")
    @ApiOperation(value = "新增指标偏差值")
    @LogAnnotation(title = "指标偏差值", action = "新增指标偏差值")
    public DataResult<ColCountResp> add(@RequestBody ColCountReq vo) {
        if(null==vo.getColName()||null==vo.getCol()||null==vo.getDeviceType()){
            throw new BusinessException(BaseResponseCode.NON_DATA);
        }
        ColCountReq queryCol = new ColCountReq();
        queryCol.setStationId(vo.getStationId());
        queryCol.setCol(vo.getCol());
        queryCol.setDeviceType(vo.getDeviceType());
        List<ColCountResp> list = colCountService.selectByParam(queryCol);
        if(null != list && list.size()>0){
            throw new BusinessException(BaseResponseCode.EXISTS_DUPLICATE_DATA);
        }
        int i = colCountService.insertColCount(vo);
        return DataResult.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "更新指标偏差值")
    @LogAnnotation(title = "指标偏差值", action = "更新指标偏差值")
    public DataResult<ColCountResp> update(@RequestBody ColCountReq vo) {
        int i = colCountService.updateById(vo);
        return DataResult.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除指标偏差值")
    @LogAnnotation(title = "指标偏差值", action = "删除指标偏差值")
    public DataResult<ColCountResp> delte(@RequestBody ColCountDel vo) {
        colCountService.deleteByList(vo.getIds());
        return DataResult.success();
    }
}
