package com.iotechn.unimall.admin.api.market.impl;

import java.util.List;

import com.iotechn.unimall.admin.api.market.IMarketCaseService;
import com.iotechn.unimall.data.domain.market.MarketCase;
import com.iotechn.unimall.data.mapper.market.MarketCaseMapper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.util.ExcelUtil;
import com.iotechn.unimall.data.model.Page;
import java.util.Date;
import org.springframework.transaction.annotation.Transactional;

/**
 * 关注Service业务层处理
 * 
 * @author jlb
 * @date 2023-02-03
 */
@Service
public class MarketCaseServiceImpl implements IMarketCaseService {
	@Autowired
	private MarketCaseMapper marketCaseMapper;
	
	@Override
	public Boolean add(MarketCase marketCase, Long adminId) throws ServiceException {
		Date now = new Date();
		marketCase.setGmtCreate(now);
		marketCase.setGmtUpdate(now);
		marketCase.setAdminId(adminId);
		return marketCaseMapper.insert(marketCase)>0;
	}

	@Override
	public Page<MarketCase> list(String concernedId,String followersId, Integer page, Integer limit)throws ServiceException {
		Wrapper<MarketCase> wrapper = new EntityWrapper<MarketCase>();
														if (!StringUtils.isEmpty(concernedId)) {
					wrapper.eq("concerned_id", concernedId);
				}
												if (!StringUtils.isEmpty(followersId)) {
					wrapper.eq("followers_id", followersId);
				}
							wrapper.eq("delete_flag", 0);
		List<MarketCase> list = marketCaseMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
		Integer count = marketCaseMapper.selectCount(wrapper);
		return new Page<MarketCase>(list, page, limit, count);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean delete(String id) {
		String[] ids = String.valueOf(id).split(",");
		for (String tt:ids) {
			MarketCase tmp =  marketCaseMapper.selectById(Long.parseLong(tt));
			if(tmp != null){
				tmp.setDeleteFlag(1);
				marketCaseMapper.updateById(tmp);
			}
		}
		return true;
	}

	@Override
	public Boolean update(MarketCase marketCase,Long adminId) throws ServiceException {
		Date now = new Date();
		marketCase.setGmtUpdate(now);
		marketCase.setAdminId(adminId);
		return marketCaseMapper.updateById(marketCase)>0;
	}

	@Override
	public MarketCase get(Long id) throws ServiceException {
		return marketCaseMapper.selectById(id);
	}
	
	@Override
	public String export(String concernedId,String followersId, Integer page, Integer limit)throws ServiceException {
		Wrapper<MarketCase> wrapper = new EntityWrapper<MarketCase>();
														if (!StringUtils.isEmpty(concernedId)) {
					wrapper.eq("concerned_id", concernedId);
				}
												if (!StringUtils.isEmpty(followersId)) {
					wrapper.eq("followers_id", followersId);
				}
							List<MarketCase> list = marketCaseMapper.selectList(wrapper);
		ExcelUtil<MarketCase> util = new ExcelUtil<MarketCase>(MarketCase.class);
		return util.exportExcel(list, "操作日志");
	}
}
