package com.zhgd.xmgl.modules.basicdata.controller.enterprise;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.xmgl.modules.basicdata.entity.Enterprise;
import com.zhgd.xmgl.modules.basicdata.entity.SystemUser;
import com.zhgd.xmgl.modules.basicdata.service.IEnterpriseService;
import com.zhgd.xmgl.modules.basicdata.service.ISystemUserService;
import com.zhgd.xmgl.modules.basicdata.vo.EnterpriseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Title: Controller
 * @Description: 企业信息
 * @author： pds
 * @date： 2023-02-15
 * @version： V1.0
 */
@RestController
@RequestMapping("/ent/enterprise")
@Slf4j
@Api(tags = "企业信息")
public class EntEnterpriseController {
    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private ISystemUserService systemUserService;

    /**
     * 企业注册申请
     *
     * @param enterpriseVo
     * @return
     */
    @OperLog(operModul = "企业管理", operType = "新增", operDesc = "企业注册申请")
    @ApiOperation(value = " 企业注册申请", notes = "企业注册申请", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<Enterprise> add(@RequestBody EnterpriseVo enterpriseVo) {
        Result<Enterprise> result = new Result<Enterprise>();
        // 检验企业账号是否已存在
        SystemUser systemUser = systemUserService.getOne(Wrappers.<SystemUser>lambdaQuery().eq(SystemUser::getAccount, enterpriseVo.getAccount()));
        if (systemUser != null) {
            result.error500("该用户账号已被使用");
            return result;
        }
        enterpriseService.saveInfo(enterpriseVo);
        result.success("添加成功！");
        return result;
    }

    @OperLog(operModul = "企业管理", operType = "查询", operDesc = "通过id查询企业信息")
    @ApiOperation(value = "通过id查询企业信息", notes = "通过id查询企业信息", httpMethod = "POST")
    @ApiImplicitParam(name = "enterpriseId", value = "企业信息ID", paramType = "query", required = true, dataType = "String")
    @PostMapping(value = "/queryById")
    public Result<Enterprise> queryById(@RequestBody Enterprise enterpriseEntity) {
        Result<Enterprise> result = new Result<Enterprise>();
        Enterprise enterprise = enterpriseService.getById(enterpriseEntity.getEnterpriseId());
        if (enterprise == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(enterprise);
            result.setSuccess(true);
        }
        return result;
    }

    @OperLog(operModul = "企业管理", operType = "查询", operDesc = "通过id查询企业详细信息")
    @ApiOperation(value = "通过id查询企业详细信息", notes = "通过id查询企业详细信息", httpMethod = "POST")
    @ApiImplicitParam(name = "enterpriseId", value = "企业信息ID", paramType = "query", required = true, dataType = "String")
    @PostMapping(value = "/getDetailById")
    public Result<EnterpriseVo> getDetailById(@RequestBody Enterprise enterpriseEntity) {
        Result<EnterpriseVo> result = new Result<EnterpriseVo>();
        Enterprise enterprise = enterpriseService.getById(enterpriseEntity.getEnterpriseId());
        if (enterprise == null) {
            result.error500("未找到对应实体");
        } else {
            EnterpriseVo enterpriseVo = enterpriseService.getDetailById(enterpriseEntity.getEnterpriseId());
            result.setResult(enterpriseVo);
            result.setSuccess(true);
        }
        return result;
    }
}
