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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bonus.hn.bearing.capacity.backstage.companypro.entity.ProjectManagerBean;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.StringHelper;
import com.bonus.hn.bearing.capacity.backstage.select.entity.DataDictBean;
import com.bonus.hn.bearing.capacity.backstage.teamset.dao.TeamSetDao;
import com.bonus.hn.bearing.capacity.backstage.teamset.entity.ResourceManage;
import com.bonus.hn.bearing.capacity.backstage.teamset.entity.ResourceSetBean;
import com.bonus.hn.bearing.capacity.backstage.teamset.entity.StringDataBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author xliu
 * @date 2023/11/7 21:22
 */
@Service("TeamSetService")
public class TeamSetServiceImpl implements TeamSetService{

    @Resource
    private TeamSetDao teamSetDao;


    @Override
    public List<ResourceSetBean> getTeamSetByType(ResourceSetBean bean) {
        return teamSetDao.getTeamSetByType(bean);
    }

    @Override
    @Transactional
    public int addBranchOfficeTeamSet(StringDataBean bean) {
        ResourceSetBean resourceSetBean = new ResourceSetBean();
        String strJson = bean.getStringKey();
        String resourceName = bean.getResourceName();
        String volLevelIds = bean.getVolLevelIds();
        String type = bean.getType();
        String groupType = bean.getGroupType();
        String notes = bean.getNotes();
        String resourceSetId = bean.getResourceSetId();
        resourceSetBean.setResourceName(resourceName);
        resourceSetBean.setVolLevel(volLevelIds);
        resourceSetBean.setResourceType(bean.getResourceType());
        resourceSetBean.setGroupType(groupType);
        resourceSetBean.setResourceNotes(notes);

        int setNum =0;
        if("1".equals(type)){
            //新增
            setNum = teamSetDao.addResourceSet(resourceSetBean);
            if(setNum <= 0){
                return 0;
            }
        }else{                //修改
            resourceSetBean.setResourceSetId(resourceSetId);
            setNum = teamSetDao.updateResourceSet(resourceSetBean);
        }


        System.err.println("ResourceSetBean="+resourceSetBean);
        System.err.println("strJson="+strJson);
        JSONArray jsonArray = JSONArray.parseArray(strJson);
        for (int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject  = jsonArray.getJSONObject(i);
            ResourceManage resourceManage = new ResourceManage();
            String id = jsonObject.getString("id");
            String dictKey = jsonObject.getString("dictKey");
            String choseNum = jsonObject.getString("choseNum");
            String LAY_NUM = jsonObject.getString("LAY_NUM");


            resourceManage.setManageResourceId(resourceSetBean.getResourceSetId());
            resourceManage.setManagePersonId(id);
            resourceManage.setManagePersonName(dictKey);
            resourceManage.setManageNum(choseNum);
            resourceManage.setManageSort(LAY_NUM);
            if("1".equals(type)) {
                //新增
                teamSetDao.addResourceManage(resourceManage);
            }else {
                String manageId = jsonObject.getString("manageId");
                resourceManage.setManageId(manageId);
                teamSetDao.updateResourceManage(resourceManage);
            }

        }
        return 1;
    }

    @Override
    public int updateTeamSetStatus(ResourceSetBean bean) {
        return teamSetDao.updateTeamSetStatus(bean);
    }

    @Override
    public int getTeamByLevelAndName(ResourceSetBean bean) {
        return teamSetDao.getTeamByLevelAndName(bean);
    }

    @Override
    public int getTeamByTeamTypeAndName(ResourceSetBean bean) {
        return teamSetDao.getTeamByTeamTypeAndName(bean);
    }

    @Override
    public List<DataDictBean> getDataDictByDickValModify(DataDictBean o) {
        return teamSetDao.getDataDictByDickValModify(o);
    }

    @Override
    public int delTeamSet(StringDataBean bean) {
        int nums = teamSetDao.getUseNumByResourceId(bean.getResourceSetId(),bean.getDeleteType());
        if(nums>0){
            return 3;
        }
        return teamSetDao.delTeamSet(bean);
    }

    @Override
    public int delDataDict(StringDataBean bean) {
        return teamSetDao.delDataDict(bean);
    }

    @Override
    public int saveDataDict(StringDataBean bean) {
        int nums = 0;
        String strJson = bean.getStringKey();
        JSONArray jsonArray = JSONArray.parseArray(strJson);
        String pId = teamSetDao.getDataDictIdByType(bean.getDataDictType());
        for (int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject  = jsonArray.getJSONObject(i);
            DataDictBean dictBean = new DataDictBean();
            String id =  jsonObject.getString("id");
            String dictKey =  jsonObject.getString("dictKey");
            String parentId =  jsonObject.getString("parentId");
            String isNewAdd =  jsonObject.getString("isNewAdd");
            dictBean.setId(id);
            dictBean.setDictKey(dictKey);
            dictBean.setParentId(parentId);
            dictBean.setDictSort((i+1)+"");
            if("0".equals(isNewAdd)){
                nums =  teamSetDao.updateDataDict(dictBean);
            }else{
                dictBean.setParentId(pId);
                int numSort =-1;
                if(StringHelper.isNotEmpty(bean.getResourceSetId())){
                    numSort = teamSetDao.getManageSortByManageResourceId(bean.getResourceSetId());
                }

                nums = teamSetDao.addDataDict(dictBean);
                ResourceManage resourceManage = new ResourceManage();
                resourceManage.setManagePersonId(dictBean.getId());
                resourceManage.setManageResourceId(bean.getResourceSetId());
                resourceManage.setManagePersonName(dictKey);
                resourceManage.setManageNum("0");
                if(numSort != -1){
                    resourceManage.setManageSort(numSort+"");
                    teamSetDao.addResourceManage(resourceManage);
                }

            }

        }

        return nums;
    }
}
