package com.ruoyi.mall.service.impl;

import java.util.List;

import cn.hutool.crypto.SmUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallCompanyMapper;
import com.ruoyi.mall.domain.MallCompany;
import com.ruoyi.mall.service.IMallCompanyService;

import javax.annotation.Resource;

/**
 * 公司Service业务层处理
 *
 * @author LCL
 * @date 2024-03-12
 */
@Service
public class MallCompanyServiceImpl implements IMallCompanyService
{
    @Resource
    private MallCompanyMapper mallCompanyMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;

    /**
     * 查询公司
     *
     * @param companyId 公司主键
     * @return 公司
     */
    @Override
    public MallCompany selectMallCompanyByCompanyId(Long companyId)
    {
        return mallCompanyMapper.selectMallCompanyByCompanyId(companyId);
    }

    /**
     * 查询公司列表
     *
     * @param mallCompany 公司
     * @return 公司
     */
    @Override
    public List<MallCompany> selectMallCompanyList(MallCompany mallCompany)
    {
        return mallCompanyMapper.selectMallCompanyList(mallCompany);
    }

    /**
     * 新增公司
     *
     * @param mallCompany 公司
     * @return 结果
     */
    @Override
    public int insertMallCompany(MallCompany mallCompany)
    {
        //账号
        String account = mallCompany.getCompanyAccount();
        //密码
        String password = mallCompany.getCompanyPassword();

        mallCompany.setCompanyPassword(SecurityUtils.encryptPassword(password));

        // 新增系统用户
        SysUser sysUser = new SysUser();
        sysUser.setUserName(account);
        sysUser.setNickName(mallCompany.getCompanyName());
        sysUser.setStatus(mallCompany.getCompanyStatus());
        sysUser.setPassword(SecurityUtils.encryptPassword(password));
        sysUserMapper.insertUser(sysUser);

        // 新增用户与角色管理
        SysUserRole ur = new SysUserRole();
        ur.setUserId(sysUser.getUserId());
        ur.setRoleId(100L);// 账号类型（100-公司
        sysUserRoleMapper.insertUserRole(ur);

        mallCompany.setSysUserId(sysUser.getUserId());
        mallCompany.setCreateAt(DateUtils.getTime());
        return mallCompanyMapper.insertMallCompany(mallCompany);
    }

    /**
     * 修改公司
     *
     * @param mallCompany 公司
     * @return 结果
     */
    @Override
    public int updateMallCompany(MallCompany mallCompany)
    {
        mallCompany.setUpdateAt(DateUtils.getTime());
        return mallCompanyMapper.updateMallCompany(mallCompany);
    }

    /**
     * 批量删除公司
     *
     * @param companyIds 需要删除的公司主键
     * @return 结果
     */
    @Override
    public int deleteMallCompanyByCompanyIds(Long[] companyIds)
    {
        return mallCompanyMapper.deleteMallCompanyByCompanyIds(companyIds);
    }

    /**
     * 删除公司信息
     *
     * @param companyId 公司主键
     * @return 结果
     */
    @Override
    public int deleteMallCompanyByCompanyId(Long companyId)
    {
        return mallCompanyMapper.deleteMallCompanyByCompanyId(companyId);
    }

    /**
     * 根据公司账号查询公司
     *
     * @param companyAccount 公司账号
     * @return 公司
     */
    @Override
    public MallCompany selectMallCompanyByCompanyAccount(String companyAccount) {
        return mallCompanyMapper.selectMallCompanyByCompanyAccount(companyAccount);
    }

    @Override
    public MallCompany selectMallCompanyByCompanySysUserId(Long sysUserId) {
        return mallCompanyMapper.selectMallCompanyByCompanySysUserId(sysUserId);
    }
}
