package com.ruixuan.jinniu.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.core.domain.entity.SysUser;
import com.ruixuan.common.utils.DateUtils;
import com.ruixuan.common.utils.PageUtils;
import com.ruixuan.jinniu.entity.KuaishouAccount;
import com.ruixuan.jinniu.entity.KuaishouTransferAccount;
import com.ruixuan.jinniu.mapper.KuaishouAccountMapper;
import com.ruixuan.jinniu.mapper.KuaishouTransferAccountMapper;
import com.ruixuan.jinniu.service.IKuaishouProjectMemberService;
import com.ruixuan.jinniu.service.IKuaishouTransferAccountService;
import com.ruixuan.system.service.ISysDeptService;
import com.ruixuan.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 账户转让记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-16
 */
@Service
public class KuaishouTransferAccountServiceImpl implements IKuaishouTransferAccountService {
    @Autowired
    private KuaishouTransferAccountMapper kuaishouTransferAccountMapper;
    @Autowired
    private KuaishouAccountMapper kuaishouAccountMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private IKuaishouProjectMemberService kuaishouProjectMemberService;

    /**
     * 查询账户转让记录
     *
     * @param id 账户转让记录主键
     * @return 账户转让记录
     */
    @Override
    public KuaishouTransferAccount selectKuaishouTransferAccountById(Long id) {
        return kuaishouTransferAccountMapper.selectKuaishouTransferAccountById(id);
    }

    /**
     * 查询账户转让记录列表
     *
     * @param kuaishouTransferAccount 账户转让记录
     * @return 账户转让记录
     */
    @Override
    public List<JSONObject> selectKuaishouTransferAccountList(KuaishouTransferAccount account) {
        String roleKey = sysUserService.getRoleKeyByUserId(account.getUserId());
        List<Long> userList;
        if ("operationsManager".equals(roleKey)) {
            //运营经理 可以看自己部门所有人
            Long deptId = deptService.getDeptIdByUserId(account.getUserId());
            userList = deptService.getDeptUserListByDeptId(deptId);
            account.setUserList(userList);
        } else if ("admin".equals(roleKey) || "directorOperations".equals(roleKey)) {
            userList = null;
        } else {
            return Collections.emptyList();
        }
        PageUtils.startPage();
        return kuaishouTransferAccountMapper.selectKuaishouTransferAccountList(account);
    }

    /**
     * 新增账户转让记录
     *
     * @param kuaishouTransferAccount 账户转让记录
     * @return 结果
     */
    @Override
    public int insertKuaishouTransferAccount(KuaishouTransferAccount kuaishouTransferAccount) {
        return kuaishouTransferAccountMapper.insertKuaishouTransferAccount(kuaishouTransferAccount);
    }

    /**
     * 修改账户转让记录
     */
    @Override
    public int updateKuaishouTransferAccount(KuaishouTransferAccount kuaishouTransferAccount) {
        KuaishouTransferAccount transferAccount = kuaishouTransferAccountMapper.selectKuaishouTransferAccountById(kuaishouTransferAccount.getId());
        transferAccount.setStatus(kuaishouTransferAccount.getStatus());
        transferAccount.setAuditor(kuaishouTransferAccount.getAuditor());
        transferAccount.setRemarks(DateUtils.getDate());
        int i = kuaishouTransferAccountMapper.updateKuaishouTransferAccount(transferAccount);
        if (i > 0 && kuaishouTransferAccount.getStatus() == 1) {
            // 通过转让审核，账户运营人变为受让人
            SysUser sysUser = sysUserService.selectUserById(transferAccount.getAssignee());
            KuaishouAccount kuaishouAccount = new KuaishouAccount();
            kuaishouAccount.setAccountId(transferAccount.getAccountId());
            kuaishouAccount.setOperatorId(sysUser.getUserId());
            kuaishouAccount.setOperatorName(sysUser.getNickName());
            kuaishouAccountMapper.updateKuaishouAccountByAccountId(kuaishouAccount);
            //添加 参与项目人员
            JSONObject mem = new JSONObject();
            mem.put("projectId", transferAccount.getProjectId());
            mem.put("userIds", Arrays.asList(sysUser.getUserId()));
            kuaishouProjectMemberService.addProjectUsers(mem);
        }
        return i;
    }

    /**
     * 删除账户转让记录信息
     *
     * @param id 账户转让记录主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouTransferAccountById(Long id) {
        return kuaishouTransferAccountMapper.deleteKuaishouTransferAccountById(id);
    }
}