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.FenceServiceI;
import com.zcloud.personnel.positioning.dto.FenceAddCmd;
import com.zcloud.personnel.positioning.dto.FenceCompanyAreaSaveCmd;
import com.zcloud.personnel.positioning.dto.FenceCompanyPageQry;
import com.zcloud.personnel.positioning.dto.FencePageQry;
import com.zcloud.personnel.positioning.dto.FenceSyncCmd;
import com.zcloud.personnel.positioning.dto.FenceUpdateCmd;
import com.zcloud.personnel.positioning.dto.clientobject.FenceCO;
import com.zcloud.personnel.positioning.dto.clientobject.FenceCompanyAreaCO;
import com.zcloud.personnel.positioning.dto.clientobject.FenceCompanyStatCO;
import com.zcloud.personnel.positioning.dto.clientobject.FenceSyncCO;
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;

@Api(tags = "人员定位围栏")
@RequestMapping("/${application.gateway}/fence")
@RestController
@AllArgsConstructor
public class FenceController {
    private final FenceServiceI fenceService;

    @ApiOperation("公司围栏统计")
    @PostMapping("/company/list")
    public PageResponse<FenceCompanyStatCO> companyPage(@RequestBody FenceCompanyPageQry qry) {
        return fenceService.listCompanyStats(qry);
    }

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

    @ApiOperation("公司区域详情")
    @GetMapping("/company-area")
    public SingleResponse<FenceCompanyAreaCO> getCompanyArea(@RequestParam(required = false) String corpinfoId,
                                                             @RequestParam(required = false) String corpinfoName) {
        SSOUser ssoUser = AuthContext.getCurrentUser();
        if (corpinfoId == null && ssoUser != null && ssoUser.getCompanyId() != null) {
            corpinfoId = String.valueOf(ssoUser.getCompanyId());
        }
        return SingleResponse.of(fenceService.queryCompanyArea(corpinfoId, corpinfoName));
    }

    @ApiOperation("保存公司区域")
    @PostMapping("/company-area/save")
    public SingleResponse<FenceCompanyAreaCO> saveCompanyArea(@Validated @RequestBody FenceCompanyAreaSaveCmd cmd) {
        fillCorpinfo(cmd);
        return SingleResponse.of(fenceService.saveCompanyArea(cmd));
    }

    @ApiOperation("新增围栏")
    @PostMapping("/save")
    public SingleResponse<FenceCO> add(@Validated @RequestBody FenceAddCmd cmd) {
        fillCorpinfo(cmd);
        return fenceService.add(cmd);
    }

    @ApiOperation("从FindS拉取围栏")
    @PostMapping("/sync")
    public SingleResponse<FenceSyncCO> sync(@RequestBody(required = false) FenceSyncCmd cmd) {
        return SingleResponse.of(fenceService.syncFromFinds(cmd));
    }

    @ApiOperation("围栏分页查询")
    @PostMapping("/list")
    public PageResponse<FenceCO> page(@RequestBody FencePageQry qry) {
        return fenceService.listPage(qry);
    }

    @ApiOperation("查询全部围栏")
    @GetMapping("/listAll")
    public MultiResponse<FenceCO> listAll() {
        return MultiResponse.of(fenceService.listAll());
    }

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

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

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

    @ApiOperation("修改围栏")
    @PutMapping("/edit")
    public SingleResponse edit(@Validated @RequestBody FenceUpdateCmd cmd) {
        fillCorpinfo(cmd);
        fenceService.edit(cmd);
        return SingleResponse.buildSuccess();
    }

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

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

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

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

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