package com.zcloud.personnel.positioning.web;

import com.zcloud.personnel.positioning.api.AreaServiceI;
import com.zcloud.personnel.positioning.dto.AreaAddCmd;
import com.zcloud.personnel.positioning.dto.AreaPageQry;
import com.zcloud.personnel.positioning.dto.AreaUpdateCmd;
import com.zcloud.personnel.positioning.dto.BiPersonPositionQry;
import com.zcloud.personnel.positioning.dto.clientobject.AreaCO;
import com.zcloud.personnel.positioning.dto.clientobject.AreaPersonStatCO;
import com.zcloud.personnel.positioning.dto.clientobject.OptionCO;
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}/area")
@RestController
@AllArgsConstructor
public class AreaController {
    private final AreaServiceI areaService;

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

    @ApiOperation("分页")
    @PostMapping("/list")
    public PageResponse<AreaCO> page(@RequestBody AreaPageQry qry) {
        return areaService.listPage(qry);
    }

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

    @ApiOperation("可选区域选项")
    @GetMapping("/options")
    public MultiResponse<OptionCO> options(@RequestParam(required = false) String corpinfoId,
                                           @RequestParam(required = false) String keyword) {
        return MultiResponse.of(areaService.listOptions(corpinfoId, keyword));
    }


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

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

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

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

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

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


    @ApiOperation("可视化大屏-封闭区域人员统计情况")
    @PostMapping("/personStat")
    public MultiResponse<AreaPersonStatCO> listPersonStat(@RequestBody BiPersonPositionQry qry) {
        return MultiResponse.of(areaService.listPersonStat(qry.getCorpinfoId()));
    }
}
