package com.kxs.system.biz.controller.kxsapp;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kxs.common.core.constant.CacheConstants;
import com.kxs.common.core.constant.CommonConstants;
import com.kxs.common.core.constant.SecurityConstants;
import com.kxs.common.core.constant.enums.ErrorTypeEnum;
import com.kxs.common.core.exception.GlobalCustomerException;
import com.kxs.common.core.util.R;
import com.kxs.common.core.util.RetOps;
import com.kxs.common.security.util.SecurityUtils;
import com.kxs.system.api.model.KxsAppModal;
import com.kxs.system.api.model.KxsBanner;
import com.kxs.system.api.vo.kxsapp.banner.BannerListVO;
import com.kxs.system.biz.service.KxsAppModalService;
import com.kxs.system.biz.service.KxsBannerService;
import com.kxs.user.api.feign.RemoteKxsUserService;
import com.kxs.user.api.model.KxsUser;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 首页(Banner)控制层
 *
 * @author 系统
 * @since 2023-12-15 12:16:33
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("banner")
public class BannerController {

    /**
     * 服务对象
     */
    private final KxsBannerService kxsBannerService;

    private final KxsAppModalService kxsAppModalService;

    private final RemoteKxsUserService remoteKxsUserService;

    /**
     * 首页-广告管理
     *
     * @return 首页-广告管理
     */
    @GetMapping("/home")
//    @Cacheable(value = CacheConstants.BANNER_LIST, key = "#colId", unless = "#result.data.isEmpty()")
    public R<List<BannerListVO>> home(@RequestParam("colId") String colId) {

        return R.ok(kxsBannerService.homeData(colId));
    }

    /**
     * 首页-广告详情
     *
     * @return 首页-广告管理
     */
    @GetMapping("/detail")
    public R<KxsBanner> detail(@RequestParam("id") Integer id) {

        return R.ok(kxsBannerService.getById(id));
    }

    @GetMapping("/appModal")
    public R appModal() {

        Long userId = SecurityUtils.getUser().getId();
        R<KxsUser> kxsUserR = remoteKxsUserService.loadUserById(userId, SecurityConstants.FROM_IN);
        KxsUser user = RetOps.of(kxsUserR)
                .getData()
                .orElseThrow(() -> new GlobalCustomerException(ErrorTypeEnum.USER_NOT_FOUND.getDescription()));

        List<KxsAppModal> list = kxsAppModalService.list(Wrappers.<KxsAppModal>lambdaQuery()
                .eq(KxsAppModal::getStatus, CommonConstants.SUCCESS));

        List<KxsAppModal> appModals = list.stream().filter(item -> {
            if (Objects.equals(item.getIsAssign(), CommonConstants.SUCCESS)) {
                String userCodes = item.getUserCodes();
                if(StrUtil.isBlank(userCodes)){
                    return Boolean.TRUE;
                }
                String[] split = userCodes.split(StrUtil.COMMA);
                //如果未包含删除该条数据
                return Arrays.asList(split).contains(user.getUserCode());
            }
            return Boolean.TRUE;
        }).toList();

        return R.ok(appModals);
    }









}

