zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.changhong.epc.tenant.service.cost.impl;
 
import com.alibaba.fastjson.JSON;
import com.changhong.epc.bean.admin.CorresElField;
import com.changhong.epc.bean.tenant.cost.CostFromMapping;
import com.changhong.epc.bean.tenant.cost.CostType;
import com.changhong.epc.bean.tenant.cost.extend.CostTypeExtend;
import com.changhong.epc.bean.tenant.sunCloud.UpdataBusType;
import com.changhong.epc.constter.admin.AdminUrlConst;
import com.changhong.epc.constter.base.BaseConst;
import com.changhong.epc.constter.base.Context;
import com.changhong.epc.constter.system.SystemClients;
import com.changhong.epc.constter.system.run.EpcRestInterface;
import com.changhong.epc.rely.api.epc.admin.CostFromTemplateApi;
import com.changhong.epc.rely.api.service.CscApiService;
import com.changhong.epc.rely.api.tool.VersionFilterApi;
import com.changhong.epc.tenant.mapper.tenant.cost.CostFromMappingMapper;
import com.changhong.epc.tenant.mapper.tenant.cost.CostTypeMapper;
import com.changhong.epc.tenant.service.cost.CostTypeService;
import com.iemsoft.framework.cloud.core.base.ResMsg;
import com.iemsoft.framework.cloud.core.thread.Keys;
import com.iemsoft.framework.cloud.core.thread.ThreadData;
import com.iemsoft.framework.cloud.core.tools.JSONTool;
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
import com.iemsoft.framework.cloud.ribbon.RestInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Consumer;
 
@Service("costTypeService")
public class CostTypeServiceImpl implements CostTypeService, BaseConst,SystemClients {
    
    @Resource
    private CostFromMappingMapper costFromMappingMapper;
    
    @Resource
    private CostTypeMapper costTypeMapper;
 
    @Resource
    private CostFromTemplateApi costFromTemplateApi;
 
    //业务类型常量key
    private static final String BUSINESS_TYPE = "businessType";
 
    @Override
    public List<CostTypeExtend> selectConstByTypeAll(String type) {    
        List<CostTypeExtend> list = costTypeMapper.selectCostTypeAll(type);
//        CostFromMapping costForm = new CostFromMapping();
//        if(type != null){
//            for(CostTypeExtend cost : list){
//                costForm.setCtId(cost.getId());
//                costForm.setType(type);
//                cost.setCostFromMapping(costFromMappingMapper.select(costForm));
//                for(CostTypeExtend costs :cost.getChildren()){
//                    costForm.setCtId(costs.getId());
//                    costForm.setType(type);
//                    costs.setCostFromMapping(costFromMappingMapper.select(costForm));
//                }
//            }
//        }
        return list;
    }
 
    @Override
    public int deleteCostType(CostType costType) {
        return costTypeMapper.deleteCost(costType.getId())==1?1:-1;
    }
    
    /**
     * 添加费用类型
     */
    @Override
    public CostType insertCostType(CostTypeExtend costType) {
        costType.initParam();
        costTypeMapper.insertCost(costType);
        //添加表单映射
        if((costType.getCostFromMapping().size())>0){
            for(CostFromMapping form : costType.getCostFromMapping()){
                if(form.getFId()!=null && !form.getFId().equals("")){
                    form.setCtId(costType.getId());
                    form.initParam();
                    costFromMappingMapper.insert(form);
                }
            }
        }
        if(costType.getParentId()==0){
            CostTypeExtend cte = new CostTypeExtend();
            cte.SetProperties(costType); 
            //判断是否有关联表单
            return cte;
            
        }else if(costType.getParentId()!=0){
            costFromMappingMapper.deleteForm(costType.getParentId());
            
        }
        
        return costType;    
    }
    
    /**
     * 修改费用类型
     */
    @Override
    public int updateCostType(CostTypeExtend costType) {
        if(costType.getIsBlock()!=null && !Objects.equals("",costType.getIsBlock())) {
            //如果修改费用类型隐藏,更新主数据缓存
 
            RestInterface restInterface = SpringUtil.getBean(SERVER_ADMIN.getBeanName(), RestInterface.class);
            Object data = restInterface.post(AdminUrlConst.GET_CORRESELFIELD,
                    new ModelMap("businessMeaningCode", BUSINESS_TYPE), ResMsg.class, EpcRestInterface.getEpcHeads()).getData();
            List<CorresElField> list = JSONTool.toList(JSONTool.toJson(data), CorresElField.class);
            CorresElField corresElField = list.stream()
                    .filter(f -> f.getMdCode() != null && !Objects.equals("", f.getMdCode()))
                    .findFirst().orElse(null);
            if (corresElField!=null && corresElField.getMdCode()!=null) {
                System.out.println("当前主数据:"+corresElField.getMdCode());
                VersionFilterApi.filter(corresElField.getMdCode(), Context.getTenantIdStr(), Context.getCompanyIdStr());
            }
        }
        return costTypeMapper.updateCostType(costType);
    }
    
    /**
     * 分页查询费用类型
     */
    @Override
    public List<CostTypeExtend> seachePage(CostType costType) {
        costType.setDataStart(0);
        ThreadData.set(Keys.PAGE_SIZE, 20);
        List<CostTypeExtend> list = costTypeMapper.selectCostTypePage(costType);
//        CostFromMapping costForm = new CostFromMapping();
//        for(CostTypeExtend cost : list){
//            costForm.setCtId(cost.getId());
//            // System.out.println("添加父类");
//            cost.setCostFromMapping(costFromMappingMapper.select(costForm));
//            for(CostTypeExtend costs :cost.getChildren()){
//                // System.out.println("添加子类");
//                costForm.setCtId(cost.getId());;
//                costs.setCostFromMapping(costFromMappingMapper.select(costForm));
//            }
//        }
        return list;
    }
 
    @Override
    public int move(CostType data) {
        return costTypeMapper.move(data);
    }
 
    @Override
    public CostType selectSingle(CostType cost) {
        return costTypeMapper.selectByPrimaryKey(cost.getId());
    }
 
    @Override
    public List<CostTypeExtend> selectlike(String nodeName) {
        return costTypeMapper.selectlike(nodeName);
    }
    
    @Autowired
    CscApiService cscApiService;
    
    //同步公司业务类型
    public List<UpdataBusType> addCostTypeList(Consumer<UpdataBusType> consumer){
//        try{
            Map<String,Object> map =new HashMap<String,Object>();
            map.put("companyId", ThreadData.get(COMPANY_MASTER_ID));
            map.put("tenantId",ThreadData.get(TENANT_ID));
            
//            map.put("companyId","66");
//            map.put("tenantId","13");
            
            Map<String, Object> appointData = cscApiService.busTypeByComp(map);
            Object list = appointData.get("list");
            List<UpdataBusType> updataBusTypes = JSONTool.toList(JSON.toJSONString(list), UpdataBusType.class);
            //递归成列表
            List<UpdataBusType> u =new ArrayList<UpdataBusType>();
            this.getUpdataBusTypeList(updataBusTypes,u);
//            int i = 0;
            // System.out.println(u.size());
            for(UpdataBusType up:u){
                if(costTypeMapper.selectByName(up.getId()) == 0){
                    //插入
                    // System.out.println("没有-插入");
                    costTypeMapper.insertUpdataBusType(up);
                    consumer.accept(up);
                }
//                else{
//                    //更改
//                    // System.out.println("有-更改");
//                    //i += costTypeMapper.updateByCodeId(up);
//                }
            }
            return u;
//        }catch (Exception e) {
//            // System.err.println(e.getMessage());
//            return Collections.emptyList();
//        }
        
    }
 
 
    public static void getUpdataBusTypeList(List<UpdataBusType> updataBusTypes,List<UpdataBusType> u ){
        if(ObjectUtil.empty(updataBusTypes) || u == null){
            return;
        }
        for(UpdataBusType up:updataBusTypes){
            if(up != null){
                u.add(up);
                if(up.getChildren()!=null && up.getChildren().size()!=0){
                    getUpdataBusTypeList(up.getChildren(),u);
                }
            }
        }
    }
}