package com.xhkjedu.sapi.service.component;

import com.xhkjedu.sapi.mapper.component.UserComponentMapper;
import com.xhkjedu.sapi.model.component.TUserComponent;
import com.xhkjedu.sapi.vo.component.ComponentParams;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description：用户组件
 * @Author: WN
 * @Date: 2024/7/19 14:47:41
 **/
@Service
public class UserComponentService {
    @Resource
    private UserComponentMapper userComponentMapper;

    /*
     * @Description 保存用户组件
     * @Date 2024/7/19 14:58:07
     * @Author WN
     * @Param [componentParams]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void save(ComponentParams componentParams){
        Integer userid = componentParams.getUserid();
        //保存前先删除已有组件
        userComponentMapper.deleteByUserid(userid);
        List<TUserComponent> userComponents = componentParams.getComponents();
        if(N_Utils.isListNotEmpty(userComponents)){
            Integer timestamp = N_Utils.getSecondTimestamp();
            int corder = 1;
            for(TUserComponent userComponent : userComponents){
                userComponent.setUserid(userid);
                userComponent.setCorder(corder);
                userComponent.setCreatetime(timestamp);
                corder++;
            }
            userComponentMapper.insertList(userComponents);
        }
    }

    /*
     * @Description 获取用户组件
     * @Date 2024/7/19 15:07:26
     * @Author WN
     * @Param [userid]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String,Object>> listComponentsByUserid(Integer userid){
        return userComponentMapper.listComponentsByUserid(userid);
    }

    /*
     * @Description 获取用户角色权限模块（匹配组件）
     * @Date 2024/7/26 15:07:24
     * @Author WN
     * @Param [userid]
     * @Return java.util.List<java.lang.String>
     **/
    public List<String>  listUserRoleModule(Integer userid){
        return userComponentMapper.listUserRoleModule(userid);
    }

    //初始化首页
    public void initAllUserComponent(){
        //初始化首页组件
        List<TUserComponent> initComponents = listInitComponents();

        //获取所有正常状态教师和管理员ID
        List<Integer> userids = userComponentMapper.listAllUserids();
        List<TUserComponent> saveComponents = new ArrayList<>();
        Integer timestamp = N_Utils.getSecondTimestamp();
        for(Integer userid : userids){
            for(TUserComponent userComponent : initComponents){
                TUserComponent sc = new TUserComponent();
                sc.setUserid(userid);
                sc.setCcode(userComponent.getCcode());
                sc.setCwidth(userComponent.getCwidth());
                sc.setCheight(userComponent.getCheight());
                sc.setCorder(userComponent.getCorder());
                sc.setCreatetime(timestamp);
                saveComponents.add(sc);
            }
        }
        userComponentMapper.insertList(saveComponents);
    }

    private List<TUserComponent> listInitComponents(){
        List<TUserComponent> initComponents = new ArrayList<>();
        TUserComponent uc1 = new TUserComponent();
        uc1.setCcode("HomeWorkNoticeResearch");
        uc1.setCwidth(1);
        uc1.setCheight(1);
        uc1.setCorder(1);
        initComponents.add(uc1);

        TUserComponent uc2 = new TUserComponent();
        uc2.setCcode("MySpace");
        uc2.setCwidth(1);
        uc2.setCheight(1);
        uc2.setCorder(2);
        initComponents.add(uc2);

        TUserComponent uc3 = new TUserComponent();
        uc3.setCcode("CommonUse");
        uc3.setCwidth(1);
        uc3.setCheight(1);
        uc3.setCorder(2);
        initComponents.add(uc3);

        TUserComponent uc4 = new TUserComponent();
        uc4.setCcode("PreviewHomeworkRecite");
        uc4.setCwidth(2);
        uc4.setCheight(1);
        uc4.setCorder(4);
        initComponents.add(uc4);

        TUserComponent uc5 = new TUserComponent();
        uc5.setCcode("SchoolResourceLibrary");
        uc5.setCwidth(1);
        uc5.setCheight(1);
        uc5.setCorder(5);
        initComponents.add(uc5);

        return initComponents;

    }


}
