package com.zcloud.modules.mall.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.zcloud.common.utils.PageUtils;
 import com.zcloud.common.utils.Tools;
 import com.zcloud.modules.mall.dao.BusBannerDao;
 import com.zcloud.modules.mall.entity.BusBannerEntity;
 import com.zcloud.modules.mall.form.SortForm;
 import com.zcloud.modules.mall.service.BusBannerService;
 import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.web.multipart.MultipartFile;

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

/**
 * @Author fangjiakai
 * @Desc 轮播图(BusBanner)表服务实现类
 * @Date 2024-06-26 10:00:25
 */
@Service("busBannerService")
public class BusBannerServiceImpl extends ServiceImpl<BusBannerDao, BusBannerEntity> implements BusBannerService {

    @Override
    public void save(BusBannerEntity param, MultipartFile file){
        param.setFileUrl(Tools.uploadFile(file, "mall"));
        baseMapper.insert(param);

    }
    @Override
	public BusBannerEntity queryByFlexibleParams(String paramName, Object paramValue) {
		QueryWrapper<BusBannerEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

		return baseMapper.selectOne(queryWrapper);
	}

	@Override
	public List<BusBannerEntity> queryListByFlexibleParams(String paramName, Object paramValue) {
		QueryWrapper<BusBannerEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

		return baseMapper.selectList(queryWrapper);
	}

    @Override
    public PageUtils listPage(Map<String, Object> params) {
        Page<Map<String,Object>> page = new Page<>(Integer.parseInt(params.get("curPage").toString()),Integer.parseInt(params.get("limit").toString()));
        params.put("isDelete",0);
        IPage<BusBannerEntity> ipage = baseMapper.listPage(page,params);
        return new PageUtils(ipage);
    }


   @Override
	public void delete(Integer bannerId, String userId){
		UpdateWrapper<BusBannerEntity> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("banner_id",bannerId);

		BusBannerEntity busBanner = new BusBannerEntity();
		busBanner.setOperator(userId);
		busBanner.setIsDelete(1);

		this.update(busBanner, updateWrapper);
	}

    @Override
    public void sort(List<SortForm> entityList){
        baseMapper.sort(entityList);
    }

    /**
     * 新增修改后重新排序
     */
    public void resort(){
        // 重新排序
        List<SortForm> sortList = new ArrayList<SortForm>();
        List<BusBannerEntity> list = queryListByFlexibleParams("is_show",1)
                .stream().sorted(Comparator.comparing(BusBannerEntity::getSort)).collect(Collectors.toList());
        if (!list.isEmpty()) {
            for(int i=0;i<list.size();i++) {
                SortForm sortForm = new SortForm();
                sortForm.setSort(i+1);
                sortForm.setId(list.get(i).getBannerId());
                sortList.add(sortForm);
            }
           sort(sortList);
        }
    }
}
