package com.ruoyi.project.business.controller;

import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.annotation.DataUpdateCheck;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.TcProject;
import com.ruoyi.project.business.domain.TcProjectForRelated;
import com.ruoyi.project.business.domain.vo.project.ProjectExportVO;
import com.ruoyi.project.business.domain.vo.selectParam.ProjectParam;
import com.ruoyi.project.business.service.ITcProjectService;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 项目管理Controller
 *
 * @author ruoyi
 * @date 2024-04-10
 */
@RestController
@RequestMapping("/business/project")
public class TcProjectController extends BaseController
{
    @Autowired
    private ITcProjectService tcProjectService;

    /**
     * 查询项目管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProjectParam tcProject)
    {
        startPage();
        return getDataTable(tcProjectService.selectTcProjectList(tcProject));
    }

    /**
     * 查询项目管理列表
     */
    @GetMapping("/list-board")
    public TableDataInfo listBoard(ProjectParam tcProject)
    {
        startPage();
        return getDataTable(tcProjectService.selectTcProjectList(tcProject));
    }

    /**
     * 查询项目管理列表
     */
    @GetMapping("/list-new")
    public TableDataInfo listNew(ProjectParam tcProject)
    {
        startPage();
        return getDataTable(tcProjectService.selectTcProjectListNew(tcProject));
    }

    /**
     * 查询项目列表 app用
     */
    @GetMapping("/listApp")
    public TableDataInfo listApp()
    {
        startPage();
        List<TcProject> list = tcProjectService.selectTcProjectListApp();
        return getDataTable(list);
    }

    /**
     * 查询项目管理列表 不分页
     */
    @GetMapping("/listAll")
    public TableDataInfo listAll(ProjectParam tcProject)
    {
        return getDataTable(tcProjectService.selectTcProjectList(tcProject));
    }

    /**
     * 查询关联项目
     */
    @GetMapping("/projectlist")
    public TableDataInfo selectProjectListByLoginId()
    {
        List<TcProjectForRelated> list = tcProjectService.selectProjectListByLoginId();
        return getDataTable(list);
    }

    /**
     * 查询关联项目
     */
    @GetMapping("/projectData")
    public TableDataInfo selectProjectListByProjectId(TcProject tcProject)
    {
        List<TcProjectForRelated> list = tcProjectService.selectProjectListByProjectId(tcProject);
        return getDataTable(list);
    }

    /**
     * 查询关联项目
     */
    @GetMapping("/projectDataQuery")
    public TableDataInfo selectProjectListByQuery(TcProject tcProject)
    {
        List<TcProjectForRelated> list = tcProjectService.selectProjectListByQuery(tcProject);
        return getDataTable(list);
    }
    /**
     * 查询关联项目
     */
    @GetMapping("/projectDataQueryPage")
    public TableDataInfo selectProjectListByQueryPage(TcProject tcProject)
    {
        startPage();
        List<TcProjectForRelated> list = tcProjectService.selectProjectListByQuery(tcProject);
        return getDataTable(list);
    }

    /**
     * 查询客户关联项目
     */
    @GetMapping("/listByAccount")
    public TableDataInfo selectProjectListByAccount(TcProject tcProject)
    {
        startPage();
        List<TcProjectForRelated> list = tcProjectService.selectProjectListByAccount(tcProject);
        return getDataTable(list);
    }

    /**
     * 导出项目管理列表
     */
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectParam tcProject)
    {
        List<ProjectExportVO> list = tcProjectService.selectTcProjectListExport(tcProject);
        ExcelUtil<ProjectExportVO> util = new ExcelUtil<ProjectExportVO>(ProjectExportVO.class);
        util.exportExcel(response, list, MessageUtils.message("project.exportSheetName"));
    }

    /**
     * 获取项目管理详细信息
     */
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId)
    {
        return success(tcProjectService.selectTcProjectByProjectId(projectId));
    }

    /**
     * 新增项目管理
     */
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TcProject tcProject)
    {
        return toAjax(tcProjectService.insertTcProject(tcProject, true));
    }

    /**
     * 修改项目管理
     */
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @DataUpdateCheck(key = "project_id", tableName = "tc_project")
    public AjaxResult edit(@RequestBody TcProject tcProject)
    {
        return toAjax(tcProjectService.updateTcProject(tcProject));
    }

    /**
     * 删除项目管理
     */
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds)
    {
        return toAjax(tcProjectService.deleteTcProjectByProjectIds(projectIds));
    }

    /**
     * 更改负责人
     */
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeOwner")
    @DataUpdateCheck(key = "project_id", tableName = "tc_project")
    public AjaxResult changeOwner(@RequestBody TcProject tcProject)
    {
        return toAjax(tcProjectService.changeOwner(tcProject));
    }

    /**
     * 设为标杆项目
     */
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping("/setModel")
    @DataUpdateCheck(key = "project_id", tableName = "tc_project")
    public AjaxResult setModel(@RequestBody TcProject tcProject)
    {
        return toAjax(tcProjectService.setModel(tcProject));
    }

    /**
     * 延期/丢单
     */
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping("/delayLost")
    @DataUpdateCheck(key = "project_id", tableName = "tc_project")
    public AjaxResult delayLost(@RequestBody TcProject tcProject)
    {
        return toAjax(tcProjectService.delayLost(tcProject));
    }

    /**
     * 审批
     */
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    @DataUpdateCheck(key = "project_id", tableName = "tc_project")
    public AjaxResult approve(@RequestBody TcProject tcProject)
    {
        return toAjax(tcProjectService.approve(tcProject));
    }


    /**
     * 查询当前客户的可见区域下拉
     */
    @GetMapping("/areas")
    public AjaxResult getAreasByCurUser() {
        return success(tcProjectService.selectAreasByCurUser());
    }
    /**
     * 获取数据范围内国家下拉
     */
    @GetMapping("/countries")
    public AjaxResult getCountriesByCurUser() {
        return success(tcProjectService.selectCountriesByCurUser());
    }

    @GetMapping(value = "/getProjectName/{agentId}")
    public AjaxResult getProjectName(@PathVariable("agentId") Long agentId) {
        return success(tcProjectService.getProjectName(agentId));
    }
}
