package com.ruoyi.mall.service.impl;

import java.util.ArrayList;
import java.util.List;

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.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallShopSupplierMapper;
import com.ruoyi.mall.domain.MallShopSupplier;
import com.ruoyi.mall.service.IMallShopSupplierService;

import javax.annotation.Resource;

/**
 * 商家供应商管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-19
 */
@Service
public class MallShopSupplierServiceImpl implements IMallShopSupplierService
{
    @Resource
    private MallShopSupplierMapper mallShopSupplierMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询商家供应商管理
     *
     * @param supplierId 商家供应商管理主键
     * @return 商家供应商管理
     */
    @Override
    public MallShopSupplier selectMallShopSupplierBySupplierId(Long supplierId)
    {
        return mallShopSupplierMapper.selectMallShopSupplierBySupplierId(supplierId);
    }

    /**
     * 查询商家供应商管理列表
     *
     * @param mallShopSupplier 商家供应商管理
     * @return 商家供应商管理
     */
    @Override
    public List<MallShopSupplier> selectMallShopSupplierList(MallShopSupplier mallShopSupplier)
    {
        return mallShopSupplierMapper.selectMallShopSupplierList(mallShopSupplier);
    }

    /**
     * 新增商家供应商管理
     *
     * @param mallShopSupplier 商家供应商管理
     * @return 结果
     */
    @Override
    public int insertMallShopSupplier(MallShopSupplier mallShopSupplier)
    {
        String account = mallShopSupplier.getSupplierAccount();// 账号
        String password = mallShopSupplier.getSupplierPassword();// 密码

        // 新增系统用户
        SysUser sysUser = new SysUser();
        sysUser.setUserName(account);
        sysUser.setNickName(mallShopSupplier.getSupplierName());
        sysUser.setPhonenumber(mallShopSupplier.getSupplierPhone());
        sysUser.setStatus(mallShopSupplier.getSupplierStatus().toString());
        sysUser.setPassword(SecurityUtils.encryptPassword(password));
        sysUserMapper.insertUser(sysUser);

        // 新增用户与角色管理
        List<SysUserRole> userRoleList = new ArrayList<>();
        SysUserRole ur = new SysUserRole();
        ur.setUserId(sysUser.getUserId());
        ur.setRoleId(7L);// 账号类型（7-供应商
        userRoleList.add(ur);
        sysUserRoleMapper.batchUserRole(userRoleList);

        mallShopSupplier.setSysUserId(sysUser.getUserId());
        mallShopSupplier.setDelFlag(0);
        mallShopSupplier.setCreateAt(DateUtils.getTime());
        return mallShopSupplierMapper.insertMallShopSupplier(mallShopSupplier);
    }

    /**
     * 修改商家供应商管理
     *
     * @param mallShopSupplier 商家供应商管理
     * @return 结果
     */
    @Override
    public int updateMallShopSupplier(MallShopSupplier mallShopSupplier)
    {
        String account = mallShopSupplier.getSupplierAccount();// 账号
        String password = mallShopSupplier.getSupplierPassword();// 密码

        // 修改系统用户
        SysUser sysUser = new SysUser();
        sysUser.setUserId(mallShopSupplier.getSysUserId());
        sysUser.setUserName(account);
        sysUser.setNickName(mallShopSupplier.getSupplierName());
        sysUser.setPhonenumber(mallShopSupplier.getSupplierPhone());
        sysUser.setStatus(mallShopSupplier.getSupplierStatus().toString());
        sysUser.setUpdateTime(DateUtils.getNowDate());
        sysUser.setPassword(SecurityUtils.encryptPassword(password));
        sysUserMapper.updateUser(sysUser);
        mallShopSupplier.setUpdateAt(DateUtils.getTime());
        return mallShopSupplierMapper.updateMallShopSupplier(mallShopSupplier);
    }

    /**
     * 批量删除商家供应商管理
     *
     * @param supplierIds 需要删除的商家供应商管理主键
     * @return 结果
     */
    @Override
    public int deleteMallShopSupplierBySupplierIds(Long[] supplierIds)
    {
        return mallShopSupplierMapper.deleteMallShopSupplierBySupplierIds(supplierIds);
    }

    /**
     * 删除商家供应商管理信息
     *
     * @param supplierId 商家供应商管理主键
     * @return 结果
     */
    @Override
    public int deleteMallShopSupplierBySupplierId(Long supplierId)
    {
        return mallShopSupplierMapper.deleteMallShopSupplierBySupplierId(supplierId);
    }

    /**
     * 根据供应商名查询供应商
     *
     * @param supplierName 供应商名
     * @return 商家供应商
     */
    @Override
    public MallShopSupplier selectMallShopSupplierBySupplierName(String supplierName) {
        return mallShopSupplierMapper.selectMallShopSupplierBySupplierName(supplierName);
    }

    @Override
    public MallShopSupplier selectMallShopSupplierBySupplierAccount(String supplierAccount) {
        return mallShopSupplierMapper.selectMallShopSupplierBySupplierAccount(supplierAccount);
    }

    /**
     * 根据系统用户ID查询供应商
     *
     * @param sysUserId 系统用户ID
     * @return 商家供应商
     */
    @Override
    public MallShopSupplier selectMallShopSupplierBySysUserId(Long sysUserId) {
        return mallShopSupplierMapper.selectMallShopSupplierBySysUserId(sysUserId);
    }
}
