package com.controller;

import com.config.BaseController;
import com.config.ControllerInterface;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.tools.JwtUtils;
import com.utils.CommonUtils;

import java.util.List;
import java.util.Map;

/**
 * marc字段设置
 */
@ControllerInterface(path="/sysmkf")
public class SysmkfController extends BaseController {

    public void index() {
        render("/view/list-manage/sysmkf.html");
    }

    public void findListByOrgid(){
        String token = this.getPara("token");
        Map userinfo = JwtUtils.validateJWTForMap(token);
        if(userinfo == null){
            fail(3,"token错误");
            return;
        }
        int orgid = (Integer) userinfo.get("orgid");
        List<Record> list = Db.find("select id,fieldid,subfieldid,cnfield from t_sysmkf where orgid = ?",orgid);
        if(CommonUtils.isNullOrEmpty(list) || list.size() == 0){
            list = Db.find("select * from t_sysmkf where orgid = 121001");
            for(Record record : list) {
                record.remove("id");
                record.set("orgid",orgid);
                Db.save("t_sysmkf",record);
            }
            list = Db.find("select id,fieldid,subfieldid,cnfield from t_sysmkf where orgid = ?",orgid);
        }
        success(list);
    }

    public void updateSysmkfInfo(){
        String token = this.getPara("token");
        Map userinfo = JwtUtils.validateJWTForMap(token);
        if(userinfo == null){
            fail(3,"token错误");
            return;
        }
        int orgid = (Integer) userinfo.get("orgid");
        Integer id = getParaToInt("id",0);
        String subfieldid = getPara("subfieldid","");
        String fieldid = getPara("fieldid","");
        Db.update("update t_sysmkf set fieldid=?,subfieldid=? where id = ? and orgid = ?",fieldid,subfieldid,id,orgid);
        success();
    }

}
