package org.jeecg.modules.ctop.service.impl;

import cn.com.ctop.common.module.entity.Project;
import cn.com.ctop.common.module.entity.SysUser;
import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.IProjectService;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.ctop.entity.ProjectMember;
import org.jeecg.modules.ctop.entity.ProjectTransferRecord;
import org.jeecg.modules.ctop.mapper.ProjectTransferRecordMapper;
import org.jeecg.modules.ctop.service.IProjectMemberService;
import org.jeecg.modules.ctop.service.IProjectTransferRecordService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 账户转项目记录
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-07-22
 */
@Service
public class ProjectTransferRecordServiceImpl extends ServiceImpl<ProjectTransferRecordMapper, ProjectTransferRecord> implements IProjectTransferRecordService {
    @Autowired
    private IProjectMemberService projectMemberService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private IUserAllocationService userAllocationService;

    @Override
    public Map<String, Object> updateStatus(Long id, Integer status) {
        Map<String, Object> result = new HashMap<>();
        ProjectTransferRecord record = this.getById(id);
        if (null == record || record.getStatus() == 0) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        record.setStatus(status);
        record.setUpdateTime(new Date());
        this.updateById(record);
        if (status == 2) {
            //需要转转项目
            UserAllocation userAllocation = userAllocationService.getByAccountId(record.getAccountId());
            SysUser user = sysUserService.getById(record.getUserId());
            Project project = projectService.getById(record.getTargetProject());
            userAllocation.setProjectId(project.getId());
            userAllocation.setAdvertiserId(project.getAdvertiserId());
            //     userAllocation.setProjectName(project.getProjectName());
            userAllocationService.updateById(userAllocation);
            ProjectMember member = projectMemberService.getProjectByParams(record.getTargetProject(), record.getUserId());
            if (null == member) {
                projectMemberService.addMember(user, project);
            }
        }
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }

    @Override
    public Map<String, Object> addRecord(Long accountId, Long projectId) {
        Map<String, Object> result = new HashMap<>();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (accountId == null || projectId == null) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        UserAllocation allocation = userAllocationService.getByAccountId(accountId);
        Project project = projectService.getById(projectId);
        if (null == allocation || null == project) {
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        ProjectTransferRecord record = new ProjectTransferRecord();
        record.setStatus(1);
        record.setAccountId(accountId);
        record.setSoucreProject(allocation.getProjectId());
        record.setTargetProject(projectId);
        record.setUserId(user.getId());
        record.setReviewerId(project.getResponsibleId());
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
        this.save(record);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }
}
