package com.zcloud.personnel.positioning.web;

import com.zcloud.personnel.positioning.api.TerminalServiceI;
import com.zcloud.personnel.positioning.dto.TerminalAddCmd;
import com.zcloud.personnel.positioning.dto.TerminalAppCompanyPageQry;
import com.zcloud.personnel.positioning.dto.TerminalAppPageQry;
import com.zcloud.personnel.positioning.dto.TerminalAppSaveCmd;
import com.zcloud.personnel.positioning.dto.TerminalAppSyncCmd;
import com.zcloud.personnel.positioning.dto.TerminalAppUpdateCmd;
import com.zcloud.personnel.positioning.dto.TerminalCardCompanyOptionQry;
import com.zcloud.personnel.positioning.dto.TerminalCardCompanyPageQry;
import com.zcloud.personnel.positioning.dto.TerminalCardPageQry;
import com.zcloud.personnel.positioning.dto.TerminalCardPersonOptionQry;
import com.zcloud.personnel.positioning.dto.TerminalCardSaveCmd;
import com.zcloud.personnel.positioning.dto.TerminalCardSyncCmd;
import com.zcloud.personnel.positioning.dto.TerminalCardUpdateCmd;
import com.zcloud.personnel.positioning.dto.TerminalPageQry;
import com.zcloud.personnel.positioning.dto.TerminalUpdateCmd;
import com.zcloud.personnel.positioning.dto.clientobject.TerminalAppCO;
import com.zcloud.personnel.positioning.dto.clientobject.TerminalAppCompanyCO;
import com.zcloud.personnel.positioning.dto.clientobject.TerminalAppSyncCO;
import com.zcloud.personnel.positioning.dto.clientobject.TerminalCardCO;
import com.zcloud.personnel.positioning.dto.clientobject.TerminalCardCompanyCO;
import com.zcloud.personnel.positioning.dto.clientobject.TerminalCardCompanyOptionCO;
import com.zcloud.personnel.positioning.dto.clientobject.TerminalCardPersonOptionCO;
import com.zcloud.personnel.positioning.dto.clientobject.TerminalCardSyncCO;
import com.zcloud.personnel.positioning.dto.clientobject.TerminalCO;
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}/terminal")
@RestController
@AllArgsConstructor
public class TerminalController {
    private final TerminalServiceI terminalService;

    @ApiOperation("APP定位公司分页查询")
    @PostMapping("/app/company/list")
    public PageResponse<TerminalAppCompanyCO> appCompanyPage(@RequestBody TerminalAppCompanyPageQry qry) {
        return terminalService.listAppCompanyPage(qry);
    }

    @ApiOperation("APP定位公司选项")
    @PostMapping("/app/company/options")
    public MultiResponse<TerminalCardCompanyOptionCO> appCompanyOptions(@RequestBody(required = false) TerminalCardCompanyOptionQry qry) {
        return MultiResponse.of(terminalService.listCardCompanyOptions(qry));
    }

    @ApiOperation("APP定位分页查询")
    @PostMapping("/app/list")
    public PageResponse<TerminalAppCO> appPage(@RequestBody TerminalAppPageQry qry) {
        return terminalService.listAppPage(qry);
    }

    @ApiOperation("从FindS同步APP定位数据")
    @PostMapping("/app/sync")
    public SingleResponse<TerminalAppSyncCO> appSync(@RequestBody(required = false) TerminalAppSyncCmd cmd) {
        return SingleResponse.of(terminalService.syncApps(cmd));
    }

    @ApiOperation("APP定位人员选项")
    @PostMapping("/app/person/options")
    public PageResponse<TerminalCardPersonOptionCO> appPersonOptions(@RequestBody TerminalCardPersonOptionQry qry) {
        return terminalService.listCardPersonOptions(qry);
    }

    @ApiOperation("新增APP定位账号")
    @PostMapping("/app/save")
    public SingleResponse<TerminalAppCO> appAdd(@Validated @RequestBody TerminalAppSaveCmd cmd) {
        fillCorpinfo(cmd);
        return SingleResponse.of(terminalService.saveApp(cmd));
    }

    @ApiOperation("修改APP定位账号")
    @PutMapping("/app/edit")
    public SingleResponse<TerminalAppCO> appEdit(@Validated @RequestBody TerminalAppUpdateCmd cmd) {
        fillCorpinfo(cmd);
        return SingleResponse.of(terminalService.updateApp(cmd));
    }

    @ApiOperation("APP定位详情")
    @GetMapping("/app/{id}")
    public SingleResponse<TerminalAppCO> appDetail(@PathVariable("id") Long id) {
        return SingleResponse.of(terminalService.queryAppById(id));
    }

    @ApiOperation("定位卡公司分页查询")
    @PostMapping("/card/company/list")
    public PageResponse<TerminalCardCompanyCO> cardCompanyPage(@RequestBody TerminalCardCompanyPageQry qry) {
        return terminalService.listCardCompanyPage(qry);
    }

    @ApiOperation("定位卡公司选项")
    @PostMapping("/card/company/options")
    public MultiResponse<TerminalCardCompanyOptionCO> cardCompanyOptions(@RequestBody(required = false) TerminalCardCompanyOptionQry qry) {
        return MultiResponse.of(terminalService.listCardCompanyOptions(qry));
    }

    @ApiOperation("定位卡分页查询")
    @PostMapping("/card/list")
    public PageResponse<TerminalCardCO> cardPage(@RequestBody TerminalCardPageQry qry) {
        return terminalService.listCardPage(qry);
    }

    @ApiOperation("从FindS同步定位卡数据")
    @PostMapping("/card/sync")
    public SingleResponse<TerminalCardSyncCO> cardSync(@RequestBody(required = false) TerminalCardSyncCmd cmd) {
        return SingleResponse.of(terminalService.syncCards(cmd));
    }

    @ApiOperation("定位卡人员选项")
    @PostMapping("/card/person/options")
    public PageResponse<TerminalCardPersonOptionCO> cardPersonOptions(@RequestBody TerminalCardPersonOptionQry qry) {
        return terminalService.listCardPersonOptions(qry);
    }

    @ApiOperation("新增定位卡")
    @PostMapping("/card/save")
    public SingleResponse<TerminalCardCO> cardAdd(@Validated @RequestBody TerminalCardSaveCmd cmd) {
        fillCorpinfo(cmd);
        return SingleResponse.of(terminalService.saveCard(cmd));
    }

    @ApiOperation("修改定位卡")
    @PutMapping("/card/edit")
    public SingleResponse<TerminalCardCO> cardEdit(@Validated @RequestBody TerminalCardUpdateCmd cmd) {
        fillCorpinfo(cmd);
        return SingleResponse.of(terminalService.updateCard(cmd));
    }

    @ApiOperation("定位卡详情")
    @GetMapping("/card/{id}")
    public SingleResponse<TerminalCardCO> cardDetail(@PathVariable("id") Long id) {
        return SingleResponse.of(terminalService.queryCardById(id));
    }

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

    @ApiOperation("分页")
    @PostMapping("/list")
    public PageResponse<TerminalCO> page(@RequestBody TerminalPageQry qry) {
        return terminalService.listPage(qry);
    }

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

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

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

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

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

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

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

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

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