package com.chinaitop.depot.intelligent.basicdata.controller;


import com.chinaitop.depot.intelligent.SendMessagePT.SendMessagePT;
import com.chinaitop.depot.intelligent.SendMessagePT.SmsDataVO;
import com.chinaitop.depot.intelligent.basicdata.model.MonitorAlarmPhone;
import com.chinaitop.depot.intelligent.basicdata.model.MonitorAlarmPhoneExample;
import com.chinaitop.depot.intelligent.basicdata.service.AlarmService;
import com.chinaitop.depot.intelligent.utils.ParameterUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unissoft.model.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 报警的配置类
 */
@RestController
@RequestMapping(value = "intelligents/alarm")
@Api(description = "智能仓房报警的配置类")
public class AlarmController {

    @Resource
    private AlarmService alarmService;

    /**
     *
     * @param pageNum 页码
     * @param pageSize 条数
     * @param detectionType 报警类型
     * @param alarmRole 角色
     * @param orgId orgid
     * @return
     */
    @RequestMapping(value="/getAlarmPhone", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value="报警配置信息", notes = "报警配置信息列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "detectionType", value = "报警类型", paramType = "query"),
            @ApiImplicitParam(name = "alarmRole", value = "报警角色", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "报警库点id", paramType = "query")
    })
    public PageInfo<MonitorAlarmPhone> getAlarmPhone(Integer pageNum, Integer pageSize, String detectionType,String  alarmRole ,Integer orgId) {
        MonitorAlarmPhoneExample example = new MonitorAlarmPhoneExample();
        MonitorAlarmPhoneExample.Criteria criteria = example.createCriteria();
        if (ParameterUtil.isnotnull(orgId)) {
            criteria.andOrgIdEqualTo(orgId);

        }
        if (ParameterUtil.isnotnull(detectionType)) {
            criteria.andDetectionTypeEqualTo(detectionType);

        }
        if (ParameterUtil.isnotnull(alarmRole)) {
            criteria.andAlarmRoleLike("%"+alarmRole+"%");
        }
            example.setOrderByClause("updatetime desc");
        if (pageNum!=null && pageSize!=null) {
            PageHelper.startPage(pageNum, pageSize);
        }

        List<MonitorAlarmPhone> list = alarmService.getAlarmPhone(example);
        PageInfo<MonitorAlarmPhone> pageInfo = new PageInfo<MonitorAlarmPhone>(list);
        return pageInfo;
    }

    /**
     * 保存报警配置信息
     * @param alarmJson 报警配置信息
     * @param orgid 组织机构ID
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/saveAlarmPhone", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value="保存报警配置信息", notes = "保存报警信息。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "alarmJson", value = "报警配置信息",paramType = "form"),
            @ApiImplicitParam(name = "orgid", value = "组织机构ID",paramType = "form")
    })
    public Map<String, Object> saveAlarmPhone(String alarmJson,Integer orgid) throws Exception {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        // JSON字符串转对象
        ObjectMapper mapper = new ObjectMapper();
        MonitorAlarmPhone camera = (MonitorAlarmPhone)mapper.readValue(alarmJson, MonitorAlarmPhone.class);
        camera.setOrgId(orgid);//添加组织
        if (camera != null) {
            alarmService.saveAlarmPhone(camera);
            modelMap.put("status", "success");
        }else {
            modelMap.put("status", "err");
        }

        return modelMap;
    }

    /**
     * 删除报警信息
     * @param alarmId 需要删除的报警信息ID
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/deleteAlarmPhone", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value="删除报警信息", notes = "删除报警信息。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "alarmId", value="需要删除的报警信息ID", paramType = "form")
    })
    public Map<String, Object> deleteAlarmPhone(String alarmId){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try{
            alarmService.deleteAlarmPhone(Integer.valueOf(alarmId));
            modelMap.put("status", "success");
        }catch (Exception e){
            modelMap.put("status", "error");
            e.printStackTrace();
        }
        return modelMap;
    }
 /**
     * 查询报警信息
     * @param alarmId 需要的报警信息ID
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/getByIdGetAlarmPhone", method = RequestMethod.POST)
    @ApiOperation(value="根据查询报警信息", notes = "根据查询报警信息。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "alarmId", value="需要查询的报警信息ID", paramType = "form")
    })
    public ResponseEntity<MonitorAlarmPhone> getByIdGetAlarmPhone(String alarmId){
        MonitorAlarmPhone alarmPhone = null;
        try{
            if (StringUtils.isNotBlank(alarmId)) {
                Integer integer = Integer.valueOf(alarmId);
                if (integer != null) {
                    alarmPhone = alarmService.getByIdAlarmPhone(integer);
                } else {
                return ResponseEntity.failed("ID数据有误!");
            }
            }
        }catch (Exception e){

            e.printStackTrace();
            return ResponseEntity.failed("ID数据有误!");
        }
        return ResponseEntity.ok(alarmPhone);
    }
    @RequestMapping(value = "/getByNameOrtpl", method = RequestMethod.GET)
    @ApiOperation(value = "校验是否重复", notes = "校验是否重复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "alarmMan", value = "接收人名称", paramType = "query"),
            @ApiImplicitParam(name = "alarmPhone", value = "接收人电话", paramType = "query")
    })
    public ResponseEntity getByNameOrtpl(String alarmMan, String alarmPhone) {
        boolean Code = alarmService.getByNameOrtpl(alarmMan, alarmPhone);
        return ResponseEntity.ok(Code);
    }
    /**
     * 报警配置信息
     * @return
     */
    @RequestMapping(value="/getAllAlarmPhone", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value="报警信息",notes = "查询报警信息。")
    public List<MonitorAlarmPhone> getAllAlarmPhone() {
        return alarmService.getAllAlarmPhone();
    }

    public static void main(String[] args){
        String sendMsg = "123456";
        sendMsg = sendMsg.substring(0, 3);
        System.out.println(sendMsg);
        try {
            SendMessagePT sendMessagePT = new SendMessagePT();
            SmsDataVO smsDataVO = new SmsDataVO();
            smsDataVO.setSdst("18331115505");
            smsDataVO.setSmsg("测试");
            sendMessagePT.sendingSMS(smsDataVO);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }
}
