package com.ruoyi.project.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.UserRolesType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.bo.ProjectStaffRecordInput;
import com.ruoyi.project.bo.ProjectStaffRecordOut;
import com.ruoyi.project.domain.ProjectStaffRecord;
import com.ruoyi.project.domain.ProjectStaffRecordLog;
import com.ruoyi.project.mapper.ProjectStaffRecordLogMapper;
import com.ruoyi.project.mapper.ProjectStaffRecordMapper;
import com.ruoyi.project.service.ProjectStaffRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectStaffRecordServiceImpl implements ProjectStaffRecordService {
    @Autowired
    private ProjectStaffRecordMapper projectStaffRecordMapper;
    @Autowired
    private ProjectStaffRecordLogMapper projectStaffRecordLogMapper;

    @Override
    public AjaxResult add(ProjectStaffRecord in) {
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        in.setAid(userId);
        in.setName(username);
        if(in.getProcessStatus()>1)in.setProcessStatus(1);
        projectStaffRecordMapper.insertSelective(in);
        addRecordLog(in);
        return AjaxResult.success();
    }

    private void addRecordLog(ProjectStaffRecord in) {
        ProjectStaffRecordLog log = new ProjectStaffRecordLog();
        log.setPsrId(in.getId());
        log.setProcessStatus(in.getProcessStatus());
        if (in.getProcessStatus()<2){
            log.setAid(in.getAid());
            log.setName(in.getName());
            log.setContent(getContent(in.getProcessStatus()));
        }else {
            log.setAid(SecurityUtils.getUserId());
            log.setName(SecurityUtils.getLoginUser().getUser().getNickName());
            log.setContent(getContent(in.getProcessStatus())+in.getContent());
        }


        projectStaffRecordLogMapper.insertSelective(log);
    }

    private String getContent(Integer processStatus) {
        switch (processStatus) {
            case 0: return "修改了内容！";
            case 1: return "发起了审核！";
            case 2: return "完成了审核！";
            case 3: return "驳回了审核！";
        }
        return "";
    }

    @Override
    public AjaxResult update(ProjectStaffRecord in) {
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getLoginUser().getUser().getNickName();
        in.setAid(userId);
        in.setName(username);
        if(in.getProcessStatus()>1)in.setProcessStatus(1);
        projectStaffRecordMapper.updateByPrimaryKeySelective(in);
        addRecordLog(in);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult examineRecord(ProjectStaffRecord in) {
        ProjectStaffRecord newP = new ProjectStaffRecord();
        newP.setId(in.getId());
        newP.setProcessStatus(in.getProcessStatus());
        projectStaffRecordMapper.updateByPrimaryKeySelective(newP);
        addRecordLog(in);
        return AjaxResult.success();
    }

    @Override
    public List<ProjectStaffRecordOut> listRecord(ProjectStaffRecordInput in) {
        if (SecurityUtils.hashRoles(UserRolesType.COMMON.getCode())){
            in.setRoleId(UserRolesType.COMMON.getCode());
            in.setAid(SecurityUtils.getUserId());
        }else if (SecurityUtils.hashRoles(UserRolesType.MANAGE.getCode())){
            in.setRoleId(UserRolesType.MANAGE.getCode());
            in.setAid(SecurityUtils.getUserId());
        }else {
            in.setRoleId(UserRolesType.ADMIN.getCode());
        }
        return projectStaffRecordMapper.selectList(in);
    }
}
