package com.microservices.pms.project.controller;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.web.controller.BaseController;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.log.annotation.Log;
import com.microservices.common.log.enums.BusinessType;
import com.microservices.common.security.annotation.Logical;
import com.microservices.common.security.annotation.RequiresPermissions;
import com.microservices.pms.project.domain.vo.*;
import com.microservices.pms.project.service.IPmsProjectService;
import com.microservices.system.api.domain.SimpleSysUser;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 项目Controller
 * 
 * @author wanjia
 */
@RestController
@RequestMapping("/{enterpriseIdentifier}/pmsProject")
@Api(tags = "项目管理-项目相关接口")
@ApiImplicitParams({
        @ApiImplicitParam(name = "enterpriseIdentifier", value = "企业标识", paramType = "path", dataType = "String")
})
public class PmsProjectController extends BaseController
{
    @Autowired
    private IPmsProjectService pmsProjectService;

    /**
     * 查询项目列表
     */
    @RequiresPermissions(value = {"pms:pmsProductRequirement:add", "pms:pmsProductRequirement:edit", "pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列（更新时间：updateTime，创建时间：createTime）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc,默认asc", paramType = "query", dataType = "String")
    })
    public GenericsTableDataInfo<PmsProjectDetailVo> list(PmsProjectSearchVo pmsProjectSearchVo)
    {
        return pmsProjectService.selectPmsProjectList(pmsProjectSearchVo);
    }

    /**
     * 获取项目负责人可选用户列表
     */
    @GetMapping("/userList")
    @ApiOperation("获取项目负责人可选用户列表")
    public GenericsTableDataInfo<SimpleSysUser> getUserList(
            @ApiParam(name = "enterpriseIdentifier", value = "企业标识") @PathVariable String enterpriseIdentifier,
            @ApiParam(name = "search", value = "模糊查询") String search) {
        return pmsProjectService.getUserList(enterpriseIdentifier, search);
    }

    /**
     * 获取项目详细信息
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pmsProjectService.selectPmsProjectById(id));
    }

    /**
     * 新增项目
     */
    @RequiresPermissions("pms:pmsProject:add")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增项目")
    public AjaxResult add(@RequestBody @Validated PmsProjectInputVo pmsProjectInputVo)
    {
        return toAjax(pmsProjectService.insertPmsProject(pmsProjectInputVo));
    }

    /**
     * 修改项目
     */
    @RequiresPermissions("pms:pmsProject:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(
            @ApiParam(name = "pmsProjectUpdateVo",
            value = "【字段说明】(非必填字段不传递则代表不更新，传null则代表置空):\n" +
                    "Id:项目Id（必填）;\n" +
                    "projectRepositoryIds:关联仓库ids;\n" +
                    "projectAssigneeId:负责人id;\n" +
                    "pmsProjectMemberIds:成员ids;\n" +
                    "projectDescription:项目简介;\n" +
                    "status:状态 0关闭 1开启 2暂停;\n" +
                    "pmsProductId:所属产品id;\n" +
                    "startTime:开始时间;\n" +
                    "endTime:结束时间;\n" +
                    "isTop:是否置顶 0否 1是;\n")
            @RequestBody JSONObject pmsProjectUpdateVo)
    {
        return toAjax(pmsProjectService.updatePmsProject(pmsProjectUpdateVo));
    }

    /**
     * 删除项目
     */
    @RequiresPermissions("pms:pmsProject:remove")
    @Log(title = "项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation(value = "删除项目（附带删除项目下的所有数据）")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pmsProjectService.deletePmsProjectByIds(ids));
    }

    @RequiresPermissions(value = {"system:notice:add", "pms:pmsProject:noticeManage"}, logical = Logical.OR)
    @PostMapping("/addNotice")
    @ApiOperation(value = "添加项目公告")
    public AjaxResult addProjectNotice( @RequestBody @Validated PmsProjectNoticeInputVo pmsProjectNoticeInputVo) {
        return toAjax(pmsProjectService.addProjectNotice(pmsProjectNoticeInputVo));
    }

    @RequiresPermissions(value = {"system:notice:remove", "pms:pmsProject:noticeManage"}, logical = Logical.OR)
    @DeleteMapping("/deleteNotice/{noticeIds}")
    @ApiOperation(value = "删除项目公告")
    public AjaxResult removeProjectNotice( @PathVariable Long[] noticeIds) {
        return toAjax(pmsProjectService.removeProjectNotice(noticeIds));
    }

    @RequiresPermissions(value = {"system:notice:edit", "pms:pmsProject:noticeManage"}, logical = Logical.OR)
    @PutMapping("/editNotice")
    @ApiOperation(value = "编辑项目公告")
    public AjaxResult editProjectNotice( @RequestBody @Validated PmsProjectNoticeUpdateVo pmsProjectNoticeUpdateVo) {
        return toAjax(pmsProjectService.editProjectNotice(pmsProjectNoticeUpdateVo));
    }

    @RequiresPermissions(value = {"system:notice:query", "pms:pmsProject:noticeManage", "pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/getNotice/{noticeId}")
    @ApiOperation(value = "获取项目公告详情")
    public AjaxResult getProjectNotice( @PathVariable Long noticeId) {
        return success(pmsProjectService.getProjectNotice(noticeId));
    }

    /**
     * 获取项目统计数据（饼图，柱状图）
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping(value = "/statistics/pie-barChart")
    @ApiOperation(value = "项目概览-获取项目统计数据（饼图，柱状图）")
    public AjaxResult getStatistics(@ApiParam("项目id") @RequestParam Long pmProjectId) {
        return success(pmsProjectService.getPmsProjectStatistics(pmProjectId));
    }

    /**
     * 获取项目统计数据（折线图）
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping(value = "/statistics/lineChart")
    @ApiOperation(value = "项目概览-获取项目统计数据（折线图）")
    public AjaxResult getPolyline(@ApiParam("项目id") @RequestParam Long pmProjectId) {
        return success(pmsProjectService.getPmsProjectPolyline(pmProjectId));
    }

}
