package com.ruoyi.app.controller.common;

import com.ruoyi.common.constant.AppUserLeaderEnum;
import com.ruoyi.common.constant.AppUserTypeEnum;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.JWTUtil;
import com.ruoyi.system.domain.AppLogin;
import com.ruoyi.system.dto.ContractorUserDTO;
import com.ruoyi.system.service.IAppLoginService;
import com.ruoyi.system.service.IContractorUserService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 登录相关接口Controller
 *
 * @author ruoyi
 * @date 2023-08-04
 */
@Api(tags = "APP登录相关接口")
@RestController
@RequestMapping("/app")
public class LoginForAppController extends BaseController {
    @Autowired
    private IAppLoginService iAppLoginService;
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IContractorUserService contractorUserService;


    @ApiOperation("APP端登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "登录用户名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "登录密码", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Map<String, String> map) {
        String userName = map.get("userName");
        String password = map.get("password");
        return iAppLoginService.loginForApp(userName, password);
    }

    @ApiOperation("APP当前登录用户信息")
    @GetMapping("/getInfo")
    public AjaxResult getInfo() {
        String id = JWTUtil.getUserId();
        String userName = JWTUtil.getUserName();

        String appUserType = JWTUtil.getAppUserType();
        String pcUserId = JWTUtil.getPCUserId();

        AppLogin appLogin = new AppLogin();
        // APP用户表主键 tb_app_login.id
        appLogin.setId(Long.parseLong(id));
        // 用户类型：1内部，2承包商
        appLogin.setUserType(Integer.parseInt(appUserType));
        if (appUserType.equals(AppUserTypeEnum.USER.getValue().toString())) {
            // 内部用户ID sys_user.user_id
            appLogin.setUserId(Long.parseLong(pcUserId));

            //设置部门ID和部门名称
            SysUser sysUser = sysUserService.selectUserById(Long.parseLong(pcUserId));
            if (sysUser != null) {
                appLogin.setDeptId(sysUser.getDeptId());
                appLogin.setDeptName(sysUser.getDeptName());
            }
        }
        //承包商名称
        else if (appUserType.equals(AppUserTypeEnum.USER.getValue().toString())) {
            String contractorUserId = appLogin.getContractorUserId();
            if (StringUtils.isNotBlank(contractorUserId)) {
                ContractorUserDTO contractorUser = new ContractorUserDTO();
                contractorUser.setId(contractorUserId);
                List<ContractorUserDTO> contractorUserDTOS = contractorUserService.selectContractorUserList(contractorUser);
                if (!CollectionUtils.isEmpty(contractorUserDTOS)) {
                    appLogin.setContractorName(contractorUserDTOS.get(0).getCompanyName());
                }

            }

        }
        appLogin.setUserName(userName);
        //是否领导：10-领导；20-员工；
        String leader = JWTUtil.isLeader() ? AppUserLeaderEnum.LEADER.getValue() : AppUserLeaderEnum.EMPLOYEES.getValue();
        appLogin.setLeader(leader);

//        System.out.println("APP用户ID = " + JWTUtil.getUserId());
//        System.out.println("系统用户ID = " + JWTUtil.getPCUserId());
//        System.out.println("承包商用户ID = " + JWTUtil.getContractorUserId());
//        System.out.println("类型（1内部，2承包商） = " + JWTUtil.getAppUserType());
//        System.out.println("用户名 = " + JWTUtil.getUserName());
//        System.out.println("昵称 = " + JWTUtil.getNickName());


        AjaxResult result = new AjaxResult(HttpStatus.SUCCESS, "操作成功", appLogin);
        return result;
    }

}
