package com.ruoyi.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.cms.mapper.*;
import com.ruoyi.cms.util.StringUtil;
import com.ruoyi.common.core.domain.entity.JobTitle;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SiteSecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.cms.service.IAppUserService;

/**
 * APP用户Service业务层处理
 *
 * @author lishundong
 * @date 2024-09-03
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper,AppUser> implements IAppUserService
{
    @Autowired
    private AppUserMapper appUserMapper;
    @Autowired
    private JobTitleMapper jobTitleMapper;
    @Autowired
    private CompanyContactMapper companyContactMapper;
    @Autowired
    private UserWorkExperiencesMapper userWorkExperiencesMapper;
    @Autowired
    private AppSkillMapper appSkillMapper;
    @Autowired
    private CompanyMapper companyMapper;
    /**
     * 查询APP用户
     *
     * @param userId APP用户主键
     * @return APP用户
     */
    @Override
    public AppUser selectAppUserByUserId(Long userId)
    {
        AppUser appUser = appUserMapper.selectById(userId);
        if(StringUtils.isNotEmpty(appUser.getJobTitleId())){
            List<String> list = Arrays.asList(appUser.getJobTitleId().split(","));
            List<Long> collect = list.stream().map(Long::valueOf).collect(Collectors.toList());
            List<JobTitle> jobs = jobTitleMapper.selectBatchIds(collect);
            appUser.setJobTitle(jobs.stream().map(JobTitle::getJobName).collect(Collectors.toList()));
        }
        return appUser;
    }

    /**
     * 查询APP用户列表
     *
     * @param appUser APP用户
     * @return APP用户
     */
    @Override
    public List<AppUser> selectAppUserList(AppUser appUser)
    {
        List<AppUser> list=appUserMapper.selectAppUserList(appUser);
        if(list!=null){
            //身份证脱敏处理
            list.forEach(x->{
                x.setIdCard(StringUtil.desensitizeIdCard(x.getIdCard()));
            });
        }
        return list;
    }

    /**
     * 新增APP用户
     *
     * @param appUser APP用户
     * @return 结果
     */
    @Override
    public int insertAppUser(AppUser appUser)
    {
        return appUserMapper.insert(appUser);
    }

    /**
     * 修改APP用户
     *
     * @param appUser APP用户
     * @return 结果
     */
    @Override
    public int updateAppUser(AppUser appUser)
    {
        return appUserMapper.updateById(appUser);
    }

    /**
     * 批量删除APP用户
     *
     * @param userIds 需要删除的APP用户主键
     * @return 结果
     */
    @Override
    public int deleteAppUserByUserIds(Long[] userIds)
    {
        return appUserMapper.deleteBatchIds(Arrays.asList(userIds));
    }

    @Override
    public AppUser getPhone(String phone) {
        return appUserMapper.selectOne(new LambdaQueryWrapper<AppUser>()
                .eq(AppUser::getPhone, phone));
    }

    @Override
    public AppUser selectByOpenid(String openid) {
        return appUserMapper.selectByOpenid(openid);
    }

    @Override
    public int registerAppUser(RegisterBody registerBody) {
        AppUser appUser=registerBody.getAppUser();
        appUser.setUserId(SiteSecurityUtils.getUserId());
        //角色集合
        Map mapUserRole=new HashMap<>();
        switch (appUser.getIsCompanyUser()){
            case "0"://企业
                if(registerBody.getCompany()!=null){
                    Long companyId=registerBody.getCompany().getCompanyId();
                    if(companyId==null){
                        companyMapper.insert(registerBody.getCompany());
                        registerBody.getCompany().getCompanyContactList().forEach(it->{
                            it.setCompanyId(registerBody.getCompany().getCompanyId());
                        });
                    }
                    if(registerBody.getCompany().getCompanyContactList()!=null){
                        companyContactMapper.batchInsert(registerBody.getCompany().getCompanyContactList());
                    }
                }
                mapUserRole.put("roleId",100);
                break;
            default://求职者
                if(registerBody.getExperiencesList()!=null){
                    userWorkExperiencesMapper.batchInsert(registerBody.getExperiencesList());
                }
                if(registerBody.getAppSkillsList()!=null){
                    appSkillMapper.batchInsert(registerBody.getAppSkillsList());
                }
                mapUserRole.put("roleId",1);
        }
        //保存sys_user
        SysUser sysUser=new SysUser();
        sysUser.setUserId(appUser.getUserId());
        sysUser.setUserName(appUser.getPhone());
        sysUser.setNickName(appUser.getName());
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        sysUser.setPhonenumber(appUser.getPhone());
        sysUser.setSex(appUser.getSex());
        sysUser.setStatus("0");
        sysUser.setLoginIp(appUser.getLoginIp());
        sysUser.setLoginDate(appUser.getLoginDate());
        appUserMapper.insertSysUser(sysUser);
        //保存sys_user_role
        mapUserRole.put("userId",appUser.getUserId());
        appUserMapper.insertSysUserRole(mapUserRole);
        return appUserMapper.updateById(appUser);
    }
}
