package com.gxwebsoft.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.cms.entity.MpAd;
import com.gxwebsoft.cms.entity.MpField;
import com.gxwebsoft.cms.entity.MpMenu;
import com.gxwebsoft.cms.mapper.MpPagesMapper;
import com.gxwebsoft.cms.service.MpAdService;
import com.gxwebsoft.cms.service.MpFieldService;
import com.gxwebsoft.cms.service.MpMenuService;
import com.gxwebsoft.cms.service.MpPagesService;
import com.gxwebsoft.cms.entity.MpPages;
import com.gxwebsoft.cms.param.MpPagesParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * 小程序页面Service实现
 *
 * @author 科技小王子
 * @since 2024-07-20 19:49:55
 */
@Service
public class MpPagesServiceImpl extends ServiceImpl<MpPagesMapper, MpPages> implements MpPagesService {
    @Resource
    private MpMenuService mpMenuService;
    @Resource
    private MpAdService mpAdService;
    @Resource
    private MpFieldService mpFieldService;

    @Override
    public PageResult<MpPages> pageRel(MpPagesParam param) {
        PageParam<MpPages, MpPagesParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time asc");
        List<MpPages> list = baseMapper.selectPageRel(page, param);
        if (!CollectionUtils.isEmpty(list) && param.getSceneType() != null && param.getSceneType().equals("showMpMenus")) {
          final Integer pageId = list.get(0).getId();
          final List<MpMenu> mpMenus = mpMenuService.list(new LambdaUpdateWrapper<MpMenu>().eq(MpMenu::getPageId, pageId).orderByAsc(MpMenu::getSortNumber).orderByDesc(MpMenu::getCreateTime));
          final Map<Integer, List<MpMenu>> collect = mpMenus.stream().collect(Collectors.groupingBy(MpMenu::getParentId));
          final List<MpAd> ads = mpAdService.list(new LambdaUpdateWrapper<MpAd>().eq(MpAd::getPageId, pageId).eq(MpAd::getStatus,0));
          final List<MpField> mpFields = mpFieldService.list(new LambdaQueryWrapper<MpField>().eq(MpField::getPageId, pageId));
          // 配置信息
          HashMap<String, Object> fields = new HashMap<>();
          mpFields.forEach(d -> {
            fields.put(d.getName(), d.getValue());
          });
          list.forEach(d -> {
            d.setMpMenus(collect);
            d.setAds(ads);
            d.setConfig(fields);
          });
        }
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<MpPages> listRel(MpPagesParam param) {
        List<MpPages> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<MpPages, MpPagesParam> page = new PageParam<>();
        page.setDefaultOrder("sort_number asc, create_time asc");
        return page.sortRecords(list);
    }

    @Override
    public MpPages getByIdRel(Integer id) {
        MpPagesParam param = new MpPagesParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

}
