package com.zcloud.personnel.positioning.web;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.jjb.saas.framework.auth.model.SSOUser;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.personnel.positioning.api.AlarmRecordServiceI;
import com.zcloud.personnel.positioning.dto.AlarmRecordAddCmd;
import com.zcloud.personnel.positioning.dto.AlarmRecordBiAlarmTypeQry;
import com.zcloud.personnel.positioning.dto.AlarmRecordBiQry;
import com.zcloud.personnel.positioning.dto.AlarmRecordPageQry;
import com.zcloud.personnel.positioning.dto.AlarmRecordUpdateCmd;
import com.zcloud.personnel.positioning.dto.clientobject.AlarmRecordCO;
import com.zcloud.personnel.positioning.dto.clientobject.AlarmTypeStatCO;
import com.zcloud.personnel.positioning.dto.clientobject.OptionCO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 告警记录接口。
 *
 * @Author fangjiakai
 * @Date 2026-07-07
 */
@Api(tags = "人员定位告警记录")
@RequestMapping("/${application.gateway}/alarmRecord")
@RestController
@AllArgsConstructor
public class AlarmRecordController {
    private final AlarmRecordServiceI alarmRecordService;

    @ApiOperation("新增")
    @PostMapping("/save")
    public SingleResponse<AlarmRecordCO> add(@Validated @RequestBody AlarmRecordAddCmd cmd) {
        fillCorpinfo(cmd);
        return alarmRecordService.add(cmd);
    }

    @ApiOperation("分页查询")
    @PostMapping("/list")
    public PageResponse<AlarmRecordCO> page(@RequestBody AlarmRecordPageQry qry) {
        return alarmRecordService.listPage(qry);
    }

    @ApiOperation("查询全部")
    @GetMapping("/listAll")
    public MultiResponse<AlarmRecordCO> listAll() {
        return MultiResponse.of(alarmRecordService.listAll());
    }


    @ApiOperation("公司选项")
    @GetMapping("/companyOptions")
    public MultiResponse<OptionCO> companyOptions() {
        return MultiResponse.of(alarmRecordService.listCompanyOptions());
    }

    @ApiOperation("告警类型选项")
    @GetMapping("/alarmTypeOptions")
    public MultiResponse<OptionCO> alarmTypeOptions() {
        return MultiResponse.of(alarmRecordService.listAlarmTypeOptions());
    }

    @ApiOperation("详情")
    @GetMapping("/{id}")
    public SingleResponse<AlarmRecordCO> getInfoById(@PathVariable("id") Long id) {
        return SingleResponse.of(alarmRecordService.queryById(id));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Response remove(@PathVariable("id") Long id) {
        alarmRecordService.remove(id);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/ids")
    public Response removeBatch(@RequestParam Long[] ids) {
        alarmRecordService.removeBatch(ids);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("修改")
    @PutMapping("/edit")
    public SingleResponse edit(@Validated @RequestBody AlarmRecordUpdateCmd cmd) {
        fillCorpinfo(cmd);
        alarmRecordService.edit(cmd);
        return SingleResponse.buildSuccess();
    }

    private void fillCorpinfo(AlarmRecordAddCmd cmd) {
        SSOUser ssoUser = AuthContext.getCurrentUser();
        if (ssoUser != null && ssoUser.getCompanyId() != null) {
            cmd.setCorpinfoId(String.valueOf(ssoUser.getCompanyId()));
        }
    }

    private void fillCorpinfo(AlarmRecordUpdateCmd cmd) {
        SSOUser ssoUser = AuthContext.getCurrentUser();
        if (ssoUser != null && ssoUser.getCompanyId() != null) {
            cmd.setCorpinfoId(String.valueOf(ssoUser.getCompanyId()));
        }
    }

    @ApiOperation("可视化大屏-报警列表")
    @PostMapping("/bi/list")
    public MultiResponse<AlarmRecordCO> listBiAlarm(@RequestBody AlarmRecordBiQry qry) {
        return MultiResponse.of(alarmRecordService.listBiAlarm(qry));
    }

    @ApiOperation("可视化大屏-报警类型")
    @PostMapping("/bi/alarmType")
    public MultiResponse<AlarmTypeStatCO> listBiAlarmType(@RequestBody AlarmRecordBiAlarmTypeQry qry) {
        return MultiResponse.of(alarmRecordService.listBiAlarmType(qry));
    }

}
