ifishSystem/src/main/java/com/ifish/serviceImpl/FactoryServiceImpl.java

298 lines
10 KiB
Java

package com.ifish.serviceImpl;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import com.ifish.dao.PayBillDao;
import com.ifish.entity.*;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.ifish.dao.DeviceDao;
import com.ifish.dao.FactoryListDao;
import com.ifish.dao.OperateRecordDao;
import com.ifish.enums.BooleanEnum;
import com.ifish.enums.RecordTypeEnum;
import com.ifish.enums.SortModeEnum;
import com.ifish.hibernate.Pagination;
import com.ifish.search.SearchFilter;
import com.ifish.service.FactoryService;
import com.ifish.util.IfishUtil;
/**
* @ClassName: FactoryServiceImpl
* @Description: TODO
* @author ggw
*
*/
@Service("factoryService")
@Transactional
public class FactoryServiceImpl implements FactoryService {
@Autowired
private DeviceDao deviceDao;
@Autowired
private FactoryListDao factoryListDao;
@Autowired
private OperateRecordDao operateRecordDao;
@Autowired
private PayBillDao payBillDao;
@Override
public Map<String, String> getFactoryByVenderCode(String venderCode) {
return factoryListDao.getFactoryByVenderCode(venderCode);
}
@Override
public int getDeviceCountByFactory(String factoryCode) {
Calendar calendar = Calendar.getInstance();
String year = String.valueOf(calendar.get(Calendar.YEAR));
String month = (calendar.get(Calendar.MONTH) < 10 ? "0" + calendar.get(Calendar.MONTH) : calendar.get(Calendar.MONTH)) + "";
int count = this.deviceDao.getNumber("SELECT count(1) from tbl_device td where td.is_charge = '0' and td.factory_code = '"+ factoryCode +"' and str_to_date(td.authorize_time,'%Y-%m')='" + year + "-" + month + "-00'");
return count;
}
@Override
public int getDeviceCountByFactory(String factoryCode, String billMonth) {
String year = billMonth.split("-")[0];
String month = billMonth.split("-")[1];
int count = this.deviceDao.getNumber("SELECT count(1) from tbl_device td where td.is_charge = '0' and td.factory_code = '"+ factoryCode +"' and str_to_date(td.authorize_time,'%Y-%m')='" + year + "-" + month + "-00'");
return count;
}
@Override
public FactoryList get(String id) {
return this.factoryListDao.get(id);
}
@Override
public void savePayBill(PayBill payBill) {
payBillDao.save(payBill);
}
@Override
public List<FactoryList> getFactoryList() {
List<Criterion> queryList = new ArrayList<Criterion>();
List<FactoryList> factoryLists = this.factoryListDao.findByProperty(queryList.toArray(new Criterion[queryList.size()]));
return factoryLists;
}
@Override
public void uploadExcel(String ip,String factoryCode,String brandCode,MultipartFile fileUpload) {
try {
InputStream is = fileUpload.getInputStream();
Workbook workbook = Workbook.getWorkbook(is);
Sheet sheet = workbook.getSheet(0);
//行
int rows = sheet.getRows();
List<String> macList = new ArrayList<String>();
for(int i=1;i < rows;i++){
Cell cell = sheet.getCell(0,i);
String value = cell.getContents();
macList.add(value);
}
List<String> hardTypeList = new ArrayList<String>();
for(int i=1;i < rows;i++){
Cell cell = sheet.getCell(1,i);
String value = cell.getContents();
hardTypeList.add(value);
}
//授权数量
int j = 0;
for (int i = 0; i < macList.size(); i++) {
String macAddress = macList.get(i);
Device device = this.deviceDao.findUniqueByProperty(Restrictions.eq("macAddress", macAddress));
if(device!=null){
VenderList venderList = new VenderList();
venderList.setBrandCode(brandCode);
FactoryList factoryList = new FactoryList();
factoryList.setFactoryCode(factoryCode);
device.setFactoryList(factoryList);
device.setVenderList(venderList);
//原来未授权
if(device.getIsBlacklist().equals(BooleanEnum.YES.getKey())){
j++;
device.setIsBlacklist(BooleanEnum.NO.getKey());
device.setAuthorizeTime(new Date());
}
device.getHardwareType().setHardwareType(hardTypeList.get(i));
device.setCreateDate(new Date());
this.deviceDao.update(device);
}
else{
j++;
device = new Device();
device.setMacAddress(macAddress);
VenderList venderList = new VenderList();
venderList.setBrandCode(brandCode);
FactoryList factoryList = new FactoryList();
factoryList.setFactoryCode(factoryCode);
device.setFactoryList(factoryList);
device.setVenderList(venderList);
device.setIsBlacklist(BooleanEnum.NO.getKey());
device.setAuthorizeTime(new Date());
device.getHardwareType().setHardwareType(hardTypeList.get(i));
device.setCreateDate(new Date());
device.setCreateTime(new Date());
this.deviceDao.save(device);
}
//分配
OperateRecord operateRecord = new OperateRecord();
operateRecord.setCreateTime(new Date());
operateRecord.setRecordIp(ip);
operateRecord.setRecordNumber(macList.size());
operateRecord.setRecordType(RecordTypeEnum.fenpei.getKey());
operateRecord.setFactoryCode(factoryCode);
this.operateRecordDao.save(operateRecord);
//授权
if(j>0){
OperateRecord operateRecord1 = new OperateRecord();
operateRecord1.setCreateTime(new Date());
operateRecord1.setRecordIp(ip);
operateRecord1.setRecordNumber(j);
operateRecord1.setRecordType(RecordTypeEnum.shouquan.getKey());
operateRecord1.setFactoryCode(factoryCode);
this.operateRecordDao.save(operateRecord1);
}
}
workbook.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public Pagination<Device> getDeviceByPage(SearchFilter searchFilter) {
if(searchFilter!=null){
//查询条件
List<Criterion> queryList = new ArrayList<Criterion>();
//排序条件
List<Order> orderList = new ArrayList<Order>();
//分页
Integer iDisplayStart = searchFilter.getiDisplayStart();
Integer iDisplayLength = searchFilter.getiDisplayLength();
//mac地址
String sSearch = searchFilter.getsSearch();
if(sSearch!=null && !sSearch.equals("")){
queryList.add(Restrictions.like("macAddress", "%"+sSearch+"%"));
}
//授权日期
String sSearch1 = searchFilter.getsSearch1();
if(sSearch1!=null && !sSearch1.equals("")){
queryList.add(Restrictions.sqlRestriction("'"+sSearch1+"'=date_format(authorize_time,'%Y-%m-%d')"));
}
//分配日期
String sSearch2 = searchFilter.getsSearch2();
if(sSearch2!=null && !sSearch2.equals("")){
queryList.add(Restrictions.eq("createDate", IfishUtil.StrToDate(sSearch2)));
}
//激活日期
String sSearch3 = searchFilter.getsSearch3();
if(sSearch3!=null && !sSearch3.equals("")){
queryList.add(Restrictions.sqlRestriction("'"+sSearch3+"'=date_format(first_activate,'%Y-%m-%d')"));
}
//分配编号
String sSearch4 = searchFilter.getsSearch4();
if(sSearch4!=null && !sSearch4.equals("")){
queryList.add(Restrictions.eq("createCode", sSearch4));
}
//是否黑名单过滤
String selectField = searchFilter.getSelectField();
if(selectField!=null && !selectField.equals("")){
queryList.add(Restrictions.eq("isBlacklist", selectField));
}
//鱼缸厂筛选
String selectField1 = searchFilter.getSelectField1();
if(selectField1!=null && !selectField1.equals("")){
queryList.add(Restrictions.eq("venderList.brandCode", selectField1));
}
//电子厂筛选
String selectField3 = searchFilter.getSelectField3();
if(selectField3!=null && !selectField3.equals("")){
queryList.add(Restrictions.eq("factoryList.factoryCode", selectField3));
}
//硬件型号筛选
String selectField2 = searchFilter.getSelectField2();
if(selectField2!=null && !selectField2.equals("")){
queryList.add(Restrictions.eq("hardwareType.hardwareType", selectField2));
}
//排序字段
String sortField = searchFilter.getSortField();
if(sortField!=null && !sortField.equals("")){
//排序方式
String sortMode = searchFilter.getSortMode();
//升序
if(sortMode!=null && SortModeEnum.ASC.getKey().equals(sortMode)){
orderList.add(Order.asc(sortField));
}
//降序
else{
orderList.add(Order.desc(sortField));
}
}
else{
orderList.add(Order.desc("createTime"));
}
return this.deviceDao.findByCriteria(iDisplayStart,iDisplayLength,orderList,queryList.toArray(new Criterion[queryList.size()]));
}
else{
return null;
}
}
@Override
public Pagination<Device> getDeviceByPageNew(SearchFilter searchFilter,Set<HardwareType> hardwareTypeSet) {
if(searchFilter!=null){
//查询条件
List<Criterion> queryList = new ArrayList<Criterion>();
//排序条件
List<Order> orderList = new ArrayList<Order>();
//分页
Integer iDisplayStart = searchFilter.getiDisplayStart();
Integer iDisplayLength = searchFilter.getiDisplayLength();
//授权日期
String sSearch1 = searchFilter.getsSearch1();
if(sSearch1!=null && !sSearch1.equals("")){
queryList.add(Restrictions.sqlRestriction("'"+sSearch1+"'=date_format(create_time,'%Y-%m-%d')"));
}
if (hardwareTypeSet != null && hardwareTypeSet.size() != 0) {
queryList.add(Restrictions.in("hardwareType",hardwareTypeSet));
}
String selectField1 = searchFilter.getSelectField1();
if(selectField1!=null && !selectField1.equals("")){
queryList.add(Restrictions.eq("isBlacklist", selectField1));
}
String selectField2 = searchFilter.getSelectField2();
if(selectField2!=null && !selectField2.equals("")){
if ("1".equals(selectField2)) {
queryList.add(Restrictions.sqlRestriction("create_code != ''"));
} else {
queryList.add(Restrictions.sqlRestriction("create_code = ''"));
}
}
orderList.add(Order.desc("createTime"));
return this.deviceDao.findByCriteria(iDisplayStart,iDisplayLength,orderList,queryList.toArray(new Criterion[queryList.size()]));
}
else{
return null;
}
}
}