package com.jpsoft.shinestar.modules.mobile.controller;

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.entity.AlarmInfo;
import com.jpsoft.shinestar.modules.base.service.AlarmInfoService;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/mobile/alarmApi")
@Api(description = "安防报警接口")
public class AlarmApiController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private AlarmInfoService alarmInfoService;

    @PostMapping("alarmList")
    @ApiOperation(value = "警报列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryStartTime", value = "查询时间-起 yyyy-MM-dd HH:mm", paramType = "form"),
            @ApiImplicitParam(name = "queryEndTime", value = "查询时间-止 yyyy-MM-dd HH:mm", paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form")
    })
    public MessageResult<Map> alarmList(@RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
                                        @RequestParam(value="pageSize",defaultValue="10") int pageSize,
                                        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") Date queryStartTime,
                                        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") Date queryEndTime,
                                        @RequestAttribute String subject,
                                        String token) {

        MessageResult<Map> messageResult = new MessageResult<>();

        try {
            Map<String,Object> searchParams = new HashMap<>();
            searchParams.put("createBy", subject);

            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("create_time","desc"));

            if(queryStartTime != null){
                searchParams.put("queryStartTime",queryStartTime);
            }

            if(queryEndTime != null){
                searchParams.put("queryEndTime",queryEndTime);
            }

            Page<AlarmInfo> page = alarmInfoService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);

            messageResult.setData(PojoUtils.pageWrapper(page));
            messageResult.setCode(200);
            messageResult.setMessage("查询成功");
            messageResult.setResult(true);

        } catch (Exception ex) {
            messageResult.setCode(400);
            messageResult.setMessage(ex.getMessage());
            messageResult.setResult(false);
        }

        return messageResult;
    }
}