package com.ruoyi.demo.service.impl;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.demo.entity.App;
import com.ruoyi.demo.entity.Commercial;
import com.ruoyi.demo.entity.bo.CommercialBo;
import com.ruoyi.demo.entity.vo.CommercialVo;
import com.ruoyi.demo.mapper.AppMapper;
import com.ruoyi.demo.mapper.CommercialMapper;
import com.ruoyi.demo.service.CommericalService;
import com.ruoyi.demo.utils.InitMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommercialServiceImpl implements CommericalService {
    @Autowired
    CommercialMapper commercialMapper;
    @Autowired
    InitMapUtil initMapUtil;

    @Autowired
    AppMapper appMapper;
    @Override
    public void addCommercial(CommercialVo commercialVo) {
        Commercial commercial = new Commercial(commercialVo);
        commercial.setCreateTime(LocalDateTime.now());
        commercial.setAddrCodeInfo(initMapUtil.getInitAddrCodeMap(commercial.getAddrCode()));
        commercialMapper.insert(commercial);
    }

    @Override
    public void deleteCommercial(String[] commercialIds) {
        commercialMapper.deleteBatchIds(Arrays.asList(commercialIds));
    }

    @Override
    public Commercial getCommercial(String commercialId) {
        return commercialMapper.selectById(commercialId);
    }

    @Override
    public HashMap getCommercialList(CommercialVo commercialVo) {
        QueryWrapper<Commercial> queryWrapper = new QueryWrapper<>();
        if(commercialVo.getSearchText() != null && !commercialVo.getSearchText().equals("")){
            queryWrapper.likeRight("commercial_id",commercialVo.getSearchText());
            queryWrapper.or().like("commercial_name",commercialVo.getSearchText());
            queryWrapper.or().like("principal_name",commercialVo.getSearchText());
            queryWrapper.or().likeRight("principal_telephone",commercialVo.getSearchText());
        }

        PageHelper.startPage(commercialVo.getPageNum(), commercialVo.getPageSize());
        List<Commercial> commercials = commercialMapper.selectList(queryWrapper);
        PageInfo<Commercial> pageInfo = new PageInfo<>(commercials);

        //1.找到需要关联的小程序
        ArrayList<String> commercialIds = new ArrayList<>();
        List<CommercialBo> result = new ArrayList<>();
        for (Commercial commercial : pageInfo.getList()) {
            commercialIds.add(commercial.getCommercialId());
            CommercialBo commercialBo = new CommercialBo(commercial);
            commercialBo.setApps(new ArrayList<>());
        }

        QueryWrapper<App> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.select("commercial_id","name");
        queryWrapper1.in("commercial_id",commercialIds);
        List<App> apps = appMapper.selectList(queryWrapper1);
        //2.组装Bo
        for (CommercialBo commercialBo : result) {
            ArrayList<String> list = new ArrayList<>();
            apps.stream().forEach(item -> {
                if(item.getCommercialId().equals(commercialBo.getCommercialId()))
                    list.add(item.getAppName());
            });
            commercialBo.setApps(list);
        }

        pageInfo.setList(null);
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("page",pageInfo);
        objectObjectHashMap.put("data",result);
        return objectObjectHashMap;
    }

    @Override
    public List<Commercial>  getCommercialName() {
        QueryWrapper<Commercial> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("commercial_name","commercial_id");
        List<Commercial> commercials = commercialMapper.selectList(queryWrapper);
        return commercials;
    }
}
