package com.qy.worksheetsystem.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qy.worksheetsystem.model.MessageResult;
import com.qy.worksheetsystem.model.MessageResultV2;
import com.qy.worksheetsystem.service.ProjectPersonService;
import com.qy.worksheetsystem.util.JWTUtil;
import com.qy.worksheetsystem.vo.InsertJobWorkingVO;
import com.qy.worksheetsystem.vo.ProjectPersonVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.ParseException;
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;

@Slf4j
@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 = "month", value = "月份", required = true, paramType = "query", 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, number);
        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", "未查询到数据！");
        }
        MessageResult resultV2 = MessageResult.success();
        resultV2.add("datalist", projectManagementByState);
        return resultV2;
    }

    @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 = "项目状态", required = true, 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", "未查询到数据！");
        }
        Map data = new HashMap();
        data.put("datalist", projectManagementByState);
        return MessageResult.success().add("data", projectManagementByState);
        //return MessageResult.success().add("dataList", projectManagementByState);
    }

    @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) throws ParseException {

        String number = JWTUtil.getCacheNumber(token);
        if (StringUtils.isEmpty(number)) {
            return MessageResult.error("token异常");
        }
        List<Map<String, Object>> memberManagementcoll = projectPersonService.getMemberManagementInfoByPerobjectId(id);
        Map<String, Object> map = memberManagementcoll.get(0);
        String expectedEndTime = map.get("expectedEndTime").toString();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        Date parse = sdf.parse(expectedEndTime);
        int status = 0;
        if (parse.compareTo(new Date()) >= 0) {
            status = 1;
        }
        Map data = new HashMap();
        for (int i = 0; i < memberManagementcoll.size(); i++) {
            Map<String, Object> memberMap = memberManagementcoll.get(i);
            memberMap.put("expectedEndTime", memberMap.get("expectedEndTime").toString());
        }
        data.put("datalist", memberManagementcoll);
        return MessageResult.success().add("list", memberManagementcoll)
                .add("name", map.get("projectName")).add("status", status);
        //.add("projectRoleName", map.get("projectRoleName"))
        //.add("projectRoleId", map.get("projectRoleId"));
        //return MessageResult.success().add("dataList", memberManagementcoll);
    }

    @ApiOperation(value = "获取项目里员工启用或禁用", notes = "获取项目里员工启用或禁用")
    @RequestMapping(value = "/enableDisablePerson", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),

    })
    public MessageResult enableDisablePerson(@RequestHeader("token") String token, @RequestBody ProjectPersonVo personVo) {
        String personID = personVo.getPersonID();
        String projectID = personVo.getProjectID();
        Boolean state = personVo.getState();

        Map resultMap = new HashMap();
        String number = JWTUtil.getCacheNumber(token);
        if (StringUtils.isEmpty(number)) {
            return MessageResult.error("token异常");
        }
        log.error("state--" + state);
        log.error("projectID--" + projectID);
        log.error("personID--" + personID);
        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"),
    })
    public MessageResult addProjectPerson(@RequestHeader("token") String token, @RequestBody ProjectPersonVo personVo) throws JsonProcessingException {
        log.error("addProjectPerson----");
        String personID = personVo.getPersonID(),
                projectID = personVo.getProjectID(),
                projectRoleID = personVo.getProjectRoleID();
        Map mapParam = new HashMap();
        String number = JWTUtil.getNumber(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);
        //获取人员信息
        Map<String, String> personMap = projectPersonService.getPerson(personID);
        // 获取当前时间
        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);
        ObjectMapper mapper = new ObjectMapper();
        log.error("projectID----" + projectID);
        //获取项目名称和编码
        //Map<String, String> projectManagementMap = projectPersonService.getProjectManagementInfoBYId(projectID);
        List<Map<String, String>> projectManagementlist = projectPersonService.getProjectManagementInfoBYId(projectID);
        Map<String, String> projectManagementMap = projectManagementlist.get(0);
        String projectName = projectManagementMap.get("projectName");
        String projectNumber = projectManagementMap.get("number");
        log.error("projectManagementMap----" + mapper.writeValueAsString(projectManagementMap));
        mapParam.put("projectName", projectName);
        mapParam.put("projectNumber", projectNumber);
        String billNumber = projectNumber + "-" + personMap.get("number");
        mapParam.put("number", billNumber);
        List<Map<String, String>> managementByNumber = projectPersonService.getManagementByNumber(billNumber);
        String mapvalue = mapper.writeValueAsString(mapParam);
        log.error("mapvalue----" + mapvalue);
        Map map = new HashMap();
        if (managementByNumber.size() <= 0) {
            map = projectPersonService.addProjectPerson(mapParam);
        } else {
            map.put("code", "500");
            map.put("msg", "编码重复：" + billNumber);
        }
        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();
        Map data = new HashMap();
        data.put("datalist", personAll);
        return MessageResult.success().add("data", personAll);
        //return MessageResult.success().add("dataList", 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>> projectPersonColl = 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", "未查询到数据！");
        //}
        Map data = new HashMap();
        data.put("datalist", projectPersonColl);
        return MessageResult.success().add("data", projectPersonColl);
        //return MessageResult.success().add("dataList", data);
    }

}
