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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.ownteam.dao.OwnTeamManageDao;
import com.bonus.hn.bearing.capacity.backstage.ownteam.entity.OwnTeamManageBean;
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("OwnTeamManageService")
public class OwnTeamManageServiceImpl implements OwnTeamManageService {

    @Resource
    private OwnTeamManageDao dao;
    @Override
    public List<OwnTeamManageBean> getOwnTeamsList(OwnTeamManageBean o) {
        return dao.getOwnTeamsList(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<OwnTeamManageBean> getProjectManagementTeamsFormList(OwnTeamManageBean o) {
        List<OwnTeamManageBean> list = dao.getProjectManagementTeamsFormList(o);
        List<OwnTeamManageBean> 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++) {
                        OwnTeamManageBean bean = new OwnTeamManageBean();
                        bean.setPostName(list.get(i).getPostName());
                        bean.setPostNum(list.get(i).getPostNum());
                        bean.setManageId(list.get(i).getManageId());
                        bean.setType("3");
                        bean.setIsRequired(list.get(i).getIsRequired());
                        bean.setIsData(list.get(i).getIsData());
                        result.add(bean);
                    }
                }else {
                    result.add(list.get(i));
                }
            }
        }
        return result;
    }

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

    @Override
    @Transactional
    public int addProManageTeam(OwnTeamManageBean bean) {
        int num = dao.determineIfExists(bean);
        if(num > 0){
            num = 3;
        }else {
            String finalData = bean.getFinalData();
            JSONArray dArray = JSONObject.parseArray(finalData);
            bean.setTeamNum(dArray.size()+"");
            num = dao.insertProManageTeam(bean);
            if (num == 1){
                for (int i = 0; i < dArray.size(); i++) {
                    JSONObject dObject = JSONObject.parseObject(dArray.get(i).toString());
                    OwnTeamManageBean b = new OwnTeamManageBean();
                    b.setName(dObject.get("name").toString());
                    b.setSex(dObject.get("sex").toString());
                    b.setNation(dObject.get("nation").toString());
                    b.setIdNumber(dObject.get("idNumber").toString());
//                    b.setImplementation(dObject.get("implementation").toString());
                    b.setImplementation(StringHelper.isEmpty(dObject.get("implementation").toString()) ? "" : 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());
                    num = dao.addProManageTeam(b);
                }
            }
        }
        return num;
    }

    @Override
    @Transactional
    public int updateProManageTeam(OwnTeamManageBean bean) {
        int num = dao.determineIfExists(bean);
        if(num > 0){
            num = 3;
        }else {
            String finalData = bean.getFinalData();
            JSONArray dArray = JSONObject.parseArray(finalData);
            bean.setTeamNum(dArray.size()+"");
            dao.updateProManage(bean);
            String teamId = "";
            for (int i = 0; i < dArray.size(); i++) {
                JSONObject dObject = JSONObject.parseObject(dArray.get(i).toString());
                OwnTeamManageBean b = new OwnTeamManageBean();
                b.setName(dObject.get("name").toString());
                b.setSex(dObject.get("sex").toString());
                b.setNation(dObject.get("nation").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.setTeamName(bean.getTeamName());
                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 delOwnTeamById(OwnTeamManageBean bean) {
        return dao.delOwnTeamById(bean);
    }

    @Override
    public List<TreeBean> getCompanyProTree(TreeBean o) {
        return dao.getCompanyProTree(o);
    }

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

    @Override
    @Transactional
    public int bindOrUnBindProTeam(OwnTeamManageBean 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 {
            //解绑
            String proId = dao.getProIdByOwnId(bean);
            if (StringHelper.isEmpty(proId)){
                num = dao.unBindProTeam(bean);
                if (num == 1){
                    bean.setStatus("0");
                    bean.setOrgId("");
                    num = dao.updateStatus(bean);
                }
            }else {
                return 3;
            }
        }
        return num;
    }

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

    @Override
    public List<OwnTeamManageBean> getOwnPersonList(OwnTeamManageBean o) {
        return dao.getOwnPersonList(o);
    }

    @Override
    public int updateOwnTeamStatus(OwnTeamManageBean bean) {
        return dao.updateOwnTeamStatus(bean);
    }
}
