/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.huimv.modular.alarmvalueset.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.huimv.core.pojo.response.ResponseData;
import com.huimv.core.pojo.response.SuccessResponseData;
import com.huimv.modular.alarmvalueset.entity.AlarmValueSet;
import com.huimv.modular.alarmvalueset.mapper.AlarmValueSetMapper;
import com.huimv.modular.alarmvalueset.param.AlarmValueSetParam;
import com.huimv.modular.alarmvalueset.service.AlarmValueSetService;
import com.huimv.modular.collectorregister.entity.CollectorRegister;
import com.huimv.modular.collectorregister.mapper.CollectorRegisterMapper;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 报警参数设置控制器
 *
 * @author ydd
 * @date 2021-08-20 13:23:30
 */
@RestController
public class AlarmValueSetController {

    @Resource
    private AlarmValueSetService alarmValueSetService;
    @Resource
    private CollectorRegisterMapper collectorRegisterMapper;
    @Resource
    private AlarmValueSetMapper  alarmValueSetMapper;

    /**
     * 查询报警参数设置
     *
     * @author ydd
     * @date 2021-08-20 13:23:30
     */

    @GetMapping("/alarmValueSet/page")
   // @BusinessLog(title = "报警参数设置_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(AlarmValueSetParam alarmValueSetParam) {
        return new SuccessResponseData(alarmValueSetService.page(alarmValueSetParam));
    }

    /**
     * 添加报警参数设置
     *
     * @author ydd
     * @date 2021-08-20 13:23:30
     */

    @PostMapping("/alarmValueSet/add")
   // @BusinessLog(title = "报警参数设置_增加", opType = LogAnnotionOpTypeEnum.ADD)
   // public ResponseData add(@RequestBody @Validated(AlarmValueSetParam.add.class) AlarmValueSetParam alarmValueSetParam) {
    public ResponseData add(@RequestParam Map<String, Object> params) {
        String alarmType = (String) params.get("alarmType");//开始时间
        Long orgId =  (Long)params.get("orgId");;
        System.out.println(orgId+"组织ID"+alarmType);

        String setValue = (String) params.get("setValue");//开始时间
        LambdaQueryWrapper<CollectorRegister> wrapper3 = Wrappers.lambdaQuery();
        wrapper3.eq(CollectorRegister::getOrgId, orgId).orderByAsc(CollectorRegister::getId).last("limit 1");
        CollectorRegister collectorRegister = collectorRegisterMapper.selectOne(wrapper3);
        if(collectorRegister==null){
            return new SuccessResponseData("当前牧场没有数据采集设备，无法设置阈值");
        }

        LambdaQueryWrapper<AlarmValueSet> wrapper4 = Wrappers.lambdaQuery();
        wrapper4.eq(AlarmValueSet::getOrgId, orgId).eq(AlarmValueSet::getAlarmType,Integer.valueOf(alarmType) );
        AlarmValueSet alarmValueSet1 = alarmValueSetMapper.selectOne(wrapper4);
        System.out.println(alarmValueSet1);
        if (alarmValueSet1!=null) {
            return new SuccessResponseData("已经设置此阈值");
        }


        AlarmValueSet alarmValueSet = new AlarmValueSet();
        alarmValueSet.setAlarmType(Integer.valueOf(alarmType));
        alarmValueSet.setCanStatus(0);
        alarmValueSet.setFarmCode(collectorRegister.getFarmCode());
        alarmValueSet.setFarmName(collectorRegister.getFarmName());
        alarmValueSet.setOrgId(String.valueOf(orgId) );
        alarmValueSet.setRemark("测试数据");
        alarmValueSet.setSetValue(Double.valueOf(setValue));
        alarmValueSet.setUpdateTime(new Date());
        alarmValueSetService.save(alarmValueSet);
        return new SuccessResponseData();
    }




    /**
     * 删除报警参数设置，可批量删除
     *
     * @author ydd
     * @date 2021-08-20 13:23:30
     */

    @PostMapping("/alarmValueSet/delete")
   // @BusinessLog(title = "报警参数设置_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(AlarmValueSetParam.delete.class) List<AlarmValueSetParam> alarmValueSetParamList) {
            alarmValueSetService.delete(alarmValueSetParamList);
        return new SuccessResponseData();
    }

    /**
     * 编辑报警参数设置
     *
     * @author ydd
     * @date 2021-08-20 13:23:30
     */

    @PostMapping("/alarmValueSet/edit")
   // @BusinessLog(title = "报警参数设置_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    //public ResponseData edit(@RequestBody @Validated(AlarmValueSetParam.edit.class) AlarmValueSetParam alarmValueSetParam) {
    public ResponseData edit(@RequestBody  Map<String, Object> params) {
           // alarmValueSetService.edit(alarmValueSetParam);
        // String farmID = (String) params.get("farmID");//牧场编码
        String  id = (String) params.get("id");//牧场编码
        Integer alarmType = (int) params.get("alarmType");//开始时间
        String setValue = (String) params.get("setValue");//开始时间
        LambdaQueryWrapper<AlarmValueSet> wrapper3 = Wrappers.lambdaQuery();
        wrapper3.eq(AlarmValueSet::getId,Integer.valueOf(id) );
        AlarmValueSet alarmValueSet = alarmValueSetMapper.selectOne(wrapper3);

        alarmValueSet.setAlarmType(alarmType);
        alarmValueSet.setCanStatus(0);
        alarmValueSet.setSetValue(Double.valueOf(setValue));
        alarmValueSetService.updateById(alarmValueSet);
        return new SuccessResponseData();
    }

    /**
     * 查看报警参数设置
     *
     * @author ydd
     * @date 2021-08-20 13:23:30
     */

    @GetMapping("/alarmValueSet/detail")
   // @BusinessLog(title = "报警参数设置_查看", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(AlarmValueSetParam.detail.class) AlarmValueSetParam alarmValueSetParam) {
        return new SuccessResponseData(alarmValueSetService.detail(alarmValueSetParam));
    }

    /**
     * 报警参数设置列表
     *
     * @author ydd
     * @date 2021-08-20 13:23:30
     */

    @GetMapping("/alarmValueSet/list")
   // @BusinessLog(title = "报警参数设置_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(AlarmValueSetParam alarmValueSetParam) {
        return new SuccessResponseData(alarmValueSetService.list(alarmValueSetParam));
    }

    /**
     * 导出系统用户
     *
     * @author ydd
     * @date 2021-08-20 13:23:30
     */

    @GetMapping("/alarmValueSet/export")
   // @BusinessLog(title = "报警参数设置_导出", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void export(AlarmValueSetParam alarmValueSetParam) {
        alarmValueSetService.export(alarmValueSetParam);
    }

}
