package com.zcloud.basic.info.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.jjb.saas.security.starter.api.co.DecryptCmd;
import com.zcloud.basic.info.api.CorpInfoServiceI;
import com.zcloud.basic.info.dto.*;
import com.zcloud.basic.info.dto.clientobject.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * web-adapter
 *
 * @Author zhaokai
 * @Date 2025-10-29 16:02:16
 */
@Api(tags = "企业信息")
@RequestMapping("/${application.gateway}/corpInfo")
@RestController
@AllArgsConstructor
public class CorpInfoController {
    private final CorpInfoServiceI corpInfoService;

    @ApiOperation("新增")
    @PostMapping("/save")
    public SingleResponse<CorpInfoCO> add(@Validated @RequestBody CorpInfoAddCmd cmd) {
        return corpInfoService.add(cmd);
    }


    @ApiOperation("新增")
    @GetMapping("/deleteCorpAndDown/{ids}")
    public void deleteCorpAndDown(@PathVariable("ids") List<Long> ids) {
         corpInfoService.deleteCorpAndDown(ids);
    }

    @ApiOperation("判断统一社会信用代码是否重复, true:没重复, false:有重复")
    @PostMapping("/checkCorpCode")
    public SingleResponse<Object> checkCorpCode(@Validated @RequestBody CorpInfoCheckCodeQry qry) {
        return SingleResponse.of(corpInfoService.checkCorpCode(qry));
    }

    @ApiOperation("判断企业名称是否重复, true:没重复, false:有重复")
    @PostMapping("/checkCorpName")
    public SingleResponse<Object> checkCorpName(@Validated @RequestBody CorpInfoCheckNameQry qry) {
        return SingleResponse.of(corpInfoService.checkCorpName(qry));
    }

    @ApiOperation("分页")
    @PostMapping("/list")
    public PageResponse<CorpInfoCO> page(@Validated @RequestBody CorpInfoPageQry qry) {
        return corpInfoService.listPage(qry);
    }
    @ApiOperation("分页")
    @PostMapping("/basicList")
    @PreAuthorize("@pms.hasAnyPermission('zgszhgl','zgsqyxxgl','gszhxxgl','qyd-xgf-zhgl')")
    public PageResponse<CorpInfoCO> pageBasic(@Validated @RequestBody CorpInfoPageQry qry) {
        return corpInfoService.listPageBasic(qry);
    }

    @ApiOperation("分页")
    @PostMapping("/listNoPms")
    public PageResponse<CorpInfoCO> list(@Validated @RequestBody CorpInfoPageQry qry) {
        return corpInfoService.listPageNoPms(qry);
    }
    @ApiOperation("所有数据")
    @GetMapping("/listAll")
    public MultiResponse<CorpInfoCO> listAll(CorpInfoQry corpInfoQry) {
        return corpInfoService.listAll(corpInfoQry);
    }

    @ApiOperation("详情")
    @PostMapping("/info/{id}")
    public SingleResponse<CorpInfoCO> getInfoById(@PathVariable("id") Long id) {
        if(id==null){
            SSOUser ssoUser = AuthContext.getCurrentUser();
            id = ssoUser.getTenantId();
        }
        return SingleResponse.of(corpInfoService.info(id));
    }


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

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

    @ApiOperation("修改")
    @PostMapping("/edit")
    public SingleResponse edit(@Validated @RequestBody CorpInfoUpdateCmd corpInfoUpdateCmd) {
        corpInfoService.edit(corpInfoUpdateCmd);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("监管端-分子公司部门管理首页")
    @PostMapping("/corpDepartlist")
    @PreAuthorize("@pms.hasAnyPermission('zgsbmgl')")
    public PageResponse<CorpDepartmentCO> corpDepartlist(@Validated @RequestBody CorpDepartmentQry qry) {
        return corpInfoService.corpDepartlist(qry);
    }

    @ApiOperation("修改密码")
    @PostMapping("/changePassword/{id}")
    public SingleResponse changePassword(@PathVariable Long id){
        corpInfoService.changePassword(id);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("修改公司名称")
    @PostMapping("/changeCompanyName")
    public SingleResponse changeCompanyName(@Validated @RequestBody CorpInfoUpdateCmd corpInfoUpdateCmd){
        corpInfoService.changeCompanyName(corpInfoUpdateCmd);
        return SingleResponse.buildSuccess();
    }
    @ApiOperation("启用停用企业")
    @PostMapping("/changeUseFlag")
    public SingleResponse changeEnableFlag(@Validated @RequestBody CorpInfoUpdateUseFlagCmd corpInfoUpdateUseFlagCmd){
        corpInfoService.changeEnableFlag(corpInfoUpdateUseFlagCmd);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("企业用户中台-首页分页列表")
    @PostMapping("/corpUserMiddlePage")
    @PreAuthorize("@pms.hasAnyPermission('qyyhzt','zgsyhgl','ryxxgl','qyd-xgf-yhgl')")
    public PageResponse<CorpUserMiddleCO> corpUserMiddlePage(@Validated @RequestBody CorpUserMiddleQry qry) {
        return corpInfoService.corpUserMiddlePage(qry);
    }
    @ApiOperation("企业信息是否完善")
    @PostMapping("/verifyCorpInfo")
    public SingleResponse<Boolean> verifyCorpInfo() {
        return SingleResponse.of(corpInfoService.verifyCorpInfo());
    }
    @ApiOperation("根据登录人密码获取相关信息")
    @PostMapping("/getEncryInfo")
    public SingleResponse<DecryptCmd> getEncryInfo(@Validated @RequestBody UserCheckPassWordCmd userCheckPassWordCmd) {
        return SingleResponse.of(corpInfoService.getEncryInfo(userCheckPassWordCmd));
    }

}

