Przeglądaj źródła

Merge branch 'dev' of http://192.168.1.220:10080/Amoy2/wlyy2.0 into dev

humingfen 5 lat temu
rodzic
commit
ce79fbde6d

+ 18 - 0
common/common-request-mapping/src/main/java/com/yihu/jw/rm/iot/IotRequestMapping.java

@ -13,6 +13,7 @@ public class IotRequestMapping {
     */
    public static class Common{
        public static final String company = api_iot_common + "/company";
        public static final String message = api_iot_common + "/message";
        public static final String system_dict = api_iot_common + "/systemDict";
        public static final String file_upload = api_iot_common + "/fileUpload";
        public static final String product = api_iot_common + "/product";
@ -104,6 +105,22 @@ public class IotRequestMapping {
    }
    /**
     * 消息模块
     */
    public static class Message {
        public static final String findAll = "findAll";
        public static final String markRead = "markRead";
        public static final String message_success_find = "message find success";
        public static final String message_success_update = "message mark success";
    }
    /**
     * 产品模块常量
     */
@ -283,6 +300,7 @@ public class IotRequestMapping {
        public static final String completePlanByPurchaseId = "completePlanByPurchaseId";
        public static final String queryQualityPlanPage = "queryQualityPlanPage";
        public static final String findById = "findById";
        public static final String findPlanTimeByOrderNo = "findPlanTimeByOrderNo";
        public static final String remindTimePlan = "remindTimePlan";
    }

+ 22 - 0
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/web/MixEnvelop.java

@ -7,6 +7,7 @@ import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 信封对象,封装REST接口的返回值内容。包括:
@ -43,9 +44,20 @@ public class MixEnvelop<T, J> extends Envelop {
    @ApiModelProperty(value = "列表内容")
    private List<T> detailModelList = new ArrayList<>(0);
    @ApiModelProperty(value = "列表内容")
    private Map<T,T> detailModelMap = new HashMap<>(0);
    @ApiModelProperty(value = "实体内容")
    private J obj = (J)new HashMap<>(0);
    public Map<T, T> getDetailModelMap() {
        return detailModelMap;
    }
    public void setDetailModelMap(Map<T, T> detailModelMap) {
        this.detailModelMap = detailModelMap;
    }
    public int getCurrPage() {
        return currPage;
    }
@ -133,6 +145,16 @@ public class MixEnvelop<T, J> extends Envelop {
        envelop.setTotalCount(count.intValue());
        return envelop;
    }
    public static MixEnvelop getSuccessMapWithPage(String message, Map detailModelMap, int page, int size, Long count) {
        MixEnvelop envelop = new MixEnvelop();
        envelop.setMessage(message);
        envelop.setPageSize(size);
        envelop.setDetailModelMap(detailModelMap);
        envelop.setCurrPage(page);
        envelop.setStatus(200);
        envelop.setTotalCount(count.intValue());
        return envelop;
    }
    public static MixEnvelop getError(String message, int errorCode) {
        MixEnvelop envelop = new MixEnvelop();

+ 3 - 3
svr/svr-iot-job/src/main/java/com/yihu/jw/job/wlw/DeviceQualityPlanJob.java

@ -19,13 +19,13 @@ public class DeviceQualityPlanJob implements Job {
    private DeviceQualityPlanService deviceQualityPlanService;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("START========PrescriptionStatusUpdateJob========");
        logger.info("START========DeviceQualityPlanJob========");
        try {
            deviceQualityPlanService.autoPush();
            logger.info("END========PrescriptionStatusUpdateJob========");
            logger.info("END========DeviceQualityPlanJob========");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("END===ERROE===PrescriptionStatusUpdateJob,message:"+e.getMessage());
            logger.error("END===ERROE===DeviceQualityPlanJob,message:"+e.getMessage());
        }
    }
}

+ 32 - 0
svr/svr-iot-job/src/main/java/com/yihu/jw/job/wlw/SimBalanceJob.java

@ -0,0 +1,32 @@
package com.yihu.jw.job.wlw;
import com.yihu.jw.service.channel.DeviceQualityPlanService;
import com.yihu.jw.service.channel.SimBalanceService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2020/5/9
 */
public class SimBalanceJob implements Job {
    private static final Logger logger = LoggerFactory.getLogger(SimBalanceJob.class);
    @Autowired
    private SimBalanceService simBalanceService;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("START========SimBalanceJob========");
        try {
            simBalanceService.autoPush();
            logger.info("END========SimBalanceJob========");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("END===ERROE===SimBalanceJob,message:"+e.getMessage());
        }
    }
}

+ 16 - 19
svr/svr-iot-job/src/main/java/com/yihu/jw/service/channel/DeviceQualityPlanService.java

@ -1,18 +1,17 @@
package com.yihu.jw.service.channel;
import com.yihu.iot.dao.device.IotDeviceQualityInspectionPlanDao;
import com.yihu.iot.dao.dict.IotSystemDictDao;
import com.yihu.iot.dao.message.IotMessageDao;
import com.yihu.iot.service.device.IotDeviceQualityInspectionPlanService;
import com.yihu.jw.entity.iot.device.IotDeviceQualityInspectionPlanDO;
import com.yihu.jw.entity.iot.dict.IotSystemDictDO;
import com.yihu.jw.entity.iot.message.IotMessageDO;
import com.yihu.jw.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.xml.ws.Action;
import java.util.Date;
import java.util.List;
/**
@ -24,37 +23,35 @@ import java.util.List;
@Transactional
public class DeviceQualityPlanService {
    private static Logger logger = LoggerFactory.getLogger(DeviceQualityPlanService.class);
    @Autowired
    private IotMessageDao iotMessageDao;
    private  IotMessageDao iotMessageDao;
    @Autowired
    private IotDeviceQualityInspectionPlanDao iotDeviceQualityInspectionPlanDao;
    private  IotDeviceQualityInspectionPlanService iotDeviceQualityInspectionPlanService;
    @Autowired
    private IotSystemDictDao iotSystemDictDao;
    private  IotSystemDictDao iotSystemDictDao;
    public void autoPush() {
    public  void autoPush() {
        logger.info("autoPush start");
        IotSystemDictDO qualityRemind = iotSystemDictDao.findByDictName("DEVICE_QUALITY_REMIND").get(0);
        int time = Integer.parseInt(qualityRemind.getTime());
        String time = qualityRemind.getTime();
        List<IotDeviceQualityInspectionPlanDO> listAll = iotDeviceQualityInspectionPlanDao.findListAll();
        List<IotDeviceQualityInspectionPlanDO> listAll = iotDeviceQualityInspectionPlanService.findListAll(time);
        listAll.forEach(one->{
            Date planTime = one.getPlanTime();
            Date remindTime = DateUtil.getPreDays(planTime, -time);
            String system = DateUtil.dateToStrShort(DateUtil.getNowDate());
            String remind = DateUtil.dateToStrShort(remindTime);
            if (system.equalsIgnoreCase(remind)){
                IotMessageDO iotMessageDO = new IotMessageDO();
                iotMessageDO.setTitle("设备质检提醒");
                iotMessageDO.setContent("订单:"+one.getOrderNo()+"将于:"+remind+"进行质检,请及时维护");
                iotMessageDO.setContent("订单:"+one.getOrderNo()+"将于:"+one.getPlanTime()+"进行质检,请及时维护");
                iotMessageDO.setReadMsg("0");
                iotMessageDO.setDel("0");
                iotMessageDO.setTitle("4");
                iotMessageDO.setType("4");
                iotMessageDao.save(iotMessageDO);
            }
        });
        logger.info("autoPush end");
    }
}

+ 59 - 0
svr/svr-iot-job/src/main/java/com/yihu/jw/service/channel/SimBalanceService.java

@ -0,0 +1,59 @@
package com.yihu.jw.service.channel;
import com.yihu.iot.dao.dict.IotSystemDictDao;
import com.yihu.iot.dao.message.IotMessageDao;
import com.yihu.iot.service.device.IotDeviceSimService;
import com.yihu.jw.entity.iot.device.IotDeviceSimDO;
import com.yihu.jw.entity.iot.dict.IotSystemDictDO;
import com.yihu.jw.entity.iot.message.IotMessageDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2020/5/9
 */
@Service
@Transactional
public class SimBalanceService {
    private static Logger logger = LoggerFactory.getLogger(SimBalanceService.class);
    @Autowired
    private IotSystemDictDao iotSystemDictDao;
    @Autowired
    private IotDeviceSimService iotDeviceSimService;
    @Autowired
    private IotMessageDao iotMessageDao;
    public  void autoPush() {
        logger.info("autoPush start");
        IotSystemDictDO simBalanceRemind = iotSystemDictDao.findByDictName("SIM_BALANCE_REMIND").get(0);
       List<IotDeviceSimDO> list  = iotDeviceSimService.findAllRemindSim(simBalanceRemind.getTime(),simBalanceRemind.getCode());
       list.forEach(one->{
           IotMessageDO iotMessageDO = new IotMessageDO();
           iotMessageDO.setTitle("SIM卡续费通知");
           iotMessageDO.setContent("您的:"+one.getDeviceName()+"设备绑定的SIM卡:"+one.getSim()+"余额不足"+simBalanceRemind.getCode()+"元,请及时充值,避免设备离线");
           iotMessageDO.setReadMsg("0");
           iotMessageDO.setDel("0");
           iotMessageDO.setType("1");
           iotMessageDao.save(iotMessageDO);
       });
    }
    public String getCron(){
        IotSystemDictDO simBalanceRemind = iotSystemDictDao.findByDictName("SIM_BALANCE_REMIND").get(0);
        return "0 58 16 "+simBalanceRemind.getTime()+" * ?";
    }
}

+ 65 - 0
svr/svr-iot/src/main/java/com/yihu/iot/controller/IotMessageService/IotMessageController.java

@ -0,0 +1,65 @@
package com.yihu.iot.controller.IotMessageService;
import com.yihu.iot.service.message.IotMessageService;
import com.yihu.jw.entity.iot.message.IotMessageDO;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import com.yihu.jw.rm.iot.IotRequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2020/5/9
 */
@RestController
@RequestMapping(IotRequestMapping.Common.message)
@Api(tags = "消息列表相关操作", description = "企业管理相关操作")
public class IotMessageController extends EnvelopRestEndpoint {
    @Autowired
    private IotMessageService iotMessageService;
    @GetMapping(value = IotRequestMapping.Message.findAll)
    @ApiOperation(value = "分页查找消息列表", notes = "分页查找消息列表")
    public MixEnvelop<IotMessageDO,IotMessageDO> findAll(@ApiParam(name = "page", value = "第几页", defaultValue = "")
                                                             @RequestParam(value = "page", required = false) Integer page,
                                                         @ApiParam(name = "size", value = "每页记录数", defaultValue = "")
                                                             @RequestParam(value = "size", required = false) Integer size){
        try {
            if(page == null|| page < 0){
                page = 1;
            }
            if(size == null){
                size = 10;
            }
            return iotMessageService.findAll(page,size);
        } catch (Exception e) {
            e.printStackTrace();
            return MixEnvelop.getError(e.getMessage());
        }
    }
    @GetMapping(value = IotRequestMapping.Message.markRead)
    @ApiOperation(value = "分页查找消息列表", notes = "分页查找消息列表")
    public MixEnvelop<IotMessageDO,IotMessageDO> markRead(@ApiParam(name = "id", value = "消息ID", defaultValue = "")
                                                         @RequestParam(value = "id", required = false) String id){
        try {
            return iotMessageService.markRead(id);
        } catch (Exception e) {
            e.printStackTrace();
            return MixEnvelop.getError(e.getMessage());
        }
    }
}

+ 27 - 0
svr/svr-iot/src/main/java/com/yihu/iot/controller/device/IotDeviceQualityController.java

@ -61,6 +61,33 @@ public class IotDeviceQualityController extends EnvelopRestEndpoint {
        }
    }
    @GetMapping(value = IotRequestMapping.DeviceQuality.findPlanTimeByOrderNo)
    @ApiOperation(value = "根据订单查找设备质检计划时间列表", notes = "根据订单查找设备质检计划时间列表")
    public MixEnvelop<IotDeviceQualityInspectionPlanVO, IotDeviceQualityInspectionPlanVO> findPlanTimeByOrderNo(@ApiParam(name = "orderNo", value = "orderNo")
                                                                                                     @RequestParam(value = "orderNo", required = true
                                                                                                     ) String orderNo,
                                                                                                                @ApiParam(name = "page", value = "第几页", defaultValue = "")
                                                                                                     @RequestParam(value = "page", required = false) Integer page,
                                                                                                                @ApiParam(name = "size", value = "每页记录数", defaultValue = "")
                                                                                                     @RequestParam(value = "size", required = false) Integer size
    ) {
        try {
            if(page == null|| page < 0){
                page = 1;
            }
            if(size == null){
                size = 10;
            }
            MixEnvelop<IotDeviceQualityInspectionPlanVO, IotDeviceQualityInspectionPlanVO> list = iotDeviceQualityInspectionPlanService.findPlanTimeByOrderNo(orderNo, page, size);
            return MixEnvelop.getSuccess(IotRequestMapping.Common.message_success_find,list);
        } catch (Exception e) {
            e.printStackTrace();
            return MixEnvelop.getError(e.getMessage());
        }
    }
    @GetMapping(value = IotRequestMapping.DeviceQuality.queryQualityPlanPage)
    @ApiOperation(value = "分页获取设备质检计划", notes = "分页获取设备质检计划")

+ 2 - 2
svr/svr-iot/src/main/java/com/yihu/iot/dao/device/IotDeviceQualityInspectionPlanDao.java

@ -31,6 +31,6 @@ public interface IotDeviceQualityInspectionPlanDao extends PagingAndSortingRepos
    @Query(value = "SELECT a.* from iot_device_quality_inspection_plan a WHERE a.purchase_id = ?1 and a.status=?2 and a.del=1 ORDER BY a.plan_time asc limit 1",nativeQuery = true)
    IotDeviceQualityInspectionPlanDO findFirstByPurchaseId(String purchaseId,String status);
    @Query("from IotDeviceQualityInspectionPlanDO w where   w.del=1 ORDER BY w.planTime desc")
    List<IotDeviceQualityInspectionPlanDO> findListAll();
    List<IotDeviceQualityInspectionPlanDO> findByOrderNo(String orderNo);
}

+ 44 - 10
svr/svr-iot/src/main/java/com/yihu/iot/service/device/IotDeviceQualityInspectionPlanService.java

@ -16,7 +16,9 @@ import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.restmodel.iot.device.IotDeviceQualityInspectionPlanVO;
import com.yihu.jw.rm.iot.IotRequestMapping;
import com.yihu.jw.util.date.DateUtil;
import com.yihu.jw.util.http.HttpClientUtil;
import com.yihu.mysql.query.BaseJpaService;
import com.yihu.utils.network.HttpUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
@ -53,6 +55,8 @@ public class IotDeviceQualityInspectionPlanService extends BaseJpaService<IotDev
    @Autowired
    private IotSystemDictDao iotSystemDictDao;
    private final static String jobUrl = "http://localhost:10031/job/reStartById?taskId=data_device_quality_plan_job";
    /**
     * 新增
     * @param iotDeviceQualityInspectionPlan
@ -237,35 +241,28 @@ public class IotDeviceQualityInspectionPlanService extends BaseJpaService<IotDev
     * @return
     */
    public MixEnvelop<IotDeviceQualityInspectionPlanVO, IotDeviceQualityInspectionPlanVO> queryPage(String status, String orderNo, String startTime, String endTime, Integer page, Integer size){
        StringBuffer sql = new StringBuffer("SELECT c.* from iot_device_quality_inspection_plan c  WHERE c.del=1 ");
        StringBuffer sqlCount = new StringBuffer("SELECT COUNT(c.id) count from iot_device_quality_inspection_plan c WHERE c.del=1 ");
        StringBuffer sql = new StringBuffer("SELECT  c.* from iot_device_quality_inspection_plan c  WHERE c.del=1 ");
        List<Object> args = new ArrayList<>();
        if(StringUtils.isNotBlank(status)){
            sql.append(" and c.status=? ");
            sqlCount.append(" and c.status='").append(status).append("' ");
            args.add(status);
        }
        if(StringUtils.isNotBlank(orderNo)){
            sql.append("and c.order_no like'%").append(orderNo).append("%' ");
            sqlCount.append(" and c.order_no like'%").append(orderNo).append("%' ");
        }
        if(StringUtils.isNotBlank(startTime)){
            sql.append(" and c.plan_time>=? ");
            sqlCount.append(" and c.plan_time>='").append(startTime).append("' ");
            args.add(startTime);
        }
        if(StringUtils.isNotBlank(endTime)){
            sql.append(" and c.plan_time<=? ");
            sqlCount.append(" and c.plan_time<='").append(endTime).append("' ");
            args.add(endTime);
        }
        sql.append("order by c.update_time desc limit ").append((page-1)*size).append(",").append(size);
        sql.append(" GROUP BY c.order_no ORDER BY c.plan_time DESC limit ").append((page-1)*size).append(",").append(size);
        List<IotDeviceQualityInspectionPlanDO> list = jdbcTempalte.query(sql.toString(),args.toArray(),new BeanPropertyRowMapper(IotDeviceQualityInspectionPlanDO.class));
        List<Map<String,Object>> countList = jdbcTempalte.queryForList(sqlCount.toString());
        long count = Long.valueOf(countList.get(0).get("count").toString());
        long count = list.size();
        //DO转VO
        List<IotDeviceQualityInspectionPlanVO> qualityInspectionPlanVOList = transforList(list);
@ -279,10 +276,47 @@ public class IotDeviceQualityInspectionPlanService extends BaseJpaService<IotDev
     */
    public void remindTimePlan(String time) {
        IotSystemDictDO qualityRemind= iotSystemDictDao.findByDictName("DEVICE_QUALITY_REMIND").get(0);
        qualityRemind.setTime(time);
        qualityRemind.setDel(1);
        iotSystemDictDao.save(qualityRemind);
    }
    /**
     * 查询所有需要提醒的质检信息
     * @param time
     * @return
     */
    public List<IotDeviceQualityInspectionPlanDO>  findListAll(String time){
        StringBuffer sql = new StringBuffer("SELECT c.* FROM  iot_device_quality_inspection_plan c WHERE del=1");
        List<Object> args = new ArrayList<>();
        if (StringUtils.isNotBlank(time)){
            sql.append("  and DATEDIFF(c.plan_time,CURRENT_TIMESTAMP)=?");
            args.add(time);
        }
        List<IotDeviceQualityInspectionPlanDO> list = jdbcTempalte.query(sql.toString(),args.toArray(),new BeanPropertyRowMapper(IotDeviceQualityInspectionPlanDO.class));
        return list;
    }
    /**
     * 根据订单编号查找所有计划时间
     * @param orderNo
     * @return
     */
    public MixEnvelop<IotDeviceQualityInspectionPlanVO, IotDeviceQualityInspectionPlanVO>  findPlanTimeByOrderNo(String orderNo,Integer page,Integer size) {
        List<IotDeviceQualityInspectionPlanDO> listOrderNo = iotDeviceQualityInspectionPlanDao.findByOrderNo(orderNo);
        List<IotDeviceQualityInspectionPlanVO> list = transforList(listOrderNo);
        long count = list.size();
        return MixEnvelop.getSuccessListWithPage(IotRequestMapping.Common.message_success_find_functions,list,page,size,count);
    }
}

+ 36 - 0
svr/svr-iot/src/main/java/com/yihu/iot/service/device/IotDeviceSimService.java

@ -7,6 +7,7 @@ import com.yihu.iot.dao.device.IotPatientDeviceDao;
import com.yihu.iot.dao.dict.IotSystemDictDao;
import com.yihu.jw.entity.iot.company.IotCompanyDO;
import com.yihu.jw.entity.iot.device.IotDeviceDO;
import com.yihu.jw.entity.iot.device.IotDeviceQualityInspectionPlanDO;
import com.yihu.jw.entity.iot.device.IotDeviceSimDO;
import com.yihu.jw.entity.iot.device.IotPatientDeviceDO;
import com.yihu.jw.entity.iot.dict.IotSystemDictDO;
@ -15,6 +16,7 @@ import com.yihu.jw.restmodel.iot.company.IotCompanyVO;
import com.yihu.jw.restmodel.iot.device.IotOrderVO;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.rm.iot.IotRequestMapping;
import com.yihu.jw.util.http.HttpClientUtil;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -52,6 +54,11 @@ public class IotDeviceSimService  extends BaseJpaService<IotDeviceSimDO, IotDevi
    @Autowired
    private IotSystemDictDao iotSystemDictDao;
    @Autowired
    private HttpClientUtil httpClientUtil;
    private final static String jobUrl = "http://localhost:10031/job/reStartById?taskId=data_sim_Balance_remind_job";
    /**
     * 增加SIM卡管理
     * @param advancePayment
@ -171,5 +178,34 @@ public class IotDeviceSimService  extends BaseJpaService<IotDeviceSimDO, IotDevi
        sim.setStatus(Integer.parseInt(status));
        sim.setTime(time);
        iotSystemDictDao.save(sim);
        //触发JOB工程
        httpClientUtil.get(jobUrl,"UTF-8");
    }
    /**
     * 获取所有需要提醒的SIM卡
     * @param time
     * @param code
     * @return
     */
    public List<IotDeviceSimDO> findAllRemindSim(String time, String code) {
        StringBuffer sql = new StringBuffer("SELECT a.* FROM iot_device_sim a WHERE 1=1");
        List<Object> args = new ArrayList<>();
        if (StringUtils.isNotBlank(time)){
            sql.append("  AND DAY(CURRENT_TIMESTAMP)=?");
            args.add(time);
        }
        if(StringUtils.isNotBlank(code)){
            sql.append(" and a.remaining_balance <=? ");
            args.add(code);
        }
        List<IotDeviceSimDO> list = jdbcTemplate.query(sql.toString(),args.toArray(),new BeanPropertyRowMapper(IotDeviceSimDO.class));
        return list;
    }
}

+ 83 - 0
svr/svr-iot/src/main/java/com/yihu/iot/service/message/IotMessageService.java

@ -0,0 +1,83 @@
package com.yihu.iot.service.message;
import com.alibaba.fastjson.JSONArray;
import com.yihu.iot.dao.message.IotMessageDao;
import com.yihu.jw.entity.iot.company.IotCompanyDO;
import com.yihu.jw.entity.iot.device.IotDeviceQualityInspectionPlanDO;
import com.yihu.jw.entity.iot.message.IotMessageDO;
import com.yihu.jw.restmodel.iot.company.IotCompanyVO;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.rm.iot.IotRequestMapping;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2020/5/9
 */
@Service
@Transactional
public class IotMessageService  extends BaseJpaService<IotMessageDO, IotMessageDao> {
    @Autowired
    private IotMessageDao iotMessageDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * 分页查找
     * @param page
     * @param size
     * @return
     * @throws ParseException
     */
    public MixEnvelop<IotMessageDO, IotMessageDO> findAll(Integer page, Integer size) throws ParseException {
        String sqlunRead="SELECT * FROM iot_message WHERE read_msg=0 ORDER BY create_time DESC;";
        String sqlRead="SELECT * FROM iot_message WHERE read_msg=1 ORDER BY create_time DESC;";
        List<IotMessageDO> unlist = jdbcTemplate.query(sqlunRead, new BeanPropertyRowMapper<>(IotMessageDO.class));
        List<IotMessageDO> list = jdbcTemplate.query(sqlRead, new BeanPropertyRowMapper<>(IotMessageDO.class));
        Map<String, List> listMap = new HashMap<>();
        listMap.put("unread",unlist);
        listMap.put("read",list);
        long size2 = unlist.size();
        long size1 = list.size();
        long count =size1+size2;
        return MixEnvelop.getSuccessMapWithPage(IotRequestMapping.Company.message_success_find_functions,listMap, page, size,count);
    }
    /**
     * 根据ID 设置消息已读
     * @param id
     * @return
     */
    public MixEnvelop<IotMessageDO, IotMessageDO> markRead(String id) {
        IotMessageDO messageDO = iotMessageDao.findOne(id);
        if(messageDO.getReadMsg().equalsIgnoreCase("1")){
            return MixEnvelop.getError("消息已标记已读,请勿重复操作");
        }
        messageDO.setReadMsg("1");
        iotMessageDao.save(messageDO);
        return MixEnvelop.getSuccess(IotRequestMapping.Message.message_success_update);
    }
}