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 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=mapper.add(plan)>0;
|
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=mapper.update(obj)>0;
|
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();
|
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();
|
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 ServiceModel judgePlan(Object obj){
|
List<BattDischarge_plan> listAll=(List<BattDischarge_plan>) obj;
|
List<BattDischarge_plan> list = new ArrayList();//存放满足计划设定条件的可行计划
|
if(listAll!=null&&listAll.size()>0){
|
for(int i=0;i<listAll.size();i++){
|
int flag=mapper.judgePlan(listAll.get(i));
|
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(Object obj){
|
List list=mapper.serchByCondition(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 ServiceModel serchBattgroupinfo(Object obj){
|
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(Object obj){
|
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 ServiceModel searchNums(Object obj) {
|
int nums= mapper.searchNums(obj);
|
//TODO perry
|
// model.setSum(nums);
|
return model;
|
}
|
//8.1放电计划管理中饼状图(放电计划统计查询)
|
/*BattIndexInFbs:未放电
|
*Preset_time:放电延时
|
*Preset_cap: 放电完成
|
*Preset_monvol:放电进行中
|
*Preset_groupvol:放电失败
|
*num: 总数*/
|
public ServiceModel searchState(Object obj){
|
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);
|
}
|
}
|