设备组添加分配设备功能,移除设备功能
diff --git a/src/main/java/com/supwisdom/dlpay/restaurant/controller/DeviceGroupController.java b/src/main/java/com/supwisdom/dlpay/restaurant/controller/DeviceGroupController.java
index c75b27f..2c222d3 100644
--- a/src/main/java/com/supwisdom/dlpay/restaurant/controller/DeviceGroupController.java
+++ b/src/main/java/com/supwisdom/dlpay/restaurant/controller/DeviceGroupController.java
@@ -2,6 +2,8 @@
 
 
 import com.supwisdom.dlpay.api.bean.JsonResult;
+import com.supwisdom.dlpay.exception.WebCheckException;
+import com.supwisdom.dlpay.framework.util.StringUtil;
 import com.supwisdom.dlpay.restaurant.bean.DeviceSearchBean;
 import com.supwisdom.dlpay.restaurant.domain.TDevice;
 import com.supwisdom.dlpay.restaurant.domain.TDeviceGroup;
@@ -17,6 +19,7 @@
 import org.springframework.ui.ModelMap;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.ArrayList;
 import java.util.List;
 
 @Controller
@@ -41,6 +44,9 @@
                                          @RequestParam("limit") Integer pageSize,
                                          @RequestParam(value = "searchkey", required = false) Integer searchKey) {
         try {
+            if(searchKey==-1){
+                return new PageResult<>();
+            }
             if (null == pageNo || pageNo < 1) pageNo = WebConstant.PAGENO_DEFAULT;
             if (null == pageSize || pageSize < 1) pageSize = WebConstant.PAGESIZE_DEFAULT;
             DeviceSearchBean searchBean = new DeviceSearchBean();
@@ -76,6 +82,7 @@
         return "restaurant/devicegroup/form";
     }
 
+
     @PostMapping("/add")
     @PreAuthorize("hasPermission('/devicegroup/add','')")
     @ResponseBody
@@ -94,4 +101,56 @@
         return groupService.delete(id);
     }
 
+    @PostMapping("/removedevice")
+    @PreAuthorize("hasPermission('/devicegroup/delete','')")
+    @ResponseBody
+    public JsonResult removedevice(@RequestParam("id") Integer id) {
+        return deviceService.deleteGroup(id);
+    }
+
+
+    @GetMapping("/loadadddevice")
+    @PreAuthorize("hasPermission('/devicegroup/loadadd','')")
+    public String loadadddevice(Model model) {
+
+        return "restaurant/devicegroup/deviceform";
+    }
+
+
+    @GetMapping("/load4devicetrans")
+    @PreAuthorize("hasPermission('/devicegroup/loadadd','')")
+    @ResponseBody
+    public PageResult<TDevice> searchBindDevparaDevices(@RequestParam(value = "searchkey", required = false) String deivcename) {
+        try {
+
+            return deviceService.getDeviceByDevicename( deivcename);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return new PageResult<>(99, "系统查询错误");
+        }
+    }
+
+
+    @PostMapping("/addbinddevice")
+    @PreAuthorize("hasPermission('/devicegroup/loadadd','')")
+    @ResponseBody
+    public JsonResult addDevparaBind(@RequestParam("groupid") Integer groupid,
+                                     @RequestParam("deviceid[]") List<Integer> deviceIds) {
+        try {
+            if(null==groupid || StringUtil.isEmpty(deviceIds)){
+                return JsonResult.error("参数传递错误");
+            }
+
+            if (deviceService.updateDeviceGroup(groupid, deviceIds)) {
+                return JsonResult.ok("绑定设备组成功");
+            } else {
+                return JsonResult.error("绑定设备组失败");
+            }
+        } catch (WebCheckException ex) {
+            return JsonResult.error(ex.getMessage());
+        } catch (Exception e) {
+            e.printStackTrace();
+            return JsonResult.error("系统处理异常").put("exception", e);
+        }
+    }
 }
diff --git a/src/main/java/com/supwisdom/dlpay/restaurant/dao/DeviceDao.java b/src/main/java/com/supwisdom/dlpay/restaurant/dao/DeviceDao.java
index 8527d61..7362f2c 100644
--- a/src/main/java/com/supwisdom/dlpay/restaurant/dao/DeviceDao.java
+++ b/src/main/java/com/supwisdom/dlpay/restaurant/dao/DeviceDao.java
@@ -10,6 +10,8 @@
 import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;
 
+import java.util.List;
+
 @Repository
 public interface DeviceDao extends JpaSpecificationExecutor<TDevice>,JpaRepository<TDevice, Integer> {
 
@@ -17,6 +19,9 @@
 
     Page<TDevice> findAllByDevgroupid(Integer devgroupid, Pageable pageable);
 
+    List<TDevice> findAllByDevgroupid(Integer devgroupid);
+
+    List<TDevice> findAllByDevicenameContainingOrderById(String name);
 
     @Query("select count(*) from TDevice t where t.devgroupid =:id and t.state=1")
     Integer countByDevgroupid(@Param("id") Integer Devgroupid);
diff --git a/src/main/java/com/supwisdom/dlpay/restaurant/service/DeviceService.java b/src/main/java/com/supwisdom/dlpay/restaurant/service/DeviceService.java
index f7223da..b3eb60f 100644
--- a/src/main/java/com/supwisdom/dlpay/restaurant/service/DeviceService.java
+++ b/src/main/java/com/supwisdom/dlpay/restaurant/service/DeviceService.java
@@ -1,6 +1,7 @@
 package com.supwisdom.dlpay.restaurant.service;
 
 import com.supwisdom.dlpay.api.bean.JsonResult;
+import com.supwisdom.dlpay.exception.WebCheckException;
 import com.supwisdom.dlpay.restaurant.bean.DeviceSearchBean;
 import com.supwisdom.dlpay.restaurant.domain.TDevice;
 import com.supwisdom.dlpay.framework.util.PageResult;
@@ -16,11 +17,15 @@
     PageResult<TDevice> getDeviceByKey(DeviceSearchBean param);
 
     @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class,readOnly = true)
+    PageResult<TDevice> getDeviceByDevicename(String devicename);
+
+
+    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class,readOnly = true)
     PageResult<TDevice> getDeviceByParam(DeviceSearchBean param);
 
 
     @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class,readOnly = true)
-    PageResult<TDevice> getDeviceByGroupId(DeviceSearchBean param);
+    List<TDevice> getDeviceByGroupId(Integer groupid);
 
 
     @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class,readOnly = true)
@@ -30,9 +35,17 @@
     JsonResult saveDevice(TDevice device);
 
     @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
+    JsonResult deleteGroup(Integer id);
+
+
+    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
     JsonResult updateDevice(TDevice device);
 
 
+    @Transactional(rollbackFor = Exception.class)
+    boolean updateDeviceGroup(Integer groupid,List<Integer> deviceIds) throws WebCheckException;;
+
+
     @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
     boolean updateState(Integer id,Integer state);
 
diff --git a/src/main/java/com/supwisdom/dlpay/restaurant/service/impl/DeviceServiceImpl.java b/src/main/java/com/supwisdom/dlpay/restaurant/service/impl/DeviceServiceImpl.java
index 25a17d5..51413cb 100644
--- a/src/main/java/com/supwisdom/dlpay/restaurant/service/impl/DeviceServiceImpl.java
+++ b/src/main/java/com/supwisdom/dlpay/restaurant/service/impl/DeviceServiceImpl.java
@@ -4,8 +4,10 @@
 import com.supwisdom.dlpay.exception.WebCheckException;
 import com.supwisdom.dlpay.restaurant.bean.DeviceSearchBean;
 import com.supwisdom.dlpay.restaurant.dao.DeviceDao;
+import com.supwisdom.dlpay.restaurant.dao.DeviceGroupDao;
 import com.supwisdom.dlpay.restaurant.dao.ShopDeviceDao;
 import com.supwisdom.dlpay.restaurant.domain.TDevice;
+import com.supwisdom.dlpay.restaurant.domain.TDeviceGroup;
 import com.supwisdom.dlpay.restaurant.domain.TShopDevice;
 import com.supwisdom.dlpay.restaurant.service.DeviceService;
 import com.supwisdom.dlpay.framework.dao.ShopSettlementDao;
@@ -44,6 +46,8 @@
     private ShopDeviceDao shopDeviceDao;
     @Autowired
     private ShopSettlementDao shopSettlementDao;
+    @Autowired
+    private DeviceGroupDao deviceGroupDao;
     @PersistenceContext
     private EntityManager entityManager;
 
@@ -87,13 +91,18 @@
     }
 
     @Override
-    public PageResult<TDevice> getDeviceByGroupId(DeviceSearchBean param) {
-        Pageable pageable = PageRequest.of(param.getPageNo() - 1, param.getPageSize()
-                , Sort.by("id"));
-        if (null != param.getDevgroupid()) {
-            return new PageResult<>(deviceDao.findAllByDevgroupid(param.getDevgroupid(), pageable));
+    public PageResult<TDevice> getDeviceByDevicename(String devicename) {
+
+        if(devicename==null){
+            devicename="";
         }
-        return new PageResult<>(deviceDao.findAll(pageable));
+        return new PageResult<>(
+                deviceDao.findAllByDevicenameContainingOrderById(devicename));
+    }
+
+    @Override
+    public List<TDevice> getDeviceByGroupId(Integer groupid) {
+            return deviceDao.findAllByDevgroupid(groupid);
     }
 
 
@@ -103,6 +112,16 @@
     }
 
     @Override
+    public JsonResult deleteGroup(Integer id) {
+
+        TDevice device=deviceDao.findTDeviceById(id);
+        device.setDevgroupid(null);
+
+        deviceDao.save(device);
+        return JsonResult.ok("移除设备成功");
+    }
+
+    @Override
     public JsonResult saveDevice(TDevice device) {
         if(shopSettlementDao.countByShopid(device.getShopid())==0){
             return JsonResult.error("商户不存在");
@@ -141,6 +160,23 @@
     }
 
     @Override
+    public boolean updateDeviceGroup(Integer groupid, List<Integer> deviceIds) throws WebCheckException {
+        TDeviceGroup group = deviceGroupDao.findById(groupid).get();
+        if (null == group) {
+            throw new WebCheckException("所选设备组不存在");
+        }
+        for (Integer id : deviceIds) {
+            TDevice device = deviceDao.findTDeviceById(id);
+            if (null == device) {
+                throw new WebCheckException("终端编号为[" + id + "]的设备不存在");
+            }
+            device.setDevgroupid(groupid);
+            saveDevice(device);
+        }
+        return true;
+    }
+
+    @Override
     public boolean updateState(Integer id, Integer state) {
         Optional<TDevice> temp = deviceDao.findById(id);
         if (!temp.isPresent()) {
diff --git a/src/main/resources/templates/restaurant/devicegroup/deviceform.html b/src/main/resources/templates/restaurant/devicegroup/deviceform.html
new file mode 100644
index 0000000..4ceb6d4
--- /dev/null
+++ b/src/main/resources/templates/restaurant/devicegroup/deviceform.html
@@ -0,0 +1,98 @@
+<div id="devicegroup-form" lay-filter="devicegroup-form-filter" class="layui-form model-form"
+     style="padding: 30px 25px 10px 25px;">
+    <div class="layui-form-item">
+        搜索:
+        <input id="deviceform-search-value" class="layui-input search-input" type="text" placeholder="输入设备名称"/>&emsp;
+        <button id="deviceform-btn-search" class="layui-btn icon-btn" data-type="search"><i class="layui-icon">&#xe615;</i>搜索
+        </button>
+
+        <table class="layui-table" id="devicegroup-form-table" lay-filter="devicegroup-form-table"></table>
+    </div>
+
+    <div class="layui-form-item model-form-footer">
+        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
+        <button class="layui-btn" lay-filter="deviceform-form-submit" lay-submit id="submitbtn">保存</button>
+    </div>
+</div>
+
+<script>
+
+    layui.use(['form', 'table', 'layer', 'admin', 'element'], function () {
+        let form = layui.form;
+        let table = layui.table;
+        let admin = layui.admin;
+
+        // 渲染表格
+        table.render({
+            elem: '#devicegroup-form-table',
+            url: '[[@{/devicegroup/load4devicetrans}]]',
+            size: 'sm',
+            height: 350,
+            page: false,
+            cols: [
+                [
+                    {type: 'checkbox', style: "#devparabind-form-css", fixed: 'left'},
+                    {field: 'id', title: '设备编号', align: 'center'},
+                    {field: 'devicename', title: '设备名称', align: 'center'}
+                ]
+            ]
+        });
+        // 搜索按钮点击事件
+        $('#deviceform-btn-search').click(function () {
+            let key = $('#deviceform-search-value').val().trim();
+            table.reload('devicegroup-form-table', {where: {searchkey: key}});
+        });
+
+        form.on('submit(deviceform-form-submit)', function (el) {
+            let groupid = admin.getTempData("t_groupid");
+            var checkStatus = table.checkStatus('devicegroup-form-table');
+            var data = checkStatus.data;
+            var deviceids = [];
+            if ("" == groupid) {
+                layer.msg("请选择设备组", {icon: 2, time: 1500});
+                return;
+            }
+            for (var i = 0; i < data.length; i++) {
+                deviceids.push(data[i].id);
+            }
+            if (deviceids.length < 1) {
+                layer.msg("请选择设备", {icon: 2, time: 1500});
+                return;
+            }
+            console.log(groupid);
+            console.log(deviceids);
+            layer.load(2);
+            var token = $("meta[name='_csrf_token']").attr("value");
+            $.ajax({
+                type: "POST",
+                dataType: "json",
+                url: '[[@{/devicegroup/addbinddevice}]]',
+                data: {
+                    "groupid": groupid,
+                    "deviceid[]": deviceids,
+                    "_csrf": token
+                },
+                success: function (result) {
+                    layer.closeAll('loading');
+                    if (result.code == 200) {
+                        layer.msg(result.msg, {icon: 1});
+                        table.reload('devparabindformTable');
+                        admin.finishPopupCenter();
+                    } else if (result.code == 401) {
+                        layer.msg(result.msg, {icon: 2, time: 1500}, function () {
+                            location.replace('[[@{/login}]]');
+                        }, 1000);
+                        return;
+                    } else {
+                        console.log('err:' + result.code);
+                        layer.msg(result.msg, {icon: 2});
+                    }
+                },
+                error: function (err) {
+                    admin.errorBack(err);
+                }
+            });
+        });
+
+    });
+</script>
diff --git a/src/main/resources/templates/restaurant/devicegroup/index.html b/src/main/resources/templates/restaurant/devicegroup/index.html
index 775f629..a26b228 100644
--- a/src/main/resources/templates/restaurant/devicegroup/index.html
+++ b/src/main/resources/templates/restaurant/devicegroup/index.html
@@ -25,12 +25,12 @@
 
     <div class="layui-card-body ">
         <div class="layui-form toolbar">
-            <input id="devicegroup-search-value" class="layui-input search-input" type="hidden"/>&emsp;
-            <!--    <button id="btn-search" class="layui-btn icon-btn" data-type="search"><i class="layui-icon">&#xe615;</i>搜索
-                </button>-->
-
+            <!-- <input id="devicegroup-search-value" class="layui-input search-input" type="hidden"/>&emsp;
+            -    <button id="btn-search" class="layui-btn icon-btn" data-type="search"><i class="layui-icon">&#xe615;</i>搜索
+                 </button>-->
+                <button id="devicegroup-btn-setdevice" class="layui-btn icon-btn" data-type="add"><i class="layui-icon"></i>分配设备</button>
         </div>
-        <table class="layui-table" id="devicegroup-table" lay-filter="table"></table>
+        <table class="layui-table" id="devicegroup-table" lay-filter="devicegroup-table"></table>
     </div>
 </div>
 
@@ -39,7 +39,8 @@
            {{d.state=='1'?'checked':''}} />
 </script>
 <script>
-
+    var groupid=null;
+    var groupname=null;
     layui.use(['form', 'table', 'layer', 'admin', 'element'], function () {
         let form = layui.form;
         let table = layui.table;
@@ -51,15 +52,21 @@
         table.render({
             elem: '#devicegroup-table',
             url: '[[@{/devicegroup/listDevice}]]',
+            where:{searchkey:-1},
             page: true,
             cols: [
                 [
-                    {field: 'id', title: '终端编号',width: 100, sort: true},
-                    {field: 'devicename', title: '设备名称',width: 160, sort: true},
+                    {field: 'id', title: '终端编号',width: 90, sort: true},
+                    {field: 'devicename', title: '设备名称',width: 120, sort: true},
                     {field: 'devphyid', sort: true, width: 120, title: '设备物理id'},
                     {field: 'shopid', sort: true, width: 100, title: '商户id'},
                     {field: 'factoryid', sort: true, width: 100, title: '设备厂商'},
-                    {field: 'state', title: '状态', sort: true, width: 100, templet: '#devicegroup-dev-tpl-state'},
+                    {field: 'state', title: '状态', sort: true, width: 90, templet: '#devicegroup-dev-tpl-state'},
+                    {
+                        field: 'id', align: 'center',width:100, title: '操作', fixed: 'right', templet: function (item) {
+                            return ' <a class="layui-btn  layui-btn-xs" lay-event="devicegroup-remove"><i class="layui-icon layui-icon-edit"></i>移除</a> ';
+                        }
+                    }
                 ]
             ]
         });
@@ -71,6 +78,9 @@
         $('#devicegroup-btn-add').click(function () {
             showModel();
         });
+        $('#devicegroup-btn-setdevice').click(function () {
+            showDeviceModel();
+        });
 
         let showModel = function (data) {
             let title = data ? '修改设备组' : '添加设备组';
@@ -84,19 +94,40 @@
             });
         };
 
+        let showDeviceModel = function (data) {
+            if(groupid==null){
+                layer.msg('请先选择设备组', {icon: 2});
+                return;
+            }
+            let title = groupname;
+            admin.putTempData('t_groupname', groupname);
+            admin.putTempData('t_groupid', groupid);
+            admin.popupCenter({
+                title: title,
+                path: '[[@{/devicegroup/loadadddevice}]]',
+                finish: function () {
+                    table.reload('devicegroup-table', {where: {searchkey:groupid}, page: {curr: 1}});
+                }
+            });
+        };
+
+
 
         // 工具条点击事件
-        table.on('tool(table)', function (obj) {
+        table.on('tool(devicegroup-table)', function (obj) {
             let data = obj.data;
             let layEvent = obj.event;
             console.log(data);
+            if (layEvent === 'devicegroup-remove') {
+                showRemove(data);
+            }
         });
-        let showDelete = function (data) {
-            layer.confirm('确定要删除吗?', function (i) {
+        let showRemove = function (data) {
+            layer.confirm('确定要移除该设备吗?', function (i) {
                 layer.close(i);
                 layer.load(2);
                 let token = $("meta[name='_csrf_token']").attr("value");
-                admin.go('[[@{/device/delete}]]', {
+                admin.go('[[@{/devicegroup/removedevice}]]', {
                     id: data.id,
                     _csrf: token
                 }, function (data) {
@@ -111,7 +142,7 @@
                     } else {
                         layer.msg(data.msg, {icon: 2});
                     }
-                    table.reload('devicegroup-table', {});
+                    table.reload('devicegroup-table', {where: {searchkey:groupid}, page: {curr: 1}});
                 }, function (ret) {
                     console.log(ret);
                     layer.closeAll('loading');
@@ -122,6 +153,8 @@
 
         function OnGrpClick(e, treeId, treeNode) {
             console.log(treeNode.id);
+            groupid=treeNode.id;
+            groupname=treeNode.name;
             table.reload('devicegroup-table', {where: {searchkey: treeNode.id}, page: {curr: 1}});
 
         }
diff --git a/src/main/resources/templates/restaurant/discountrule/rule.html b/src/main/resources/templates/restaurant/discountrule/rule.html
index 7921d16..dbfb625 100644
--- a/src/main/resources/templates/restaurant/discountrule/rule.html
+++ b/src/main/resources/templates/restaurant/discountrule/rule.html
@@ -50,7 +50,7 @@
             cols: [
                 [
                     {field: 'rulename', title: '餐补名称', align: 'center', fixed: 'left' },
-                    {field: 'timeperiod', title: '优惠时间段',  align: 'center'},
+                    {field: 'timeperiod',width:100, title: '优惠时间段',  align: 'center'},
                     {
                         field: 'ruletype', title: '餐补类型', align: 'center', width: 120,  sort: true, templet: function (d) {
                             if ('quota' == d.ruletype) {
diff --git a/src/main/resources/templates/restaurant/discountrule/rulecheck.html b/src/main/resources/templates/restaurant/discountrule/rulecheck.html
index 8449f57..df4bc99 100644
--- a/src/main/resources/templates/restaurant/discountrule/rulecheck.html
+++ b/src/main/resources/templates/restaurant/discountrule/rulecheck.html
@@ -51,7 +51,7 @@
                 cols: [
                     [
                         {field: 'rulename', title: '餐补名称', align: 'center', fixed: 'left'},
-                        {field: 'timeperiod', title: '优惠时间段', align: 'center'},
+                        {field: 'timeperiod', title: '优惠时间段',width:100,align: 'center'},
                         {
                             field: 'ruletype',
                             title: '餐补类型',