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


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.alUser.service.IndexService;
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.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.userRole.entity.TbUserRole;
import org.jeecg.modules.userRole.service.ITbUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
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;
    @Autowired
    private IHanHaiMemberService iHanHaiMemberService;
    @Autowired
    private ShiroRealm shiroRealm;
    @Resource
    private ITbUserRoleService tbUserRoleService;

    @Override
    public Result<?> getIndexInfo(String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        hanHaiMember.setPasswordSalt(null);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("phone", "123456");
        jsonObject.put("my", hanHaiMember);

        LambdaQueryWrapper<TbUserRole> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(TbUserRole::getUserId, hanHaiMember.getId()).
                eq(TbUserRole::getRole, 0).eq(TbUserRole::getAuditStatus, 1);
        TbUserRole cg = tbUserRoleService.getOne(objectLambdaQueryWrapper);
        LambdaQueryWrapper<TbUserRole> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(TbUserRole::getUserId, hanHaiMember.getId()).
                eq(TbUserRole::getRole, 1).eq(TbUserRole::getAuditStatus, 1);
        TbUserRole shop = tbUserRoleService.getOne(queryWrapper1);
//        返回采购商身份信息
        jsonObject.put("buy", cg);
//          返回供应商身份信息
        jsonObject.put("shop", shop);

        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);
    }
}
