package org.jeecg.modules.userCode.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.banner.entity.WebBanner;
import org.jeecg.modules.banner.service.IWebBannerService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.tbConf.entity.TbConf;
import org.jeecg.modules.tbConf.service.ITbConfService;
import org.jeecg.modules.userCode.service.IndexService;
import org.jeecg.modules.userRole.service.ITbUserRoleService;
import org.jeecg.modules.utils.ValidateTool;
import org.springframework.stereotype.Service;

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


@Service
public class IndexServiceImpl implements IndexService {

    @Resource
    private IWebBannerService webBannerService;
    @Resource
    private ITbConfService tbConfService;
    @Resource
    private ShiroRealm shiroRealm;
    @Resource
    private ITbUserRoleService tbUserRoleService;
    @Resource
    private RedisUtil redisUtil;

    @Override
    public Result<?> getIndexInfo(String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        hanHaiMember.setPasswordSalt(null);
        JSONObject jsonObject = new JSONObject();
        QueryWrapper<TbConf> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("name", "sys_phone");
        TbConf one = tbConfService.getOne(objectQueryWrapper);
        //平台客户电话号码
        jsonObject.put("phone", ValidateTool.isNull(one) ? "19198239116" : one.getValue());
        jsonObject.put("my", hanHaiMember);
        Map roleInfo = tbUserRoleService.getRoleInfo(hanHaiMember.getId(),false);
//          返回用人方个人身份信息
        jsonObject.put("boss", roleInfo.get("boss"));
        //   返回用人方企业身份信息
        jsonObject.put("company", roleInfo.get("company"));
        jsonObject.put("worker", roleInfo.get("worker"));

        return Result.OK(jsonObject);
    }


    @Override
    public Result<Map<Integer, List<WebBanner>>> bannerList() {
        //获取banner
        QueryWrapper<WebBanner> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("is_disable", 0).orderByDesc("sort");
        List<WebBanner> bannerList = webBannerService.list(objectQueryWrapper);
        //分组排序返回
        Map<Integer, List<WebBanner>> collect = bannerList.stream().sorted(Comparator.comparing(WebBanner::getSort).reversed()).collect(Collectors.groupingBy(WebBanner::getType));
        return Result.ok(collect);
    }
}
