package com.qy.worksheetsystem.controller;

import com.qy.worksheetsystem.model.MessageResult;
import com.qy.worksheetsystem.service.ProjectPersonService;
import com.qy.worksheetsystem.util.JWTUtil;
import com.qy.worksheetsystem.vo.InsertJobWorkingVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/work/project/")
@Api(value = "项目管理、项目人员管理", tags = "项目管理、项目人员管理")
public class ProjectController {
    //项目管理 项目人员管理
    @Resource
    ProjectPersonService projectPersonService;

    @ApiOperation(value = "获取员工有效项目列表", notes = "获取员工有效项目列表")
    @RequestMapping(value = "/getProjectList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "项目状态", paramType = "query", dataType = "String"),
    })
    public MessageResult getProjectList(@RequestHeader("token") String token, String state) {
        Map result = new HashMap();

        String number = JWTUtil.getCacheNumber(token);
        if (StringUtils.isEmpty(number)) {
            return MessageResult.error("token异常");
        }
        //获取当前时间
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = sdf.format(currentDate);
        List<Map<String, String>> projectManagementByState = projectPersonService.getProjectListByState(state, dateTime);
        if (projectManagementByState.size() > 0) {
            result.put("code", 200);
            result.put("data", projectManagementByState);
            result.put("msg", "查询成功");

        } else {
            result.put("code", 500);
            result.put("data", projectManagementByState);
            result.put("msg", "未查询到数据！");
        }
        return MessageResult.success().add("data", result);
    }

    @ApiOperation(value = "获取员工时间范围内项目列表", notes = "获取员工时间范围内项目列表")
    @RequestMapping(value = "/getProjectByTimeList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "项目状态", paramType = "query", dataType = "String"),
    })
    public MessageResult getProjectByTimeList(@RequestHeader("token") String token, String state) {
        Map result = new HashMap();
        String number = JWTUtil.getCacheNumber(token);
        if (StringUtils.isEmpty(number)) {
            return MessageResult.error("token异常");
        }
        //获取当前时间
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = sdf.format(currentDate);
        List<Map<String, String>> projectManagementByState = projectPersonService.getProjectManagementByState(state, dateTime);
        if (projectManagementByState.size() > 0) {
            for (int i = 0; i < projectManagementByState.size(); i++) {
                Map<String, String> projectManagement = projectManagementByState.get(i);
                String expectedStartTime = String.valueOf(projectManagement.get("expectedStartTime"));
                String expectedEndTime = String.valueOf(projectManagement.get("expectedEndTime"));
                projectManagement.put("expectedStartTime", expectedStartTime);
                projectManagement.put("expectedEndTime", expectedEndTime);
            }
            result.put("code", 200);
            result.put("data", projectManagementByState);
            result.put("msg", "查询成功");

        } else {
            result.put("code", 500);
            result.put("data", projectManagementByState);
            result.put("msg", "未查询到数据！");
        }
        return MessageResult.success().add("data", result);
    }

    @ApiOperation(value = "获取项目里面员工详情", notes = "获取项目里面员工详情")
    @RequestMapping(value = "/getPersonListByProject", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "项目id", required = true, paramType = "query", dataType = "String"),
    })
    public MessageResult getPersonListByProject(@RequestHeader("token") String token, String id) {

        String number = JWTUtil.getCacheNumber(token);
        if (StringUtils.isEmpty(number)) {
            return MessageResult.error("token异常");
        }
        List<Map<String, String>> memberManagementcoll = projectPersonService.getMemberManagementInfoByPerobjectId(id);
        return MessageResult.success().add("list", memberManagementcoll);
    }

    @ApiOperation(value = "获取项目里员工启用或禁用", notes = "获取项目里员工启用或禁用")
    @RequestMapping(value = "/enableDisablePerson", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "personID", value = "员工ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "projectID", value = "项目ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "true启用，false禁用", required = true, paramType = "query", dataType = "Boolean"),
    })
    public MessageResult enableDisablePerson(@RequestHeader("token") String token, String personID, String projectID, Boolean state) {
        Map resultMap = new HashMap();
        String number = JWTUtil.getCacheNumber(token);
        if (StringUtils.isEmpty(number)) {
            return MessageResult.error("token异常");
        }
        int updateNum = projectPersonService.updateStateByPersonIdAndPerobject(state, personID, projectID);
        if (updateNum > 0) {
            resultMap.put("code", 200);
            resultMap.put("data", "修改条数：" + updateNum);
            resultMap.put("msg", "修改成功");
        } else {
            resultMap.put("code", 500);
            resultMap.put("data", "修改条数：" + updateNum);
            resultMap.put("code", "修改失败");
        }
        return MessageResult.success().add("resultMap", resultMap);
    }

    @ApiOperation(value = "项目里员工添加员工", notes = "项目里员工添加员工")
    @RequestMapping(value = "/addProjectPerson", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "personID", value = "员工ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "projectID", value = "项目ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "projectRoleID", value = "项目角色", required = true, paramType = "query", dataType = "String"),
    })
    public MessageResult addProjectPerson(@RequestHeader("token") String token, String personID, String projectID, String projectRoleID) {
        Map mapParam = new HashMap();
        String number = JWTUtil.getCacheNumber(token);
        if (StringUtils.isEmpty(number)) {
            return MessageResult.error("token异常");
        }
        //获取当前登录人
        String currentUser = projectPersonService.getPersonIdBYNumber(number);
        mapParam.put("personID", personID);
        mapParam.put("currentUser", currentUser);
        mapParam.put("projectID", projectID);
        mapParam.put("projectRoleID", projectRoleID);
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 定义时间格式化的模式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 按照指定格式进行格式化
        String formattedDateTime = now.format(formatter);
        mapParam.put("dateTime", formattedDateTime);
        mapParam.put("state", "1");
        mapParam.put("controUnit", "00000000-0000-0000-0000-000000000000CCE7AED4");
        mapParam.put("hrOrgUnit", "00000000-0000-0000-0000-000000000000CCE7AED4");
        String id = projectPersonService.getMemberManagementID();
        mapParam.put("id", id);
        //获取项目名称和编码
        Map<String, String> projectManagementMap = projectPersonService.getProjectManagementInfoBYId(projectID);
        String projectName = projectManagementMap.get("projectName");
        String projectNumber = projectManagementMap.get("number");
        mapParam.put("projectName", projectName);
        mapParam.put("projectNumber", projectNumber);

        Map map = projectPersonService.addProjectPerson(mapParam);
        return MessageResult.success().add("result", map);
    }

    @ApiOperation(value = "获取所有人员信息", notes = "获取所有人员信息")
    @RequestMapping(value = "/getPersonList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),
            //@ApiImplicitParam(name = "projectID", value = "项目ID", required = true, paramType = "query", dataType = "String"),
    })
    public MessageResult getPersonList(@RequestHeader("token") String token) {

        String number = JWTUtil.getCacheNumber(token);
        if (StringUtils.isEmpty(number)) {
            return MessageResult.error("token异常");
        }
        List<Map<String, String>> personAll = projectPersonService.getPersonAll();
        return MessageResult.success().add("persons", personAll);
    }

    @ApiOperation(value = "选择项目角色列表", notes = "选择项目角色列表")
    @RequestMapping(value = "/getProjectRoleList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),
    })
    public MessageResult getProjectRoleList(@RequestHeader("token") String token) {
        Map result = new HashMap();
        String number = JWTUtil.getCacheNumber(token);
        if (StringUtils.isEmpty(number)) {
            return MessageResult.error("token异常");
        }
        List<Map<String, String>> data = projectPersonService.getProjectRoleList();
        if (data.size() > 0) {
            result.put("code", 200);
            result.put("data", data);
            result.put("msg", "查询成功");

        } else {
            result.put("code", 500);
            result.put("data", data);
            result.put("msg", "未查询到数据！");
        }
        return MessageResult.success().add("data", result);
    }

}
