package com.zcloud.personnel.positioning.web;

import com.zcloud.personnel.positioning.api.AlarmRecordTargetServiceI;
import com.zcloud.personnel.positioning.dto.AlarmRecordTargetAddCmd;
import com.zcloud.personnel.positioning.dto.AlarmRecordTargetPageQry;
import com.zcloud.personnel.positioning.dto.AlarmRecordTargetUpdateCmd;
import com.zcloud.personnel.positioning.dto.clientobject.AlarmRecordTargetCO;
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 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;

/**
 * web-adapter
 *
 * @Author fangjiakai
 * @Date 2026-07-07
 */
@Api(tags = "告警主体明细")
@RequestMapping("/${application.gateway}/alarmRecordTarget")
@RestController
@AllArgsConstructor
public class AlarmRecordTargetController {
    private final AlarmRecordTargetServiceI alarmRecordTargetService;

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

    @ApiOperation("分页")
    @PostMapping("/list")
    public PageResponse<AlarmRecordTargetCO> page(@RequestBody AlarmRecordTargetPageQry qry) {
        return alarmRecordTargetService.listPage(qry);
    }

    @ApiOperation("所有数据")
    @GetMapping("/listAll")
    public MultiResponse<AlarmRecordTargetCO> listAll() {
        return MultiResponse.of(alarmRecordTargetService.listAll());
    }

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

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

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

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

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

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