1. 程式人生 > >踩坑記錄_八月篇_1

踩坑記錄_八月篇_1

2018-8-14 星期二

1.部門管理和選單管理中的初始化表格:當前採用的方法為註釋id所在欄位列初始化程式碼

存在問題:
    1. id欄位所在列的visible設定不可見無效
    2. 將該欄位置於預設單選框{field: 'selectItem', radio: true}上後,發現複選框按鈕不存在
    3. 刪除初始化列的時候,所影響後面的欄位列的align配置

2.使用者管理

2.1 新增/修改使用者資訊的出生日期欄位不應該超過當前時間,需要加入出生日期判斷邏輯

定位到指定模組User,添加出生日期判斷邏輯
座標:user_info.js
    /*
    * 日期格式化方法
    * */
    Date.prototype.format = function(format) {
        var args = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
            "S": this.getMilliseconds()
        };
        if (/(y+)/.test(format))
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var i in args) {
            var n = args[i];
            if (new RegExp("(" + i + ")").test(format))
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
        }
        return format;
    };

    /*
    * 出生日期與當前時間比對
    * */
    UserInfoDlg.validateBir = function () {
        //獲取表單中所填入日期並格式化
        var userBir = new Date(Date.parse(this.get("birthday"))).format("yyyy-MM-dd");
        //獲取當前時間並格式化
        var nowTime = new Date().format("yyyy-MM-dd");
        if (nowTime >= userBir) {
            return true;
        } else {
            return false;
        }
    };
    /*
     * 新增使用者/編輯使用者提交時新增
     **/
    //出生日期判斷
    if (!this.validateBir()) {
        Feng.error("請填入正確的出生日期");
        return;
    }

2.2 出生日期新增時採用H+自帶的時間元件,原本輸入框的type=”date”去除

3.字典管理

3.1 完善字典管理備註欄位的使用

1. dict_add.html、dict_edit.html表單內容填充
    <div class="col-sm-12">
        <div class="form-group" id="tipsArea">
            <label class="col-sm-2 control-label">字典備註</label>
            <div class="col-sm-2">
                <input class="form-control" id="tips" type="text">
            </div>
        </div>
    </div>
2. dict_info.js獲取字典表單的資料
    初始化對話方塊加入:tips: '',        
    /**
     * 收集新增字典的資料
     */
    DictInfoDlg.collectData = function () {
        this.clearNullDom();
        var mutiString = "";
        //鍵值對組合獲取
        $("[name='dictItem']").each(function(){
            var num = $(this).find("[name='itemNum']").val();
            var name = $(this).find("[name='itemName']").val();
            mutiString = mutiString + (num + ":" + name + ";");
        });

        this.dictName = $("#dictName").val();
        this.mutiString = mutiString;
        //獲取欄位備註輸入框中的內容
        this.tips = $("#tips").val();

        // console.log(this.dictName)
        // console.log(this.mutiString)
        // console.log(this.tips)
    };
    //新增和編輯表單提交
    //傳入字典備註資訊
    ajax.set('tips', this.tips);
3. dict_edit.html:注意區分dict和subDicts
    dict:為字典外部類
    subDicts:為內部的鍵值對,這裡後續介紹字典的儲存結構。
    //獲取字典中的tips欄位
    <input class="form-control" id="tips" type="text" value="${dict.tips}">
    //迴圈遍歷內部的鍵值對組合,並顯示到頁面上
    @for(item in subDicts){
        <div class="form-group" name="dictItem" id="dictItem${itemLP.index}">
            <label class="col-sm-2 control-label">值</label>
            <div class="col-sm-2">
                <input class="form-control" type="text" name="itemNum" value="${item.num}">
            </div>
            <label class="col-sm-2 control-label" style="width: 8%;">名稱</label>
            <div class="col-sm-2">
                <input class="form-control" type="text" name="itemName" value="${item.name}">
            </div>
            <div class="col-sm-4">
                <#button btnCss="danger" name="刪除" id="cancel" icon="fa-remove" clickFun="DictInfoDlg.deleteItem(event)"/>
           </div>
        </div>
   @}
4. DictServiceImpl.java
    addDict、editDict,引數新增String tips
    addDict(String dictName, String dictValues, String tips){
        //解析dictValues
        List<Map<String, String>> items = parseKeyValue(dictValues);

        //新增字典
        Dict dict = new Dict();
        dict.setName(dictName);
        //新增字典備註
        dict.setTips(tips);
        dict.setNum(0);
        dict.setPid(0);
        this.dictMapper.insert(dict);

        //新增字典條目
        for (Map<String, String> item : items) {
            String num = item.get(MUTI_STR_KEY);
            String name = item.get(MUTI_STR_VALUE);
            Dict itemDict = new Dict();
            itemDict.setPid(dict.getId());
            itemDict.setName(name);
            try {
                itemDict.setNum(Integer.valueOf(num));
            }catch (NumberFormatException e){
                throw new BussinessException(BizExceptionEnum.DICT_MUST_BE_NUMBER);
            }
            this.dictMapper.insert(itemDict);
        }
    }
5. DictController.java 新增引數String tips,@BusinessLog中key加入tips
6. 關於資料表dict:劃分為兩部分 dict + subDicts
    CREATE TABLE `dict` (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵id',
      `num` int(11) DEFAULT NULL COMMENT '排序',
      `pid` int(11) DEFAULT NULL COMMENT '父級字典',
      `name` varchar(255) DEFAULT NULL COMMENT '名稱',
      `tips` varchar(255) DEFAULT NULL COMMENT '提示',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=92 DEFAULT CHARSET=utf8 COMMENT='字典表';
    欄位對應關係物件結構:
    'id'、'tips' --> dict
    'num'、'pid'、'name' --> subDicts --> List<Map<String, String>>       

3.2 Warpper對字典status欄位進行包裝:通知管理模組程式碼完善

包裝的含義在於對資料庫中存在的簡化儲存欄位進行解釋說明,這部分工作從Controller層實現,結合map+warpper。
/**
 * 獲取通知列表
 */
@RequestMapping(value = "/list")
@ResponseBody
public Object list(String condition) {
    List<Map<String, Object>> list = this.noticeDao.list(condition);
    return super.warpObject(new NoticeWrapper(list));
}
可以看出,在物件返回之前進行了warpper包裝處理:NoticeWrapper繼承BaseControllerWarpper
//實現抽象方法
protected abstract void warpTheMap(Map<String, Object> map);
NoticeWrapper.java中:
@Override
public void warpTheMap(Map<String, Object> map) {
    Integer creater = (Integer) map.get("creater");
    map.put("createrName", ConstantFactory.me().getUserNameById(creater));
    //根據Controller返回的通知資訊列表資料進行欄位包裝
    map.put("statusName", ConstantFactory.me().getNoticeStatusName((Integer) map.get("status")));
}
這裡我們看到了ConstantFactory這個類:常量的生產工廠類,繼承IConstantFactory。
我們要在IConstantFactory新增:
    /*
    * 獲取通知狀態
    * */
    String getNoticeStatusName(Integer status);
在ConstantFactory實現getNoticeStatusName方法:
    /*
    * 獲取通知狀態
    * */
    @Override
    public String getNoticeStatusName(Integer status) {
        return NoticeStatus.valueOf(status);
    }
重點來了:NoticeStatus 自定義列舉類 對status進行處理
/**
 * description:通知狀態列舉
 * author:jiangyanfei
 * date:2018/8/14
 * time:16:28
 */
public enum NoticeStatus {

    NOTICED(1, "已傳送"),
    DISABLE(0, "未傳送");

    int code;
    String message;

    NoticeStatus(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    //主要方法,對status進行解釋說明並返回
    public static String valueOf(Integer status) {
        if (status == null) {
            return "";
        } else {
            for (NoticeStatus s : NoticeStatus.values()) {
                if (s.getCode() == status) {
                    return s.getMessage();
                }
            }
            return "";
        }
    }
}
最後將進行處理後的欄位返回頁面:此時顯示的就是我們預先定義好的字典內容了
    {title: '狀態',field: 'statusName', align: 'center', valign: 'middle' }

2018-8-16 星期四

1.字典管理:字典管理最重要的功能是對資料表中的可讀性較差的欄位進行解釋說明,使得使用者可以通過web端形式對這些欄位進行操作。

字典管理:包裝可讀性較差的欄位
ConstantFactory.me().getStatusName((Integer) map.get("status")),通用的warpper呼叫方式。
第一次調整:
    ConstantFactory中採用的方案是列舉類XxxStatus覆寫valueOf方法,這樣寫的確可以對Controller中返回的資料進行包裝展示。
    OK(1, "啟用"), FREEZED(2, "凍結"), DELETED(3, "被刪除"); 例如這樣的列舉類定義格式。
    但是若超級管理員進行修改後(例:修改了key對應的value),不生效,這是必然的,因為warpper此時只對Controller中返回的結果,根本上就是字典dict沒有與對應的Controller建立聯絡。
    所以更改是無效的。而且列舉類中定義的常量key-value對,若修改了字典中的value,也是無法進行對應。
第二次調整:ConstantFactory類中存在getDictsByName(String name, Integer val)方法
    根據字典名稱和status值,查詢字典dict表中對應status(key)的value值,這樣在Controller中需要返回的資料中進行了與dict表的聯絡,幷包裝了欄位,實現了字典修改同步。
    但是,在呼叫此方法時候,name欄位一定要和字典管理中的dictname對應,即:當進行字典修改時,修改字典的名稱欄位會導致字典無法進行欄位包裝及字典同步。

2.使用者管理:使用者管理中的刪除操作時邏輯刪除,即不刪除user表中資料,只是將使用者的狀態置為”已刪除”。

3.角色管理:保留欄位變更為角色描述欄位,頁面更改,新增、編輯邏輯。

4.新添模組:交易管理:dealMgr、策略管理:strategyMgr

5.BaseJunit:單元測試父類@Transactional註解開啟預設單元測試執行完畢之後資料回滾!