package com.controller.sysconfig;

import com.config.BaseController;
import com.config.ControllerInterface;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.model.OrgBookField;
import com.model.OrgSetting;
import com.model.User;
import com.service.SysConfigService;
import com.tools.JwtUtils;
import com.utils.CommonUtils;

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

@ControllerInterface(path="/sysconfig")
public class SysConfigController extends BaseController {
    SysConfigService sysConfigService = SysConfigService.me;

    public void index() {
        render("/view/sysconfig/sysconfig.html");
    }

    public void synchronizedData(){
        render("/view/sysconfig/synchronizedData.html");
    }

    public void synchronizedInfo_web(){
        String token = this.getPara("token");
        int operatorid = JwtUtils.validateJWTForUserId(token);
        if(operatorid == 0){
            fail(3,"token错误");
            return;
        }

        User user= getSessionAttr("userInfo");
        if(CommonUtils.isNullOrEmpty(user)) {
            fail(1,"非法操作");
            return;
        }

        String parentorgid = this.getPara("parentorgid");
        if(CommonUtils.isNullOrEmpty(parentorgid)){
            fail(1,"parentorgid不能为空");
            return;
        }

        success();
    }

    public void synchronizedInfo_offLine(){
        String token = this.getPara("token");
        int operatorid = JwtUtils.validateJWTForUserId(token);
        Integer currentOrgid = getParaToInt("currentOrgid",0);
        if(operatorid == 0){
            fail(3,"token错误");
            return;
        }
        User user= getSessionAttr("userInfo");
        if(CommonUtils.isNullOrEmpty(user)) {
            fail(1,"非法操作");
            return;
        }
        String parentorgid = this.getPara("parentorgid");
        if(CommonUtils.isNullOrEmpty(parentorgid) && currentOrgid == 0){
            fail(1,"parentorgid不能为空");
            return;
        }
        if(currentOrgid != 0) {
            Record record = Db.findById("t_org",currentOrgid);
            if(CommonUtils.isNullOrEmpty(record)) {
                fail(1,"查询不到需要同步到的机构信息");
                return;
            }
        }

        success();
    }

    public void verificationShucihaoByOrgid(){
        String token = this.getPara("token");
        int operatorid = JwtUtils.validateJWTForUserId(token);
        if(operatorid == 0){
            fail(3,"token错误");
            return;
        }
        User user= getSessionAttr("userInfo");
        if(CommonUtils.isNullOrEmpty(user)) {
            fail(1,"非法操作");
            return;
        }
        Integer orgid = this.getParaToInt("orgid");
        if(CommonUtils.isNullOrEmpty(orgid)){
            fail(1,"orgid不能为空");
            return;
        }
        List<Record> list = Db.find("select id,bookno from t_book where orgid = ?",orgid);
        for(Record record : list) {
            String bookno = record.getStr("bookno");
            if(CommonUtils.isNullOrEmpty(bookno) || bookno.indexOf("/") == -1 ||  (bookno.indexOf("/") > bookno.indexOf(":") && bookno.indexOf(":")>-1)){
                continue;
            }
            try {
                String shucihaostr = "";
                if(bookno.indexOf("-",3)>-1 && bookno.indexOf("/") < bookno.indexOf("-",3)){
                    shucihaostr = bookno.substring(bookno.indexOf("/")+1,bookno.indexOf("-",3));
                } else if(bookno.indexOf(":")>-1){
                    shucihaostr = bookno.substring(bookno.indexOf("/")+1,bookno.indexOf(":"));
                } else {
                    shucihaostr = bookno.substring(bookno.indexOf("/")+1);
                }
                Integer shucihao = Integer.parseInt(shucihaostr);
                record.set("shucihao",shucihao);
            }catch (Exception e){
                logger.info("书次号转换异常，bookno:"+bookno+" id:"+record.getInt("id"));
                continue;
            }
        }
        Db.batchUpdate("t_book",list,1000);
        success();
    }

    public void addOrUpdate(){
        try{
            User user= getSessionAttr("userInfo");
            int operatorid = JwtUtils.validateJWTForUserId(user.get("token").toString());
            if(operatorid == 0){
                fail(3,"token错误");
                return;
            }
            OrgBookField orgBookField=getBean(OrgBookField.class,"bean");
            OrgSetting orgSetting = getBean(OrgSetting.class,"bean");

            orgBookField.setOrgid(user.getOrgid());
            orgSetting.setOrgid(user.getOrgid());
            orgSetting.setStatus(1);
            if(orgSetting.getUsername() != null){
                orgSetting.setUsername(orgSetting.getUsername().trim());
            }
            if(orgSetting.getPassword() != null){
                orgSetting.setPassword(orgSetting.getPassword().trim());
            }

            sysConfigService.addOrUpdate(orgBookField,orgSetting);
        }catch (Exception e){
            fail(0,e.getMessage());
        }

        success();
    }


    public void findSysConfig(){
        User user= getSessionAttr("userInfo");
        int operatorid = JwtUtils.validateJWTForUserId(user.get("token").toString());
        if(operatorid == 0){
            fail(3,"token错误");
            return;
        }
        OrgBookField orgBookField=sysConfigService.findOrgBookField(user.getOrgid());
        OrgSetting orgSetting = sysConfigService.findOrgSetting(user.getOrgid());
        Map<String,Object> result = new HashMap<String, Object>();
        result.put("OrgBookField",orgBookField);
        result.put("OrgSetting",orgSetting);
        success(result);
    }

    public void findSelect(){
        List<Record> list = Db.find("select id,orgname from t_org where orgtypeid = 1 order by id asc");
        success(list);
    }


}
