package com.bonus.hn.bearing.capacity.backstage.subteam.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.subteam.dao.SubTeamDao;
import com.bonus.hn.bearing.capacity.backstage.subteam.entity.SubTeamBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author bonus
 */
@Service("SubTeamService")
public class SubTeamServiceImpl implements SubTeamService{

    @Resource
    private SubTeamDao dao;
    @Override
    public List<SubTeamBean> getSubTeamsList(SubTeamBean o) {
        return dao.getSubTeamsList(o);
    }

    @Override
    public List<SubTeamBean> getSubTeamsFormList(SubTeamBean o) {
        List<SubTeamBean> list = dao.getSubTeamsFormList(o);
        List<SubTeamBean> 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++) {
                        SubTeamBean bean = new SubTeamBean();
                        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<SubTeamBean> getSubTeamNum(SubTeamBean o) {
        return dao.getSubTeamNum(o);
    }

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

    @Override
    @Transactional
    public int addSubTeam(SubTeamBean 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(size+"");

            num = dao.insertTmTeamSub(bean);
            if (num == 1){
                for (int i = 0; i < dArray.size(); i++) {
                    JSONObject dObject = JSONObject.parseObject(dArray.get(i).toString());
                    SubTeamBean b = new SubTeamBean();
                    b.setName(dObject.get("name").toString());
                    b.setSex(dObject.get("sex").toString());
                    b.setIdNumber(dObject.get("idNumber").toString());
                    b.setNation(dObject.get("nation").toString());
                    b.setIsSocialSecurity(dObject.get("isSocialSecurity").toString());
                    b.setIsEmploymentAgreement(dObject.get("isEmploymentAgreement").toString());
                    b.setIsSalaryPaymentCertificate(dObject.get("isSalaryPaymentCertificate").toString());
                    b.setTeamTypeId(bean.getTeamTypeId());
                    b.setManageId(dObject.get("manageId").toString());
                    b.setSubTeamId(bean.getSubTeamId());
                    num = dao.insertTmTeamPerson(b);
                }
            }
        }
        return num;
    }

    @Override
    @Transactional
    public int updateSubTeam(SubTeamBean 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(size+"");
            dao.updateSubTeam(bean);
            String teamId = "";
            for (int i = 0; i < dArray.size(); i++) {
                JSONObject dObject = JSONObject.parseObject(dArray.get(i).toString());
                SubTeamBean b = new SubTeamBean();
                b.setName(dObject.get("name").toString());
                b.setSex(dObject.get("sex").toString());
                b.setIdNumber(dObject.get("idNumber").toString());
                b.setNation(dObject.get("nation").toString());
                b.setIsSocialSecurity(dObject.get("isSocialSecurity").toString());
                b.setIsEmploymentAgreement(dObject.get("isEmploymentAgreement").toString());
                b.setIsSalaryPaymentCertificate(dObject.get("isSalaryPaymentCertificate").toString());
                b.setTeamTypeId(bean.getTeamTypeId());
                b.setManageId(dObject.get("manageId").toString());
                b.setTeamName(bean.getTeamName());
                if (dObject.get("type").toString().equals("2")){
                    b.setSubTeamId(bean.getId());
                    num = dao.insertTmTeamPerson(b);
                }else {
                    teamId = dObject.get("tmPersonId").toString();
                    b.setTmPersonId(teamId);
                    num = dao.updateTeamPerson(b);
                }
            }
        }
        return num;
    }

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

    @Override
    public int bindOrUnBindProTeam(SubTeamBean 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){
                num = dao.getIsExistOtherRelation(bean);
                if (num == 0) {
                    bean.setStatus("0");
                    bean.setProId("");
                    num = dao.updateStatus(bean);
                }
            }
        }
        return num;
    }

    @Override
    public int delSubTeamTeamById(SubTeamBean bean) {
        return dao.delSubTeamTeamById(bean);
    }

    @Override
    public int delSubTeamPersonById(SubTeamBean bean) {
        return dao.delSubTeamPersonById(bean);
    }

    @Override
    public List<SubTeamBean> getSubcontractaList(SubTeamBean o) {
        return dao.getSubcontractaList(o);
    }

    @Override
    public int delSubcontractById(SubTeamBean bean) {
        return dao.delSubcontractById(bean);
    }

    @Override
    public int addSubcontract(SubTeamBean bean) {
        int num = dao.deteName(bean);
        if(num > 0){
            num = 3;
        }else {
            num = dao.addSubcontract(bean);
        }
        return  num;
    }

    @Override
    public int upSubcontract(SubTeamBean bean) {
        int num = dao.deteName(bean);
        if(num > 0){
            num = 3;
        }else {
            num = dao.upSubcontract(bean);
        }
        return  num;
    }

    @Override
    public List<SubTeamBean> getTowerBindList(SubTeamBean o) {
        return dao.getTowerBindList(o);
    }

    @Override
    public int bindTowerBindTeam(SubTeamBean bean) {
            bean.setUserId(UserUtil.getLoginUser().getId().toString());
            bean.setTime(DateTimeHelper.getNowDayHMS());
            int num = 0;
            if ("1".equals(bean.getType())){
                //绑定
                num = dao.bindTowerBindTeam(bean);
//                if (num == 1){
//                    bean.setStatus("1");
//                    num = dao.updateStatus(bean);
//                }
            }else {
                //解绑
                num = dao.unbindTowerBindTeam(bean);
//                if (num == 1){
//                    bean.setStatus("0");
//                    bean.setProId("");
//                    num = dao.updateStatus(bean);
//                }
            }
            return num;
    }

    @Override
    public Map<String, Object> getTowerSubcontract(SubTeamBean o) {
        Map<String, Object> map = new HashMap<>(100);
        List<SubTeamBean> list = dao.getdata1Map(o);
        List<SubTeamBean> list1 = dao.getdata2Map(o);
        map.put("data1",list);
        map.put("data2",list1);
        return map;
    }

    @Override
    public int bindTowerSubcon(SubTeamBean bean) {
        int result = 0;
        bean.setUserId(UserUtil.getLoginUser().getId().toString());
        int num = dao.delbindTowerSubcon(bean);
        // 获取ids数组
        String ids = bean.getIds();
        String[] arr = ids.split(",");
        if (!ids.isEmpty() && arr.length > 0) {
            for (int i = 0; i < arr.length; i++) {
                String id = arr[i];
                bean.setTempId(id);
                result = dao.bindTowerSubcon(bean);
            }
        }else {
            result = 1;
        }
        return result;
    }

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

    @Override
    public List<SubTeamBean> getSubPersonList(SubTeamBean o) {
        return dao.getSubPersonList(o);
    }

    @Override
    public int updateSubTeamStatus(SubTeamBean bean) {
        return dao.updateSubTeamStatus(bean);
    }

}
