package com.ruoyi.web.controller.project;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.bo.ProjectListInput;
import com.ruoyi.project.bo.ProjectStaffRecordInput;
import com.ruoyi.project.bo.ProjectStaffRecordOut;
import com.ruoyi.project.bo.ProjectTaskListOut;
import com.ruoyi.project.domain.ProjectStaff;
import com.ruoyi.project.domain.ProjectStaffRecord;
import com.ruoyi.project.domain.ProjectTask;
import com.ruoyi.project.service.ProjectStaffRecordService;
import com.ruoyi.project.service.ProjectStaffService;
import com.ruoyi.project.service.ProjectTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.awt.print.Pageable;
import java.util.List;

/**
 *  项目管理
 */
@RestController
@RequestMapping("/api/project")
public class ProjectTaskController extends BaseController {

    @Autowired
    private ProjectTaskService projectTaskService;
    @Autowired
    private ProjectStaffService projectStaffService;
    @Autowired
    private ProjectStaffRecordService projectStaffRecordService;

    /**
     * 新增项目任务
     * @param projectTask
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add( @RequestBody ProjectTask projectTask){
        if (projectTask.getAid()== null){
            return error("负责人不能为空");
        }
        return projectTaskService.addProjectTask(projectTask);
    }

    /**
     * 新增项目成员
     * @param in
     * @return
     */
    @PostMapping("/addStaff")
    public AjaxResult addStaff( @RequestBody ProjectStaff in){
        if (in.getAid()== null){
            return error("项目成员不能为空");
        }
        return projectStaffService.addStaff(in);
    }

    /**
     * 新增成员打卡
     * @param in
     * @return
     */
    @PostMapping("/addRecord")
    public AjaxResult addRecord( @RequestBody ProjectStaffRecord in){
        return projectStaffRecordService.add(in);
    }


    /**
     * 修改成员打卡
     * @param in
     * @return
     */
    @PostMapping("/updateRecord")
    public AjaxResult updateRecord( @RequestBody ProjectStaffRecord in){
        return projectStaffRecordService.update(in);
    }


    /**
     * 审核成员打卡
     * @param in
     * @return
     */
    @PostMapping("/examineRecord")
    public AjaxResult examineRecord( @RequestBody ProjectStaffRecord in){
        if (in.getProcessStatus()!=2 && in.getProcessStatus()!=3){
            return AjaxResult.error("审核状态错误");
        }
        return projectStaffRecordService.examineRecord(in);
    }

    /**
     * 成员打卡列表
     * @param in
     * @return
     */
    @GetMapping("/listRecord")
    public TableDataInfo listRecord( @RequestBody ProjectStaffRecordInput in){
        startPage();
        List<ProjectStaffRecordOut> list = projectStaffRecordService.listRecord(in);
        return getDataTable(list);
    }

    /**
     * 删除项目成员
     * @param in
     * @return
     */
    @PostMapping("/deleteStaff")
    public AjaxResult deleteStaff( @RequestBody ProjectStaff in){
        if (in.getId()== null){
            return error("项目成员编号不能为空");
        }
        return projectStaffService.deleteStaff(in);
    }


    /**
     * 删除项目
     * @param in
     * @return
     */
    @PostMapping("/deleteProject")
    public AjaxResult deleteProject( @RequestBody ProjectTask in){
        if (in.getId()== null){
            return error("项目编号不能为空");
        }
        return projectTaskService.deleteProject(in);
    }

    /**
     * 项目详情
     * @param id
     * @return
     */
    @GetMapping ("/details")
    public AjaxResult details( Long id){
        if (id== null){
            return error("项目编号不能为空");
        }
        return projectTaskService.details(id);
    }


    /**
     * 项目列表
     * @param
     * @return
     */
    @GetMapping ("/list")
    public TableDataInfo list(ProjectListInput in){
        startPage();
        List<ProjectTaskListOut> list=projectTaskService.list(in);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     * @param response
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response,ProjectListInput in)
    {
        List<ProjectTaskListOut> list=projectTaskService.list(in);
        ExcelUtil<ProjectTaskListOut> util = new ExcelUtil<>(ProjectTaskListOut.class);
        util.exportExcel(response, list, "项目列表");
    }

    /**
     * 导入数据
     * @param file
     * @param isUpdateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean isUpdateSupport) throws Exception
    {
        ExcelUtil<ProjectTaskListOut> util = new ExcelUtil<>(ProjectTaskListOut.class);
        List<ProjectTaskListOut> list = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = projectTaskService.importProject(list, isUpdateSupport, operName);
        return success(message);
    }


    /**
     * 上传项目文件
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) throws Exception{
        if (!file.isEmpty()){
            String avatar = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", avatar);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }

}
