package com.njcn.product.system.theme.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;

import com.njcn.product.auth.pojo.constant.HomePageState;
import com.njcn.product.auth.pojo.enums.UserResponseEnum;
import com.njcn.product.system.theme.mapper.HomePageMapper;
import com.njcn.product.system.theme.pojo.param.HomePageParam;
import com.njcn.product.system.theme.pojo.po.HomePage;
import com.njcn.product.system.theme.service.IHomePageService;
import com.njcn.web.utils.RequestUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2021-12-13
 */
@Service
public class HomePageServiceImpl extends ServiceImpl<HomePageMapper, HomePage> implements IHomePageService {

    @Override
    public boolean add(HomePageParam homePageParam) {
        checkHomePageName(homePageParam,false);
        String component = homePageParam.getLayout().replace("&quot;","\"");
        HomePage homePage = new HomePage();
        BeanUtil.copyProperties(homePageParam, homePage);
        homePage.setUserId(RequestUtil.getUserIndex());
        homePage.setState(HomePageState.ENABLE);
        homePage.setLayout(component);
        return this.save(homePage);
    }

    @Override
    public boolean delete(String id) {
        return this.lambdaUpdate()
                .set(HomePage::getState, HomePageState.DELETE)
                .in(HomePage::getId, id)
                .update();
    }

    @Override
    public boolean update(HomePageParam.HomePageUpdateParam homePageUpdate) {
        checkHomePageName(homePageUpdate,true);
        HomePage homePage = new HomePage();
        BeanUtil.copyProperties(homePageUpdate, homePage);
        return this.updateById(homePage);
    }

    @Override
    public List<HomePage> getHomePagesByUserId(String id) {
        List<String> userList = new ArrayList<>();
        userList.add(id);
        userList.add(HomePageState.DEFAULT_USER_ID);
        return this.lambdaQuery().in(HomePage::getUserId,userList).eq(HomePage::getState, HomePageState.ENABLE).orderByAsc(HomePage::getSort).list();
    }

    @Override
    public HomePage getHomePageById(String id) {
        return this.lambdaQuery().in(HomePage::getId,id).one();
    }

    @Override
    public List<String> getUsedHomePage(String path) {
        return this.lambdaQuery().in(HomePage::getUserId,RequestUtil.getUserIndex()).eq(HomePage::getState,HomePageState.ENABLE).likeRight(HomePage::getPath,path).list().stream().map(HomePage::getPath).distinct().collect(Collectors.toList());
    }

    /**
     * 校验参数，检查是否存在相同名称的首页
     */
    private void checkHomePageName(HomePageParam homePageParam, boolean isExcludeSelf) {
        LambdaQueryWrapper<HomePage> homePageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        homePageLambdaQueryWrapper
                .eq(HomePage::getName,homePageParam.getName())
                .eq(HomePage::getState, HomePageState.ENABLE);
        //更新的时候，需排除当前记录
        if(isExcludeSelf){
            if(homePageParam instanceof HomePageParam.HomePageUpdateParam){
                homePageLambdaQueryWrapper.ne(HomePage::getId,((HomePageParam.HomePageUpdateParam) homePageParam).getId());
            }
        }
        int countByAccount = this.count(homePageLambdaQueryWrapper);
        //大于等于1个则表示重复
        if (countByAccount >= 1) {
            throw new BusinessException(UserResponseEnum.REGISTER_HOMEPAGE_NAME_EXIST);
        }
    }

}
