package com.iotechn.unimall.app.api.market;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.market.MarketCase;
import com.iotechn.unimall.data.domain.market.MarketOrderInfo;
import com.iotechn.unimall.data.mapper.market.MarketCaseMapper;
import com.iotechn.unimall.data.mapper.market.MarketOrderInfoMapper;
import com.iotechn.unimall.data.mapper.market.MarketShopInfoMapper;
import com.iotechn.unimall.data.model.Page;
import com.iotechn.unimall.data.util.ExcelUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

import java.util.List;

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.data.model.Page;
import java.util.Date;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生鲜圈Service业务层处理
 *
 * @author jlb
 * @date 2023-01-03
 */
@Service
public class MarketCircleInfoAppServiceImpl implements IMarketCircleInfoAppService{
    @Autowired
    private MarketCircleInfoMapper marketCircleInfoMapper;
    @Autowired
    private MarketCaseMapper marketCaseMapper;
    @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 region, String status, String url, Integer deleteFlag, Date gmtCreate, Date gmtUpdate, Long userId,Long fllowId, 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(region)) {
            wrapper.like("region", region);
        }
        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<MarketCase> wrapper1 = new EntityWrapper<MarketCase>();
        wrapper1.eq("followers_id", fllowId);
        List<MarketCase> floowList = marketCaseMapper.selectList(wrapper1);

        wrapper.eq("delete_flag", 0);
        wrapper.orderBy("gmt_update",false);
        List<MarketCircleInfo> list = marketCircleInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        for(MarketCircleInfo tmp : list){
            for(MarketCase marketCase : floowList){
                if(String.valueOf(tmp.getUserId()).equals(marketCase.getConcernedId())){
                    tmp.setFlag(true);
                    break;
                }
            }
            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);
    }

   }
