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

import java.util.List;

import com.iotechn.unimall.data.mapper.market.MarketShopInfoMapper;
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.mapper.market.MarketCircleInfoMapper;
import com.iotechn.unimall.data.domain.market.MarketCircleInfo;
import com.iotechn.unimall.admin.api.market.IMarketCircleInfoService;
import com.iotechn.unimall.data.model.Page;
import java.util.Date;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生鲜圈Service业务层处理
 * 
 * @author jlb
 * @date 2023-01-03
 */
@Service
public class MarketCircleInfoServiceImpl implements IMarketCircleInfoService{
	@Autowired
	private MarketCircleInfoMapper marketCircleInfoMapper;
	@Autowired
	private MarketShopInfoMapper marketShopInfoMapper;
	
	@Override
	public Boolean add(MarketCircleInfo marketCircleInfo,Long adminId) throws ServiceException {
		Date now = new Date();
		marketCircleInfo.setGmtCreate(now);
		marketCircleInfo.setGmtUpdate(now);
		marketCircleInfo.setAdminId(adminId);
		return marketCircleInfoMapper.insert(marketCircleInfo)>0;
	}

	@Override
	public Page<MarketCircleInfo> list(Long companyId,Long shopId,String content,String status,String url,Integer deleteFlag,Date gmtCreate,Date gmtUpdate,Long userId,Long adminId, Integer page, Integer limit)throws ServiceException {
		Wrapper<MarketCircleInfo> wrapper = new EntityWrapper<MarketCircleInfo>();
														if (!StringUtils.isEmpty(companyId)) {
					wrapper.eq("company_id", companyId);
				}
												if (!StringUtils.isEmpty(shopId)) {
					wrapper.eq("shop_id", shopId);
				}
												if (!StringUtils.isEmpty(content)) {
					wrapper.eq("content", content);
				}
												if (!StringUtils.isEmpty(status)) {
					wrapper.eq("status", status);
				}
												if (!StringUtils.isEmpty(url)) {
					wrapper.eq("url", url);
				}
												if (!StringUtils.isEmpty(deleteFlag)) {
					wrapper.eq("delete_flag", deleteFlag);
				}
												if (!StringUtils.isEmpty(gmtCreate)) {
					wrapper.eq("gmt_create", gmtCreate);
				}
												if (!StringUtils.isEmpty(gmtUpdate)) {
					wrapper.eq("gmt_update", gmtUpdate);
				}
												if (!StringUtils.isEmpty(userId)) {
					wrapper.eq("user_id", userId);
				}
												if (!StringUtils.isEmpty(adminId)) {
					wrapper.eq("admin_id", adminId);
				}
							wrapper.eq("delete_flag", 0);
		List<MarketCircleInfo> list = marketCircleInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
		for(MarketCircleInfo tmp : list){
			tmp.setShopName(marketShopInfoMapper.selectById(tmp.getShopId()).getName());
		}
		Integer count = marketCircleInfoMapper.selectCount(wrapper);
		return new Page<MarketCircleInfo>(list, page, limit, count);
	}

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

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

	@Override
	public MarketCircleInfo get(Long id) throws ServiceException {
		return marketCircleInfoMapper.selectById(id);
	}
	
	@Override
	public String export(Long companyId,Long shopId,String content,String status,String url,Integer deleteFlag,Date gmtCreate,Date gmtUpdate,Long userId,Long adminId, Integer page, Integer limit)throws ServiceException {
		Wrapper<MarketCircleInfo> wrapper = new EntityWrapper<MarketCircleInfo>();
														if (!StringUtils.isEmpty(companyId)) {
					wrapper.eq("company_id", companyId);
				}
												if (!StringUtils.isEmpty(shopId)) {
					wrapper.eq("shop_id", shopId);
				}
												if (!StringUtils.isEmpty(content)) {
					wrapper.eq("content", content);
				}
												if (!StringUtils.isEmpty(status)) {
					wrapper.eq("status", status);
				}
												if (!StringUtils.isEmpty(url)) {
					wrapper.eq("url", url);
				}
												if (!StringUtils.isEmpty(deleteFlag)) {
					wrapper.eq("delete_flag", deleteFlag);
				}
												if (!StringUtils.isEmpty(gmtCreate)) {
					wrapper.eq("gmt_create", gmtCreate);
				}
												if (!StringUtils.isEmpty(gmtUpdate)) {
					wrapper.eq("gmt_update", gmtUpdate);
				}
												if (!StringUtils.isEmpty(userId)) {
					wrapper.eq("user_id", userId);
				}
												if (!StringUtils.isEmpty(adminId)) {
					wrapper.eq("admin_id", adminId);
				}
							List<MarketCircleInfo> list = marketCircleInfoMapper.selectList(wrapper);
		ExcelUtil<MarketCircleInfo> util = new ExcelUtil<MarketCircleInfo>(MarketCircleInfo.class);
		return util.exportExcel(list, "操作日志");
	}
}
