package com.bonus.hn.bearing.capacity.backstage.companypro.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bonus.hn.bearing.capacity.backstage.companypro.dao.ProjectManagerDao;
import com.bonus.hn.bearing.capacity.backstage.companypro.entity.ProjectManagerBean;
import com.bonus.hn.bearing.capacity.backstage.companypro.entity.TreeBean;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.DateTimeHelper;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.StringHelper;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.UserUtil;
import com.bonus.hn.bearing.capacity.backstage.select.entity.SelectBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 包名称：com.bonus.hn.bearing.capacity.backstage.companypro.service
 * 类名称：ProjectManagerServiceImpl
 * 类描述：
 * 创建人：@author xxxxxx
 * 创建时间：2023年11月06日 16:27
 */
@Service("ProjectManagerService")
public class ProjectManagerServiceImpl implements ProjectManagerService{

    @Resource
    private ProjectManagerDao dao;
    @Override
    public List<ProjectManagerBean> getProjectManagementTeamsList(ProjectManagerBean o) {
        return dao.getProjectManagerTeamsList(o);
    }

    @Override
    public List<SelectBean> getBranchName(SelectBean o) {
        return dao.getBranchName(o);
    }

    @Override
    public List<SelectBean> getVoltageLevel(SelectBean o) {
        return dao.getVoltageLevel(o);
    }

    @Override
    public List<SelectBean> getProName(SelectBean o) {
        return dao.getProName(o);
    }

    @Override
    public List<SelectBean> getTeamType(SelectBean o) {
        return dao.getTeamType(o);
    }

    @Override
    public List<ProjectManagerBean> getProjectManagementTeamsFormList(ProjectManagerBean o) {
        List<ProjectManagerBean> list = dao.getProjectManagementTeamsFormList(o);
        List<ProjectManagerBean> result = new ArrayList<>();
        if ("2".equals(o.getType())){
            result = list;
        }else {
            for (int i = 0; i < list.size(); i++) {
                int num = Integer.parseInt(list.get(i).getPostNum());
                if (num > 1){
                    for (int j = 0; j < num; j++) {
                        ProjectManagerBean bean = new ProjectManagerBean();
                        bean.setPostName(list.get(i).getPostName());
                        bean.setPostNum(list.get(i).getPostNum());
                        bean.setManageId(list.get(i).getManageId());
                        bean.setType("3");
                        bean.setIsData(list.get(i).getIsData());
                        bean.setIsRequired(list.get(i).getIsRequired());
                        result.add(bean);
                    }
                }else {
                    result.add(list.get(i));
                }
            }
        }
        return result;
    }

    @Override
    public List<ProjectManagerBean> getProManageTeamNum(ProjectManagerBean o) {
        return dao.getProManageTeamNum(o);
    }

    @Override
    @Transactional
    public int addProManageTeam(ProjectManagerBean bean) {
        int num = dao.determineIfExists(bean);
        if(num > 0){
            num = 3;
        }else {
            String finalData = bean.getFinalData();
            JSONArray dArray = JSONObject.parseArray(finalData);
            int size = 0;
            for (int i = 0; i < dArray.size(); i++) {
                JSONObject dObject = JSONObject.parseObject(dArray.get(i).toString());
                if (!StringHelper.isEmpty(dObject.get("name").toString())){
                    size ++;
                }
            }
            bean.setTeamNum(String.valueOf(size));
            num = dao.insertProManageTeam(bean);

            if (num == 1){
                for (int i = 0; i < dArray.size(); i++) {
                    JSONObject dObject = JSONObject.parseObject(dArray.get(i).toString());
                    ProjectManagerBean b = new ProjectManagerBean();
                    b.setName(dObject.get("name").toString());
                    b.setSex(dObject.get("sex").toString());
                    b.setIdNumber(dObject.get("idNumber").toString());
                    b.setImplementation(StringHelper.isEmpty(dObject.get("implementation").toString()) ? "" : dObject.get("implementation").toString());
//                    b.setImplementation(dObject.get("implementation").toString());
//                    b.setVocationalSkillLevel(dObject.get("vocationalSkillLevel").toString());
                    b.setVocationalSkillLevel(StringHelper.isEmpty(dObject.get("vocationalSkillLevel").toString()) ? "" : dObject.get("vocationalSkillLevel").toString());
                    b.setTeamTypeId(bean.getTeamTypeId());
                    b.setManageId(dObject.get("manageId").toString());
                    b.setTeamId(bean.getTeamId());
                    b.setIsPersonCharge(dObject.get("isPersonCharge").toString());
                    num = dao.addProManageTeam(b);
                }
            }
        }
        return num;
    }

    @Override
    @Transactional
    public int updateProManageTeam(ProjectManagerBean bean) {
        int num = dao.determineIfExists(bean);
        if(num > 0){
            num = 3;
        }else {
            String finalData = bean.getFinalData();
            JSONArray dArray = JSONObject.parseArray(finalData);
            int size = 0;
            for (int i = 0; i < dArray.size(); i++) {
                JSONObject dObject = JSONObject.parseObject(dArray.get(i).toString());
                if (!StringHelper.isEmpty(dObject.get("name").toString())){
                    size ++;
                }
            }
            bean.setTeamNum(String.valueOf(size));
            dao.updateProManage(bean);
            String teamId = "";
            for (int i = 0; i < dArray.size(); i++) {
                JSONObject dObject = JSONObject.parseObject(dArray.get(i).toString());
                ProjectManagerBean b = new ProjectManagerBean();
                b.setName(dObject.get("name").toString());
                b.setSex(dObject.get("sex").toString());
                b.setIdNumber(dObject.get("idNumber").toString());
                b.setImplementation(StringHelper.isEmpty(dObject.get("implementation").toString()) ? "" : dObject.get("implementation").toString());
                b.setVocationalSkillLevel(StringHelper.isEmpty(dObject.get("vocationalSkillLevel").toString()) ? "" : dObject.get("vocationalSkillLevel").toString());
                b.setTeamTypeId(bean.getTeamTypeId());
                b.setManageId(dObject.get("manageId").toString());
                b.setTeamName(bean.getTeamName());
                b.setIsPersonCharge(dObject.get("isPersonCharge").toString());
                if (dObject.get("type").toString().equals("2")){
                    b.setTeamId(bean.getId());
                    num = dao.addProManageTeam(b);
                }else {
                    teamId = dObject.get("tmPersonId").toString();
                    b.setTmPersonId(teamId);
                    num = dao.updateProManageTeam(b);
                }
            }
        }
        return num;
    }

    @Override
    public int delProManageTeamById(ProjectManagerBean bean) {
        return dao.deleteProManageTeam(bean);
    }

    @Override
    public List<TreeBean> getCompanyProTree(TreeBean o) {
        o.setLevel(UserUtil.getLoginUser().getLevel());
        o.setOrgIds(UserUtil.getLoginUser().getOrgId());
        return dao.getCompanyProTree(o);
    }

    @Override
    public List<ProjectManagerBean> getTeamsList(ProjectManagerBean o) {
        return dao.getTeamsList(o);
    }

    @Override
    @Transactional
    public int bindOrUnBindProTeam(ProjectManagerBean bean) {
        bean.setUserId(UserUtil.getLoginUser().getId().toString());
        bean.setTime(DateTimeHelper.getNowDayHMS());
        int num = 0;
        if ("1".equals(bean.getType())){
            //绑定
            num = dao.bindProTeam(bean);
            if (num == 1){
                bean.setStatus("1");
                num = dao.updateStatus(bean);
            }
        }else {
            //解绑
            num = dao.unBindProTeam(bean);
            if (num == 1){
                int code = dao.getIsExistRelationship(bean);
                if (code == 0){
                    bean.setStatus("0");
                    bean.setProId("");
                    num = dao.updateStatus(bean);
                }
            }
        }
        return num;
    }

    @Override
    public int delProTeamPersonById(ProjectManagerBean bean) {
        return dao.delProTeamPersonById(bean);
    }

    @Override
    public List<SelectBean> getProjectName(SelectBean o) {
        return dao.getProjectName(o);
    }

    @Override
    public List<SelectBean> getEngineerLevel(SelectBean o) {
        return dao.getEngineerLevel(o);
    }

    @Override
    public List<SelectBean> getBearingCapacity(SelectBean o) {
        return dao.getBearingCapacity(o);
    }

    @Override
    public List<SelectBean> getRealNameSystem(SelectBean o) {
        return dao.getRealNameSystem(o);
    }

    @Override
    public List<ProjectManagerBean> getProPersonList(ProjectManagerBean o) {
        o.setLevel(UserUtil.getLoginUser().getLevel());
        o.setOrgIds(UserUtil.getLoginUser().getOrgId());
        return dao.getProPersonList(o);
    }
}
