package com.ruoyi.mall.service.impl;

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

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.MallAppCompany;
import com.ruoyi.mall.domain.MallSalesman;
import com.ruoyi.mall.mapper.MallAppCompanyMapper;
import com.ruoyi.mall.mapper.MallSalesmanMapper;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallAppCompanyUserMapper;
import com.ruoyi.mall.domain.MallAppCompanyUser;
import com.ruoyi.mall.service.IMallAppCompanyUserService;

import javax.annotation.Resource;

/**
 * app-公司人员Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-27
 */
@Service
public class MallAppCompanyUserServiceImpl implements IMallAppCompanyUserService
{
    @Resource
    private MallAppCompanyUserMapper mallAppCompanyUserMapper;
    @Resource
    private MallAppCompanyMapper mallAppCompanyMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private SysDictDataMapper sysDictDataMapper;
    @Resource
    private MallSalesmanMapper mallSalesmanMapper;

    /**
     * 查询app-公司人员
     *
     * @param companyUserId app-公司人员主键
     * @return app-公司人员
     */
    @Override
    public MallAppCompanyUser selectMallAppCompanyUserByCompanyUserId(Long companyUserId)
    {
        return mallAppCompanyUserMapper.selectMallAppCompanyUserByCompanyUserId(companyUserId);
    }

    /**
     * 查询app-公司人员列表
     *
     * @param mallAppCompanyUser app-公司人员
     * @return app-公司人员
     */
    @Override
    public List<MallAppCompanyUser> selectMallAppCompanyUserList(MallAppCompanyUser mallAppCompanyUser)
    {
        return mallAppCompanyUserMapper.selectMallAppCompanyUserList(mallAppCompanyUser);
    }

    /**
     * 新增app-公司人员
     *
     * @param mallAppCompanyUser app-公司人员
     * @return 结果
     */
    @Override
    public int insertMallAppCompanyUser(MallAppCompanyUser mallAppCompanyUser)
    {
        String account = mallAppCompanyUser.getCompanyUserAccount();// 账号
        String password = mallAppCompanyUser.getCompanyUserPassword();// 密码

        // 新增系统用户
        SysUser sysUser = new SysUser();
        sysUser.setUserName(account);
        sysUser.setNickName(mallAppCompanyUser.getCompanyUserName());
        sysUser.setPhonenumber(mallAppCompanyUser.getCompanyUserPhone());
        sysUser.setStatus(mallAppCompanyUser.getCompanyUserStatus().toString());
        sysUser.setPassword(SecurityUtils.encryptPassword(password));
        sysUserMapper.insertUser(sysUser);

        // 新增用户与角色管理
        List<SysUserRole> userRoleList = new ArrayList<>();
        SysUserRole ur = new SysUserRole();
        ur.setUserId(sysUser.getUserId());
        if (mallAppCompanyUser.getCompanyUserType() == 0) {
            // 账号类型（10-分公司管理员，11-业务员
            ur.setRoleId(10L);
            mallAppCompanyUser.setRoleId(10L);
        } else {
            ur.setRoleId(11L);
            mallAppCompanyUser.setRoleId(11L);
        }
        userRoleList.add(ur);
        sysUserRoleMapper.batchUserRole(userRoleList);

        mallAppCompanyUser.setSysUserId(sysUser.getUserId());
        mallAppCompanyUser.setCreateAt(DateUtils.getTime());
        mallAppCompanyUser.setDelFlag(0);
        return mallAppCompanyUserMapper.insertMallAppCompanyUser(mallAppCompanyUser);
    }

    /**
     * 修改app-公司人员
     *
     * @param mallAppCompanyUser app-公司人员
     * @return 结果
     */
    @Override
    public int updateMallAppCompanyUser(MallAppCompanyUser mallAppCompanyUser)
    {
        String account = mallAppCompanyUser.getCompanyUserAccount();// 账号
        String password = mallAppCompanyUser.getCompanyUserPassword();// 密码

        // 修改系统用户
        SysUser sysUser = new SysUser();
        sysUser.setUserId(mallAppCompanyUser.getSysUserId());
        sysUser.setUserName(account);
        sysUser.setNickName(mallAppCompanyUser.getCompanyUserName());
        sysUser.setPhonenumber(mallAppCompanyUser.getCompanyUserPhone());
        sysUser.setStatus(mallAppCompanyUser.getCompanyUserStatus().toString());
        sysUser.setUpdateTime(DateUtils.getNowDate());
        sysUser.setPassword(SecurityUtils.encryptPassword(password));
        sysUserMapper.updateUser(sysUser);

        mallAppCompanyUser.setUpdateAt(DateUtils.getTime());
        return mallAppCompanyUserMapper.updateMallAppCompanyUser(mallAppCompanyUser);
    }

    /**
     * 批量删除app-公司人员
     *
     * @param companyUserIds 需要删除的app-公司人员主键
     * @return 结果
     */
    @Override
    public int deleteMallAppCompanyUserByCompanyUserIds(Long[] companyUserIds)
    {
        return mallAppCompanyUserMapper.deleteMallAppCompanyUserByCompanyUserIds(companyUserIds);
    }

    /**
     * 删除app-公司人员信息
     *
     * @param companyUserId app-公司人员主键
     * @return 结果
     */
    @Override
    public int deleteMallAppCompanyUserByCompanyUserId(Long companyUserId)
    {
        MallAppCompanyUser mallAppCompanyUser = mallAppCompanyUserMapper.selectMallAppCompanyUserByCompanyUserId(companyUserId);
        if (mallAppCompanyUser != null) {
            sysUserMapper.deleteReallyUserById(mallAppCompanyUser.getCompanyUserId());
        }
        return mallAppCompanyUserMapper.deleteMallAppCompanyUserByCompanyUserId(companyUserId);
    }

    /**
     * 根据公司人员名称查询公司人员
     *
     * @param companyUserName 公司人员名称
     * @return 公司人员
     */
    @Override
    public MallAppCompanyUser selectMallAppCompanyUserByCompanyUserName(String companyUserName) {
        return mallAppCompanyUserMapper.selectMallAppCompanyUserByCompanyUserName(companyUserName);
    }

    /**
     * 根据公司人员账号查询公司人员
     *
     * @param companyUserAccount 公司人员账号
     * @return 公司人员
     */
    @Override
    public MallAppCompanyUser selectMallAppCompanyUserByCompanyUserAccount(String companyUserAccount) {
        return mallAppCompanyUserMapper.selectMallAppCompanyUserByCompanyUserAccount(companyUserAccount);
    }

    /**
     * 根据系统用户ID查询公司人员
     *
     * @param sysUserId 系统用户ID
     * @return 公司人员
     */
    @Override
    public MallAppCompanyUser selectMallAppCompanyUserBySysUserId(Long sysUserId) {
        return mallAppCompanyUserMapper.selectMallAppCompanyUserBySysUserId(sysUserId);
    }

    /**
     * app用户登录
     *
     * @param param 参数
     * @return ApiResult
     */
    @Override
    public ApiResult appUserLogin(Map<String, Object> param) {

        String account = MapUtils.getString(param, "account");
        String password = MapUtils.getString(param, "password");

        MallAppCompanyUser appCompanyUser = new MallAppCompanyUser();

        // 查询用户信息
        SysUser user = sysUserMapper.selectUserByUserName(account);
        if (ObjectUtils.isEmpty(user)) {
            return ApiResult.error("账号不存在");
        } else {
            appCompanyUser.setSysUserId(user.getUserId());
            appCompanyUser.setCompanyUserPortrait(user.getAvatar());
            appCompanyUser.setCompanyUserAccount(account);
            appCompanyUser.setCompanyUserPassword(password);
            appCompanyUser.setCompanyUserPhone(user.getPhonenumber());
            appCompanyUser.setCompanyUserName(user.getNickName());

            String userPassword = user.getPassword();
            // 账号是否设置密码，密码校验
            if (StringUtils.isBlank(userPassword) || !SecurityUtils.matchesPassword(password, userPassword)) {
                return ApiResult.error("账号或密码错误");
            } else {
                if ("1".equals(user.getStatus())) {
                    return ApiResult.error("账号已冻结");
                }
                MallAppCompanyUser mallAppCompanyUser = mallAppCompanyUserMapper.selectMallAppCompanyUserBySysUserId(user.getUserId());
                if (ObjectUtils.isNotEmpty(mallAppCompanyUser)) {
                    MallAppCompany mallAppCompany = mallAppCompanyMapper.selectMallAppCompanyByCompanyId(mallAppCompanyUser.getCompanyId());
                    if (ObjectUtils.isEmpty(mallAppCompanyUser) || mallAppCompany.getCompanyStatus() != 0) {
                        return ApiResult.error("公司已被冻结");
                    }

                    appCompanyUser.setRoleId(mallAppCompanyUser.getRoleId());
                } else {
                    appCompanyUser.setRoleId(sysUserRoleMapper.selectUserRoleByUserId(user.getUserId()));
                }
            }
        }

        if (appCompanyUser.getRoleId() == null || (appCompanyUser.getRoleId() != 1 &&appCompanyUser.getRoleId() != 12 && appCompanyUser.getRoleId() != 13 && appCompanyUser.getRoleId() != 10 && appCompanyUser.getRoleId() != 11)) {
            return ApiResult.error("登录失败");
        }

        if (appCompanyUser.getRoleId() == 12) {
            appCompanyUser.setRoleName("仓库管理员");
        } else if (appCompanyUser.getRoleId() == 13) {
            appCompanyUser.setRoleName("财务");
        } else if (appCompanyUser.getRoleId() == 10) {
            appCompanyUser.setRoleName("分公司管理员");
        } else if (appCompanyUser.getRoleId() == 11) {
            appCompanyUser.setRoleName("业务员");
        } else if (appCompanyUser.getRoleId() == 1) {
            appCompanyUser.setRoleName("总管理员");
        }

        return ApiResult.ok().put("data", appCompanyUser);
    }

    /**
     * 业务员列表
     *
     * @param param 参数
     * @return List
     */
    @Override
    public List<MallAppCompanyUser> getSalesmanList(Map<String, Object> param) {
        long sysUserId = Long.parseLong(param.get("sysUserId").toString());
        Long roleByUserId = sysUserRoleMapper.selectUserRoleByUserId(sysUserId);

        MallAppCompanyUser mallAppCompanyUser = new MallAppCompanyUser();
        mallAppCompanyUser.setCompanyUserType(1);
        if (roleByUserId == 10) {
            // MallAppCompanyUser companyUser = mallAppCompanyUserMapper.selectMallAppCompanyUserBySysUserId(sysUserId);
            MallSalesman mallSalesman = mallSalesmanMapper.selectMallSalesmanBySysUserId(sysUserId);
            mallAppCompanyUser.setCompanyId(mallSalesman.getCompanyId());
        } else if (roleByUserId == 11) {
            mallAppCompanyUser.setSysUserId(sysUserId);
        }
        return mallAppCompanyUserMapper.selectMallSalesmanList(mallAppCompanyUser);
    }

    /**
     * 订单状态列表
     *
     * @param param 参数
     * @return List
     */
    @Override
    public List<Map<String, Object>> getOrderStatusList(Map<String, Object> param) {
        long sysUserId = Long.parseLong(param.get("sysUserId").toString());
        int orderType = Integer.parseInt(param.get("orderType").toString());
        Long roleId = sysUserRoleMapper.selectUserRoleByUserId(sysUserId);
        param.put("roleId", roleId);
        param.put("orderType", orderType);
        return sysDictDataMapper.selectOrderStatusDictDataByRoleId(param);
    }

    /**
     * 分公司业务员列表
     *
     * @param mallAppCompanyUser app-公司人员
     * @return app-公司人员集合
     */
    @Override
    public List<MallAppCompanyUser> selectMallSalesmanList(MallAppCompanyUser mallAppCompanyUser) {
        return mallAppCompanyUserMapper.selectMallSalesmanList(mallAppCompanyUser);
    }
}
