package com.usky.pm.controller.web;


import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.pm.domain.PmProject;
import com.usky.pm.service.PmProjectService;
import com.usky.pm.service.vo.PmProjectWorkTimeTwoVo;
import com.usky.pm.service.vo.PmUsersProjectWorkTimeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 项目表 前端控制器
 * </p>
 *
 * @author fu
 * @since 2024-05-20
 */
@RestController
@RequestMapping("/pmProject")
public class PmProjectController {
    @Autowired
    private PmProjectService pmProjectService;

    /**
     * 查询项目名（下拉框）
     *
     * @return
     */
    @GetMapping("/projects")
    public ApiResult<List<PmProject>> queryProjectName() {
        return ApiResult.success(pmProjectService.queryProject());
    }

    /**
     * 项目列表分页
     *
     * @param projectName
     * @param projectType
     * @param projectStatus
     * @param projectAscription
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public ApiResult<CommonPage<PmProject>> pageList(@RequestParam(value = "projectName", required = false) String projectName,
                                                     @RequestParam(value = "projectType", required = false) Integer projectType,
                                                     @RequestParam(value = "projectStatus", required = false) Integer projectStatus,
                                                     @RequestParam(value = "projectAscription", required = false, defaultValue = "0") Integer projectAscription,
                                                     @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "projectId", required = false) Integer projectId,
                                                     @RequestParam(value = "visibleRange", required = false) Byte visibleRange) {
        return ApiResult.success(pmProjectService.projectList(projectName, projectType, projectStatus, projectAscription, pageNum, pageSize, projectId, visibleRange));
    }

    /**
     * 统计
     *
     * @return
     */
    @GetMapping("/counting")
    public ApiResult<Map<String, Integer>> count() {
        return ApiResult.success(pmProjectService.sum());
    }

    /**
     * 新增、编辑项目
     *
     * @param pmProject
     */
    @Log(title = "新增/编辑项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public void add(@RequestBody PmProject pmProject) {
        pmProjectService.addProject(pmProject);
    }

    /**
     * 删除
     *
     * @param projectId 项目id
     */
    @Log(title = "删除项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectId}")
    public void del(@RequestParam Integer projectId) {
        pmProjectService.delProject(projectId);
    }

    //回退代码
    @Log(title = "删除项目", businessType = BusinessType.DELETE)
    @GetMapping("/del")
    public void del2(@RequestParam Integer projectId) {
        pmProjectService.delProject(projectId);
    }

    /**
     * 人员、项目下拉
     *
     * @param identifying
     * @return
     */
    @GetMapping("/userOrProject")
    public ApiResult<List<Map<String, Object>>> userOrProject(@RequestParam(value = "identifying", required = false) Integer identifying) {
        return ApiResult.success(pmProjectService.userOrProject(identifying));
    }

    /**
     * 退出项目
     *
     * @param projectId 项目id
     */
    @Log(title = "退出项目", businessType = BusinessType.UPDATE)
    @GetMapping("/exitProject")
    public void exitProject(@RequestParam Integer projectId) {
        pmProjectService.exitProject(projectId);
    }

    /**
     * 项目工时统计
     *
     * @param projectId
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/projectWorkTime")
    public ApiResult<PmProjectWorkTimeTwoVo> projectIdName(@RequestParam(value = "projectId") Integer projectId,
                                                               @RequestParam(value = "startDate", required = false) String startDate,
                                                               @RequestParam(value = "endDate", required = false) String endDate) {
        return ApiResult.success(pmProjectService.projectWorkTime(projectId, startDate, endDate));
    }

    /**
     * 项目工时统计(人员)
     *
     * @param projectId
     * @return
     */
    @GetMapping("/usersProjectWorkTime")
    public ApiResult<PmUsersProjectWorkTimeVO> usersProjectWorkTime(@RequestParam(value = "projectId") Integer projectId,
                                                                    @RequestParam(value = "startDate", required = false) String startDate,
                                                                    @RequestParam(value = "endDate", required = false) String endDate) {
        return ApiResult.success(pmProjectService.usersProjectWorkTime(projectId, startDate, endDate));
    }

    // 加入项目
    @Log(title = "加入项目", businessType = BusinessType.UPDATE)
    @GetMapping("/joinProject")
    public void projectIdName(@RequestParam(value = "projectId") Integer projectId) {
        pmProjectService.joinProject(projectId);
    }

}

