whycrzg
2021-02-03 9948b3f9a8d2d471a608b169de946eeb7d08b4b5
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package com.fgkj.services;
 
import com.fgkj.dto.*;
import com.fgkj.mapper.UinfDaoFactory;
import com.fgkj.util.*;
import com.fgkj.mapper.BattTestData;
import com.fgkj.mapper.impl.BattDischarge_planMapper;
import com.fgkj.mapper.impl.BattInfMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
@Service
public class BattDischarge_planService {
 
    @Resource
    private BattDischarge_planMapper mapper;
    @Resource
    private BattInfMapper battInfMapper;
 
    @Resource
    private User_logService userLogService;
    @Autowired
    DataSourceTransactionManager dataSourceTransactionManager;
    @Autowired
    TransactionDefinition transactionDefinition;
 
    ServiceModel model = new ServiceModel();
    
    public ServiceModel add(BattDischarge_plan plan){
        ServiceModel model = new ServiceModel();
        Boolean bl= null;
        try {
            bl = mapper.add(plan)>0;
        } catch (Exception e) {
            e.printStackTrace();
            model.setCode(0);
            model.setMsg("添加失败!");
            return model;
        }
        if(bl){
            model.setCode(1);
            model.setMsg("添加成功!");
        }else{
            model.setCode(0);
            model.setMsg("添加失败!");
        }
        return model;
    }
    public ServiceModel update(BattDischarge_plan obj){
        ServiceModel model = new ServiceModel();
        Boolean bl= null;
        try {
            bl = mapper.update(obj)>0;
        } catch (Exception e) {
            e.printStackTrace();
            model.setCode(0);
            model.setMsg("修改失败!");
            return model;
        }
        if(bl){
            model.setCode(1);
            model.setMsg("修改成功!");
        }else{
            model.setCode(0);
            model.setMsg("修改失败!");
        }
        return model;
    }
    //8.1批量添加电池计划
    public ServiceModel addPro(@RequestBody List<BattDischarge_plan> list) {
        ServiceModel model = new ServiceModel();
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        Boolean bl = true;
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (!bl)continue;
                BattDischarge_plan bdp = list.get(i);
                try {
                    bl = mapper.addPro(bdp) > 0;
                } catch (Exception e) {
                    e.printStackTrace();
                    dataSourceTransactionManager.rollback(transactionStatus);
                    model.setCode(0);
                    model.setMsg("添加失败!");
                    return model;
                }
                String msg = "添加" + bdp.getNote() + "电池组在" + bdp.getDischarge_starttime() + "的放电计划";
                User_log ulog = UinfDaoFactory.CreateULog(UinfDaoFactory.Increase, msg);
                userLogService.add(ulog);
            }
        }else{
            bl = false;
        }
        if (bl) {
            dataSourceTransactionManager.commit(transactionStatus);
            model.setCode(1);
            model.setMsg("添加成功!");
        } else {
            dataSourceTransactionManager.rollback(transactionStatus);
            model.setCode(0);
            model.setMsg("添加失败!");
        }
        return model;
    }
     //8.1批量修改放电计划的开始时间
    public ServiceModel updatePro(List<BattDischarge_plan> list) {
        ServiceModel model = new ServiceModel();
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        Boolean bl = true;
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (!bl) continue;
                BattDischarge_plan bdp = list.get(i);
                try {
                    bl = mapper.updatePro(bdp) > 0;
                } catch (Exception e) {
                    e.printStackTrace();
                    dataSourceTransactionManager.rollback(transactionStatus);
                    model.setCode(0);
                    model.setMsg("添加失败!");
                    return model;
                }
                {
                    String msg = "批量修改放电计划的开始时间" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(bdp.getDischarge_starttime())+ " " + bdp.getBattgroupid() + "组 " + bdp.getNote();
                    User_log ulog = UinfDaoFactory.CreateULog(UinfDaoFactory.Alter, msg);
                    userLogService.add(ulog);
                }
            }
        }else{
            bl = false;
        }
        if (bl) {
            dataSourceTransactionManager.commit(transactionStatus);
            model.setCode(1);
            model.setMsg("修改成功!");
        } else {
            dataSourceTransactionManager.rollback(transactionStatus);
            model.setCode(0);
            model.setMsg("修改失败!");
        }
        return model;
    }
 
    public ServiceModel del(BattDischarge_plan plan){
        ServiceModel model = new ServiceModel();
        Boolean bl=mapper.del(plan)>0;
        if(bl){
            model.setCode(1);
            model.setMsg("删除成功!");
        }else{
            model.setCode(0);
            model.setMsg("删除失败!");
        }
        return model;
    }
    public ServiceModel searchAll(){
        ServiceModel model = new ServiceModel();
        List list=mapper.searchAll();
        if(list!=null&&list.size()>0){
            model.setCode(1);
            model.setMsg("查询成功!");
            model.setData(list);
        }else{
            model.setCode(0);
            model.setMsg("查询失败!");
        }
        return model;
    }
    
    //8.1批量添加作业计划/修改作业计划时时先检测计划的可行性
    /*原则:
         *1.同机房(或设备)同一时间只能一组电池组放电
         *2.同机房(或设备)内电池组放电时间相隔至少3天*/    
    public ServiceModelOnce judgePlan(List<BattDischarge_plan> listAll){
        ServiceModelOnce model = new ServiceModelOnce();
        List<BattDischarge_plan> list = new ArrayList();//存放满足计划设定条件的可行计划
        if(listAll!=null&&listAll.size()>0){
            for(int i=0;i<listAll.size();i++){
                int flag=1;
                List<String> strings = mapper.judgePlan(listAll.get(i));
                if (strings!=null&&strings.size()>0){
                    flag=0;
                };
                if(flag==1){//判断通过是否数据库存在同机房放电计划,满足条件
                    if(list.size()>0){
                        int judge=judgeList(listAll.get(i), list);
                        if(judge==1){////判断通过时间相隔3天,满足条件
                            list.add(listAll.get(i));
                        }else{
                            continue;
                        }
                    }else{
                        list.add(listAll.get(i));
                    }
                    
                }
            }
        }
        if(list!=null&&list.size()>0){
            model.setCode(1);
            model.setData(list);
            //TODO perry
             model.setSum(list.size());//可以添加的总数
        }else{
            model.setCode(0);
            //TODO perry
             model.setSum(0);
        }
        //System.out.println(model);
        return model;
    }
    
    //放电计划中添加计划时,有没有出现同机房存在电池组有计划
    public  int judgeList(Object obj1,Object obj2){
        BattDischarge_plan bplan=(BattDischarge_plan) obj1;
        List<BattDischarge_plan> list=(List<BattDischarge_plan>) obj2;
        int flag=1;
        if(list!=null&&list.size()>0){
            for(int i=0;i<list.size();i++){
                
                if(bplan.getNote().equals(list.get(i).getNote())){
                    //System.out.println("1:"+bplan.getNote()+"  "+list.get(i).getNote());
                    int hours=ActionUtil.HoursBetween(bplan.getDischarge_starttime(), list.get(i).getDischarge_starttime());
                    if(hours<BattTestData.hoursBetween){
                        flag=0;
                        break;
                    }
                }
            }
        }
        return flag;
    }
 
    //8.1放电计划查询
    public ServiceModel serchByCondition(Batt_Maint_Dealarm bmd) {
        ServiceModel model = new ServiceModel();
        PageBean pageBean = bmd.getPageBean();
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize(),true);
        List<Batt_Maint_Dealarm> list = mapper.serchByCondition(bmd);
        if (list != null && list.size() > 0) {
            PageInfo<Batt_Maint_Dealarm> pageInfo = new PageInfo<>(list);
            model.setCode(1);
            model.setMsg("查询成功!");
            model.setData(pageInfo);
        } else {
            model.setCode(0);
            model.setMsg("查询失败!");
        }
        return model;
    }
    //8.1放电计划管理中查询
    public ServiceModel serchBattgroupinfo(User_inf obj){
        ServiceModel model = new ServiceModel();
        List<BattInf> list=battInfMapper.serchBattgroupinfo(obj);
        if(list!=null&&list.size()>0){
            model.setCode(1);
            model.setMsg("查询成功!");
            model.setData(list);
        }else{
            model.setCode(0);
            model.setMsg("查询失败!");
        }
        return model;
    }
    //新版添加放电计划查询<排除设置过放电计划的电池组>
    public ServiceModel serchBattPlan(BattInf obj){
        ServiceModel model = new ServiceModel();
        List list=battInfMapper.serchBattPlan(obj);
        if(list!=null&&list.size()>0){
            model.setCode(1);
            model.setMsg("查询成功!");
            model.setData(list);
        }else{
            model.setCode(0);
            model.setMsg("查询失败!");
        }
        return model;
    }
    //8.1查询未开始放电的放电计划总数
    public ServiceModelOnce searchNums(User_inf obj) {
        ServiceModelOnce model = new ServiceModelOnce();
        int nums = mapper.searchNums(obj);
        model.setSum(nums);
        return model;
    }
     //8.1放电计划管理中饼状图(放电计划统计查询)
    /*BattIndexInFbs:未放电
     *Preset_time:放电延时
     *Preset_cap: 放电完成
     *Preset_monvol:放电进行中
     *Preset_groupvol:放电失败 
     *num: 总数*/
    public ServiceModelOnce searchState(User_inf obj){
        ServiceModelOnce model = new ServiceModelOnce();
        List<BattDischarge_plan> list=mapper.searchState(obj);
        BattDischarge_plan bplan=new BattDischarge_plan();//存放返回各种放电计划状态对应的值
        bplan.setBattIndexInFbs(0);
        bplan.setPreset_time(0);
        bplan.setPreset_cap(0f);
        bplan.setPreset_monvol(0f);
        bplan.setPreset_groupvol(0f);
        int test_plan0=0;
        int test_plan1=0;
        int test_plan2=0;
        int test_plan3=0;
        int test_plan4=0;
        if(list!=null && list.size()>0){
            for(int i=0;i<list.size();i++){
                if(list.get(i).getDischarge_state()==BattTestData.test_plan0){//未放电
                    test_plan0+=list.get(i).getNum();
                    bplan.setBattIndexInFbs(test_plan0);
                }
                if(list.get(i).getDischarge_state()==BattTestData.test_plan1){//放电延时
                    test_plan1+=list.get(i).getNum();
                    bplan.setPreset_time(test_plan1);
                }
                if(list.get(i).getDischarge_state()==BattTestData.test_plan2){//放电完成
                    test_plan2+=list.get(i).getNum();
                    bplan.setPreset_cap((float)test_plan2);
                }
                if(list.get(i).getDischarge_state()==BattTestData.test_plan3){//放电进行中
                    test_plan3+=list.get(i).getNum();
                    bplan.setPreset_monvol((float)test_plan3);
                }
                if(list.get(i).getDischarge_state()==BattTestData.test_plan4){//放电失败
                    test_plan4+=list.get(i).getNum();
                    bplan.setPreset_groupvol((float)test_plan4);
                }                
                
            }
            int sum=(int) (bplan.getBattIndexInFbs()+bplan.getPreset_time()+bplan.getPreset_cap()
                    +bplan.getPreset_monvol()+bplan.getPreset_groupvol());//总数
            model.setCode(1);
            model.setData(bplan);
            model.setMsg("查询成功!");
            //TODO perry
             model.setSum(sum);//总数
        }else{
            model.setCode(0);
            model.setMsg("查询失败!");
        }
        //System.out.println(model.getData());
        return model;
    }
        
/*    public static void main(String[] args) {
        BattDischarge_planService service = new BattDischarge_planService();
        User_inf uinf=new User_inf();
        uinf.setuId(1038);
        service.serchBattgroupinfo(uinf);
    }*/
}