package com.xhkjedu.sapi.service.system;

import com.xhkjedu.sapi.mapper.cloud.CloudMapper;
import com.xhkjedu.sapi.mapper.system.SchoolMapper;
import com.xhkjedu.sapi.mapper.system.SchoolStypeMapper;
import com.xhkjedu.sapi.model.cloud.TCloud;
import com.xhkjedu.sapi.model.system.TSchool;
import com.xhkjedu.sapi.model.system.TSchoolStype;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ywx
 * @classname TSchoolStypeService
 * @description
 * @date 2021/2/23 9:51
 **/
@Service
public class SchoolStypeService {
    @Resource
    private SchoolStypeMapper schoolStypeMapper;
    @Resource
    private SchoolMapper schoolMapper;
    @Resource
    private CloudMapper cloudMapper;

    //设置登录页
    public ResultVo batchSave(List<TSchoolStype> list) {
        TSchoolStype stype = list.get(0);
        Integer schoolid = stype.getSchoolid();
        String schooldomain = stype.getSchooldomain();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1});
        TSchool school = new TSchool();
        school.setSchoolid(schoolid);
        if (schooldomain == null) schooldomain = "";
        school.setSchooldomain(schooldomain);
        schoolMapper.updateByPrimaryKeySelective(school);
        list.forEach(model -> {
            model.setCreatetime(N_Utils.getSecondTimestamp());
        });
        schoolStypeMapper.batchSave(list);
        return new ResultVo(0, "保存成功");
    }

    //获取列表
    public List <TSchoolStype> listAll(TSchoolStype stype) {
        List<TSchoolStype> list = schoolStypeMapper.findAll(stype);
        if (list == null) {
            list = new ArrayList<>();
        }
        TCloud cloud = cloudMapper.getCloud();
        String schooldomain = stype.getSchooldomain();
        //登录页面字体图片
        int size = list.stream().filter(t -> t.getSchooltype().equals("loginname")).collect(Collectors.toList()).size();
        if (size == 0) {
            TSchoolStype type = new TSchoolStype();
            type.setSchooldomain(schooldomain);
            type.setSchooltype("loginname");
            type.setStypeaddr(cloud.getLoginname());
            list.add(type);
        }

        //登录背景图片
        size = list.stream().filter(t -> t.getSchooltype().equals("loginbg")).collect(Collectors.toList()).size();
        if (size == 0) {
            TSchoolStype type = new TSchoolStype();
            type.setSchooldomain(schooldomain);
            type.setSchooltype("loginbg");
            type.setStypeaddr(cloud.getLoginbg());
            list.add(type);
        }

        //技术支持
        size = list.stream().filter(t -> t.getSchooltype().equals("tesupport")).collect(Collectors.toList()).size();
        if (size == 0) {
            TSchoolStype type = new TSchoolStype();
            type.setSchooldomain(schooldomain);
            type.setSchooltype("tesupport");
            type.setStypeaddr(cloud.getTesupport());
            list.add(type);
        }

        //备案号
        size = list.stream().filter(t -> t.getSchooltype().equals("recordno")).collect(Collectors.toList()).size();
        if (size == 0) {
            TSchoolStype type = new TSchoolStype();
            type.setSchooldomain(schooldomain);
            type.setSchooltype("recordno");
            type.setStypeaddr(cloud.getRecordno());
            list.add(type);
        }
        return list;
    }

    //设置主题
    public ResultVo batchSave2(List<TSchoolStype> list) {
        TSchoolStype stype = list.get(0);
        Integer schoolid = stype.getSchoolid();
        N_Utils.validation(new Object[]{schoolid,"学校id",1});
        TSchoolStype mainlogo = list.stream().filter(s -> s.getSchooltype().equals("mainlogo")).findFirst().orElse(null);
        if (mainlogo != null) {
            TSchool school = new TSchool();
            school.setSchoolid(schoolid);
            school.setSchoollogo(mainlogo.getStypeaddr());
            schoolMapper.updateByPrimaryKeySelective(school);
        }
        list.forEach(model->{model.setCreatetime(N_Utils.getSecondTimestamp());});
        schoolStypeMapper.batchSave(list);
        return new ResultVo(0, "保存成功");
    }

    //根据学校id获取列表
    public List <TSchoolStype> listAll2(TSchoolStype stype) {
        List<TSchoolStype> lst = schoolStypeMapper.findAll2(stype);
        List<TSchoolStype> rtnlst = new ArrayList<>();
        if(N_Utils.isListNotEmpty(lst)) {
            List<TSchoolStype> filterlst = new ArrayList<>();
            for (TSchoolStype s : lst) {
                if (s.getStid() != null && s.getStid() > 0) {
                    filterlst.add(s);
                }
            }

            //获取学校样式，如果学校未设置使用default设置的内容
            Map<String,List<TSchoolStype>> map = filterlst.stream().collect(Collectors.groupingBy(TSchoolStype :: getSchooltype,Collectors.toList()));
            for(Map.Entry<String, List<TSchoolStype>> entry : map.entrySet()){
                List<TSchoolStype> maplist = entry.getValue();
                if(maplist.size() == 1){
                    rtnlst.add(maplist.get(0));
                }else if(maplist.size()>1){
                    for(TSchoolStype s : maplist){
                        if(s.getSchooldomain() == null || !s.getSchooldomain().equals("default")){
                            rtnlst.add(s);
                            break;
                        }
                    }
                }
            }
        }
        return rtnlst;
    }

    /*
     * @Description 获取学校样式（设置登录页面获取数据）
     * @Date 2023/11/21 10:39:18
     * @Author WN
     * @Param [stype]
     * @Return java.util.Map<java.lang.String,java.util.List<com.xhkjedu.sapi.model.system.TSchoolStype>>
     **/
    public Map<String,List<TSchoolStype>> listSchoolStypeAndDefault(TSchoolStype stype) {
        List<TSchoolStype> lst = schoolStypeMapper.findAll2(stype);

        Map<String,List<TSchoolStype>> rtnMap = new HashMap<>();
        if(N_Utils.isListNotEmpty(lst)){
            //获取学校设置的
            List<TSchoolStype> schoolStypes = lst.stream().filter(s -> stype.getSchoolid().equals(s.getSchoolid())).collect(Collectors.toList());
            //默认地址
            List<TSchoolStype> defaultStypes = lst.stream().filter(s -> "default".equals(s.getSchooldomain())).collect(Collectors.toList());

            rtnMap.put("school",schoolStypes);
            rtnMap.put("default",defaultStypes);
        }else{
            rtnMap.put("school",new ArrayList<>());
            rtnMap.put("default",new ArrayList<>());
        }

        return rtnMap;
    }
}
