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.common.utils.Result;
import com.ruixuan.jinniu.entity.KuaishouAccount;
import com.ruixuan.jinniu.entity.KuaishouAgentAccountList;
import com.ruixuan.jinniu.entity.KuaishouProject;
import com.ruixuan.jinniu.mapper.KuaishouAccountMapper;
import com.ruixuan.jinniu.mapper.KuaishouAgentAccountListMapper;
import com.ruixuan.jinniu.mapper.KuaishouProjectMapper;
import com.ruixuan.jinniu.service.IKuaishouAccountService;
import com.ruixuan.jinniu.service.IKuaishouProjectMemberService;
import com.ruixuan.system.service.ISysDeptService;
import com.ruixuan.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 账户Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-15
 */
@Service
public class KuaishouAccountServiceImpl implements IKuaishouAccountService {
    @Autowired
    private KuaishouAccountMapper kuaishouAccountMapper;

    @Autowired
    private KuaishouAgentAccountListMapper agentAccountListMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private KuaishouProjectMapper projectMapper;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private IKuaishouProjectMemberService kuaishouProjectMemberService;

    @Override
    public List<JSONObject> selectUnboundAccountList(Long accountId, Long ksId, String accountName, Long status) {
        return kuaishouAccountMapper.selectUnboundAccountList(accountId, ksId, accountName, status);
    }

    /**
     * 查询账户
     *
     * @param id 账户主键
     * @return 账户
     */
    @Override
    public KuaishouAccount selectKuaishouAccountById(Long id) {
        return kuaishouAccountMapper.selectKuaishouAccountById(id);
    }

    /**
     * 查询账户列表
     *
     * @param kuaishouAccount 账户
     * @return 账户
     */
    @Override
    public List<KuaishouAccount> selectKuaishouAccountList(KuaishouAccount kuaishouAccount) {
        String roleKey = sysUserService.getRoleKeyByUserId(kuaishouAccount.getUserId());
        List<Long> userList;
        if ("admin".equals(roleKey)) {
            //查看所有
            userList = null;
        } else if ("operationsManager".equals(roleKey) || "directorOperations".equals(roleKey)) {
            //运营总监 和 运营经理 可以看自己部门所有人的（自己和下属的）
            Long deptId = deptService.getDeptIdByUserId(kuaishouAccount.getUserId());
            userList = deptService.getDeptUserListByDeptId(deptId);
            kuaishouAccount.setUserList(userList);
        } else {
            userList = Arrays.asList(kuaishouAccount.getUserId());
            kuaishouAccount.setUserList(userList);
        }
        PageUtils.startPage();
        return kuaishouAccountMapper.selectKuaishouAccountList(kuaishouAccount);
    }

    @Override
    public List<KuaishouAccount> getAccountByProjectId(Long projectId) {
        return kuaishouAccountMapper.getAccountByProjectId(projectId);
    }

    /**
     * 新增账户
     *
     * @param kuaishouAccount 账户
     * @return 结果
     */
    @Override
    public int insertKuaishouAccount(JSONObject o) {
        Long accountId = o.getLong("accountId");
        Long userId = o.getLong("userId");
        Long projectId = o.getLong("projectId");
        KuaishouAgentAccountList agentAccount = agentAccountListMapper.selectKuaishouAgentAccountListByAccountId(accountId);
        KuaishouAccount kuaishouAccount = new KuaishouAccount();
        BeanUtils.copyProperties(agentAccount, kuaishouAccount);
        SysUser sysUser = sysUserService.selectUserById(userId);
        KuaishouProject project = projectMapper.selectKuaishouProjectById(projectId);
        kuaishouAccount.setOperatorId(userId);
        kuaishouAccount.setOperatorName(sysUser.getNickName());
        kuaishouAccount.setKsId(agentAccount.getUserId());
        kuaishouAccount.setKsName(agentAccount.getUserName());
        kuaishouAccount.setAccountType(o.getString("accountType"));
        kuaishouAccount.setMediaCode(o.getString("mediaCode"));
        kuaishouAccount.setProjectId(projectId);
        kuaishouAccount.setProjectName(project.getProjectName());
        kuaishouAccount.setAccountCreateTime(DateUtils.formatDate(agentAccount.getAccountCreateTime(), DateUtils.YYYY_MM_DD_HH_MM_SS));
        kuaishouAccount.setBindTime(DateUtils.getDate());
        kuaishouAccount.setAdvertiserId(project.getAdvertiserId());
        int i = kuaishouAccountMapper.insertKuaishouAccount(kuaishouAccount);
        if (i > 0) {
            //添加 参与项目人员
            JSONObject mem = new JSONObject();
            mem.put("projectId", projectId);
            mem.put("projectName", project.getProjectName());
            mem.put("userIds", Arrays.asList(userId));
            kuaishouProjectMemberService.addProjectUsers(mem);
        }
        insertAccountRecord(kuaishouAccount, userId, 1);//操作类型 1 绑定，2解绑
        return i;
    }

    /**
     * 修改账户
     *
     * @param kuaishouAccount 账户
     * @return 结果
     */
    @Override
    public int updateKuaishouAccount(KuaishouAccount kuaishouAccount) {
        return kuaishouAccountMapper.updateKuaishouAccount(kuaishouAccount);
    }

    /**
     * 删除账户信息
     *
     * @param id 账户主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouAccountById(Long id) {
        return kuaishouAccountMapper.deleteKuaishouAccountById(id);
    }

    @Override
    public Result<Object> deleteClaimAccount(Long accountId, Long userId) {
        KuaishouAccount account = kuaishouAccountMapper.selectKuaishouAccountByAccountId(accountId);
        int i = kuaishouAccountMapper.deleteKuaishouAccountById(account.getId());
        insertAccountRecord(account, userId, 2);//操作类型 1 绑定，2解绑
        return Result.success();
    }

    @Override
    public Result<Object> edit(KuaishouAccount kuaishouAccount) {
        int i = kuaishouAccountMapper.updateKuaishouAccountByAccountId(kuaishouAccount);
        return Result.success();
    }

    @Override
    public List<JSONObject> getAccountNameByProjectId(Long projectId) {
        return kuaishouAccountMapper.getAccountNameByProjectId(projectId);
    }

    @Override
    public int deleteKuaishouAccountByProjectId(Long id) {
        return kuaishouAccountMapper.deleteKuaishouAccountByProjectId(id);
    }

    private void insertAccountRecord(KuaishouAccount account, Long userId, int type) {
        JSONObject o = new JSONObject();
        o.put("accountId", account.getAccountId());
        o.put("userId", userId);
        o.put("bindUserId", account.getOperatorId());
        o.put("bindType", type);//操作类型 1 绑定，2解绑
        kuaishouAccountMapper.insertRecord(o);
    }
}