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.AlarmRuleServiceI;
import com.zcloud.personnel.positioning.dto.AlarmRuleAddCmd;
import com.zcloud.personnel.positioning.dto.AlarmRuleCompanyPageQry;
import com.zcloud.personnel.positioning.dto.AlarmRulePageQry;
import com.zcloud.personnel.positioning.dto.AlarmRuleUpdateCmd;
import com.zcloud.personnel.positioning.dto.clientobject.AlarmRuleCO;
import com.zcloud.personnel.positioning.dto.clientobject.AlarmRuleCompanyStatCO;
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;

/**
 * web-adapter
 *
 * @Author fangjiakai
 * @Date 2026-07-07
 */
@Api(tags = "人员定位告警规则")
@RequestMapping("/${application.gateway}/alarmRule")
@RestController
@AllArgsConstructor
public class AlarmRuleController {
    private final AlarmRuleServiceI alarmRuleService;

    @ApiOperation("公司配置统计分页")
    @PostMapping("/company/list")
    public PageResponse<AlarmRuleCompanyStatCO> companyPage(@RequestBody AlarmRuleCompanyPageQry qry) {
        return alarmRuleService.listCompanyStats(qry);
    }

    @ApiOperation("公司选项")
    @GetMapping("/company/options")
    public MultiResponse<OptionCO> companyOptions(@RequestParam(required = false) String keyword) {
        return MultiResponse.of(alarmRuleService.listCompanyOptions(keyword));
    }

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

    @ApiOperation("配置明细分页")
    @PostMapping("/list")
    public PageResponse<AlarmRuleCO> page(@RequestBody AlarmRulePageQry qry) {
        return alarmRuleService.listPage(qry);
    }

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

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

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

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

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

    @ApiOperation("启用")
    @PutMapping("/{id}/enable")
    public Response enable(@PathVariable("id") Long id) {
        alarmRuleService.enable(id);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("停用")
    @PutMapping("/{id}/disable")
    public Response disable(@PathVariable("id") Long id) {
        alarmRuleService.disable(id);
        return SingleResponse.buildSuccess();
    }

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

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