package com.xhkjedu.sapi.service.dock;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.sapi.config.ConfigKey;
import com.xhkjedu.sapi.mapper.system.SchoolMapper;
import com.xhkjedu.sapi.mapper.system.UserMapper;
import com.xhkjedu.sapi.model.system.TSchool;
import com.xhkjedu.sapi.vo.dock.LcAdminVo;
import com.xhkjedu.sapi.vo.dock.LcResultVo;
import com.xhkjedu.sapi.vo.dock.LcSchoolVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.vo.system.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName LcService
 * Description TODO 领创对接
 * Author WN
 * Date 2021/5/24 11:49
 **/
@Service
@Slf4j
public class LcService {
    @Resource
    private SchoolMapper schoolMapper;
    @Resource
    private UserMapper userMapper;

    //绑定学校
    public ResultVo saveSchool(LcSchoolVo schoolVo){
        //获取学校信息
        TSchool school = schoolMapper.getSchoolDetailById(schoolVo.getSchoolid());

        if(school!=null && school.getDocklc()==1){
            //说明已成功对接领创
            String new_schoolid = ConfigKey.lcSchoolPrefix + school.getCloudcode() + "_"+school.getSchoolid();

            LcSchoolVo lcs = new LcSchoolVo();
            lcs.setApp_id(ConfigKey.lcAppId);
            lcs.setSecurity_key(ConfigKey.lcSecurityKey);
            lcs.setSchool_id(new_schoolid);
            lcs.setName(school.getSchoolname());
            lcs.setAbbr(school.getShortname());
            lcs.setC_area_id(null);
            lcs.setProvince(schoolVo.getProvince());
            lcs.setProvince_id(schoolVo.getProvince_id());
            lcs.setCity(schoolVo.getCity());
            lcs.setCity_id(schoolVo.getCity_id());
            lcs.setCounty(schoolVo.getCounty());
            lcs.setCounty_id(schoolVo.getCounty_id());
            lcs.setAddress(schoolVo.getAddress());
            lcs.setDescription(schoolVo.getDescription());

            String rtnResult = sendPostParam("com.linspirer.school.create", JSON.toJSONString(lcs));
            LcResultVo lcResultVo = JSON.parseObject(rtnResult,LcResultVo.class);//学校管理员绑定
            if(lcResultVo.getCode()==0){
                //修改学校绑定绑定状态未已完成绑定
                schoolMapper.updateSchoolDock(school.getSchoolid(),2);

                return new ResultVo(0,"成功绑定管控学校信息");

            }else{
                return new ResultVo(1,"绑定管控学校信息失败:"+lcResultVo.getCode());
            }

        }else{
            //未成功对接领创
            return new ResultVo(1,"不对接管控");
        }

    }

    //绑定学校管理员
    public ResultVo saveSchoolAdmin(Integer schoolid){
        //获取学校信息
        TSchool school = schoolMapper.getSchoolDetailById(schoolid);
        //说明已成功对接领创
        String new_schoolid = ConfigKey.lcSchoolPrefix + school.getCloudcode() + "_"+school.getSchoolid();
        if(school.getDocklc()==2){
            //当绑定学校后绑定管理员
            //获取学校管理员
            UserVo userVo = userMapper.getSchoolAdminBySchoolid(schoolid);
            LcAdminVo lcAdminVo = new LcAdminVo();
            lcAdminVo.setApp_id(ConfigKey.lcAppId);
            lcAdminVo.setSecurity_key(ConfigKey.lcSecurityKey);
            lcAdminVo.setName(userVo.getLoginname());
            lcAdminVo.setEmail(userVo.getLoginname() + "@xhkjedu.com");
            lcAdminVo.setPassword("123456");
            lcAdminVo.setRole(3);
            lcAdminVo.setSchool_id(new_schoolid);
            lcAdminVo.setZone_id("");
            lcAdminVo.setStatus(1);
            lcAdminVo.setPhone("");
            lcAdminVo.setAddress("");
            lcAdminVo.setDescription("");

            String rtnResult = sendPostParam("com.linspirer.admin.create",JSON.toJSONString(lcAdminVo));

            LcResultVo userResultVo = JSON.parseObject(rtnResult,LcResultVo.class);
            if(userResultVo.getCode()==0){
                //标识已绑定挂管理员
                schoolMapper.updateSchoolDock(school.getSchoolid(),3);

                return new ResultVo(0, "保存成功");
            }else{
                return new ResultVo(0, "保存失败"+userResultVo.getCode());
            }
        }else if(school.getDocklc()==3){
            return new ResultVo(1,"已绑定学校管理员");
        }else{
            return new ResultVo(0,"请先绑定学校");
        }
    }

    private String sendPostParam(String method,String jsonObj){
        String strParams = "{\"id\":1,\"!version\":3,\"method\": \""+method+"\",\"params\":"+jsonObj+",\"jsonrpc\":\"2.0\"}";
//        log.info(strParams);
        String rtnResult = N_Utils.sendJsonPost(ConfigKey.lcVisiturl,strParams);
        return rtnResult;
    }

    //修改学校信息
    public ResultVo updateLcSchool(Integer schoolid){
        //获取学校信息
        TSchool school = schoolMapper.getSchoolDetailById(schoolid);
        if(school.getDocklc()==2){
            //已绑定管控，修改学校信息时修改领创上学校信息

            String new_schoolid = ConfigKey.lcSchoolPrefix + school.getCloudcode() + "_"+school.getSchoolid();

            LcSchoolVo lcs = new LcSchoolVo();
            lcs.setApp_id(ConfigKey.lcAppId);
            lcs.setSecurity_key(ConfigKey.lcSecurityKey);
            lcs.setSchool_id(new_schoolid);
            lcs.setName(school.getSchoolname());
            lcs.setAbbr(school.getShortname());
            lcs.setC_area_id(null);
            lcs.setProvince("河南省");
            lcs.setProvince_id(1664);
            lcs.setCity("郑州市");
            lcs.setCity_id(1665);
            lcs.setCounty("金水");
            lcs.setCounty_id(1670);
            lcs.setAddress("河南省郑州市金水区金水东路120号");
            lcs.setDescription("");

            String rtnResult = sendPostParam("com.linspirer.school.update", JSON.toJSONString(lcs));
            LcResultVo userResultVo = JSON.parseObject(rtnResult,LcResultVo.class);
            if(userResultVo.getCode()==0){
                return new ResultVo(0,"修改成功");
            }else{
                return new ResultVo(0,"修改失败"+userResultVo.getCode());
            }
        }else{
            //未添加管控
            return new ResultVo(0,"修改成功");
        }
    }

    //删除领创学校
    public ResultVo deleteLcSchool(Integer schoolid){
        //获取学校信息
        TSchool school = schoolMapper.getSchoolDetailById(schoolid);
        if(school.getDocklc()==2){
            String new_schoolid = ConfigKey.lcSchoolPrefix + school.getCloudcode() + "_"+school.getSchoolid();
            //已绑定管控，删除学校时，删除管控平台学校
            LcSchoolVo lcs = new LcSchoolVo();
            lcs.setApp_id(ConfigKey.lcAppId);
            lcs.setSecurity_key(ConfigKey.lcSecurityKey);
            lcs.setSchool_id(new_schoolid);
            String rtnResult = sendPostParam("com.linspirer.school.delete", JSON.toJSONString(lcs));
            LcResultVo userResultVo = JSON.parseObject(rtnResult,LcResultVo.class);
            if(userResultVo.getCode()==0){
                return new ResultVo(0,"删除成功");
            }else{
                return new ResultVo(0,"删除失败"+userResultVo.getCode());
            }
        }else{
            ////未添加管控
            return new ResultVo(0,"删除成功");
        }
    }



}
