package cn.ezhizao.project.system.controller;

import cn.ezhizao.common.constant.Constants;
import cn.ezhizao.common.utils.SecurityUtils;
import cn.ezhizao.framework.security.LoginBody;
import cn.ezhizao.framework.security.service.SysLoginService;
import cn.ezhizao.framework.security.service.SysPermissionService;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.project.business.domain.BizSupplier;
import cn.ezhizao.project.business.domain.BizSupplierUser;
import cn.ezhizao.project.business.service.IBizSupplierService;
import cn.ezhizao.project.business.service.IBizSupplierUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
public class SysLoginController extends BaseController
{
    @Resource
    private SysLoginService loginService;

    @Resource
    private SysPermissionService permissionService;

    @Resource
    private IBizSupplierUserService bizSupplierUserService;
    @Resource
    private IBizSupplierService bizSupplierService;

    /**
     * 根据助记码查询供应商信息
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/getSupplierByMnemonicCode")
    public AjaxResult getSupplierByMnemonicCode(@RequestBody LoginBody loginBody)
    {
        // 正常登录
        List<BizSupplier> suppliers = bizSupplierService.query().eq("mnemonic_code", loginBody.getMnemonicCode()).list();
        if (suppliers.isEmpty())
        {
            return AjaxResult.error(201, "助记码错误");
        } else {
            if (suppliers.size() == 1)
            {
                BizSupplier supplier = suppliers.get(0);
                return AjaxResult.success(supplier);
            } else {
                return AjaxResult.error(201, "助记码有误，匹配到多个企业");
            }
        }
    }
    @PostMapping("/getSupplierByCode")
    public AjaxResult getSupplierByCode(@RequestBody LoginBody loginBody)
    {
        // 正常登录
        List<BizSupplier> suppliers = bizSupplierService.query().eq("code", loginBody.getSupplierCode()).list();
        if (suppliers.isEmpty())
        {
            return AjaxResult.error(201, "外协商编码错误");
        } else {
            if (suppliers.size() == 1)
            {
                BizSupplier supplier = suppliers.get(0);
                return AjaxResult.success(supplier);
            } else {
                return AjaxResult.error(201, "供应商编码有误，匹配到多个企业");
            }
        }
    }
    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 正常登录
        Map<String, Object> loginResult = loginService.login(loginBody.getUsername(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, loginResult.get(Constants.TOKEN));
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        BizSupplierUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = new HashSet<>();
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        BizSupplier supplier = bizSupplierService.getById(user.getSupplierId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("supplier", supplier);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        return AjaxResult.success(new ArrayList<>());
    }

    /**
     * 根据企业名称 模糊查询租户
     * @param orgName 公司名
     * @return AjaxResult
     */
    @GetMapping("/snms/getSupplierListByOrgName/{orgName}")
    public AjaxResult getTenantListByOrgName(@PathVariable("orgName") String orgName){
        return success(bizSupplierService.query().like("name",orgName).list());
    }
}
