package com.ruoyi.project.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.UserRolesType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.bo.ProjectListInput;
import com.ruoyi.project.bo.ProjectTaskListOut;
import com.ruoyi.project.bo.ProjectTaskOut;
import com.ruoyi.project.domain.ProjectStaff;
import com.ruoyi.project.domain.ProjectTask;
import com.ruoyi.project.mapper.ProjectStaffMapper;
import com.ruoyi.project.mapper.ProjectTaskMapper;
import com.ruoyi.project.service.ProjectTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class ProjectTaskServiceImpl   implements ProjectTaskService {
    @Autowired
    private ProjectTaskMapper projectTaskMapper;
    @Autowired
    private ProjectStaffMapper projectStaffMapper;

    @Override
    public AjaxResult addProjectTask(ProjectTask projectTask) {
        return AjaxResult.success(projectTaskMapper.insertSelective(projectTask));
    }

    @Override
    public AjaxResult deleteProject(ProjectTask in) {
        int i = projectTaskMapper.deleteByPrimaryKey(in.getId());
        if (i>0){
            projectStaffMapper.deleteBypid(in.getId());
            return AjaxResult.success();
        }
        return AjaxResult.error("项目编号不存在");
    }

    @Override
    public AjaxResult details(Long id) {
        ProjectTask projectTask = projectTaskMapper.selectByPrimaryKey(id);
        ProjectTaskOut out=  new ProjectTaskOut();

        if (projectTask!=null){
            BeanUtils.copyBeanProp(out, projectTask);
            List<ProjectStaff> projectStaffs = projectStaffMapper.selectBypid(id);
            if (!projectStaffs.isEmpty())out.setList(projectStaffs);
            return AjaxResult.success(out);
        }
        return AjaxResult.error("项目不存在");
    }

    @Override
    public List<ProjectTaskListOut> list(ProjectListInput in) {
        in.setUid(SecurityUtils.getUserId());
        if (SecurityUtils.hashRoles(UserRolesType.MANAGE.getCode())){
            in.setRolesId(UserRolesType.MANAGE.getCode());
        }else if(SecurityUtils.hashRoles(UserRolesType.COMMON.getCode())){
            in.setRolesId(UserRolesType.COMMON.getCode());
        }
        if (in.getEndTime()!=null)in.setEndTime(in.getEndTime()+" 23:59:59");
        return projectTaskMapper.selectList(in);
    }
}
