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

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.MarketAdvertisementInfoMapper;
import com.iotechn.unimall.data.domain.market.MarketAdvertisementInfo;
import com.iotechn.unimall.admin.api.market.IMarketAdvertisementInfoService;
import com.iotechn.unimall.data.model.Page;

import java.util.Date;

import org.springframework.transaction.annotation.Transactional;

/**
 * 广告信息Service业务层处理
 *
 * @author jlb
 * @date 2022-08-01
 */
@Service
public class MarketAdvertisementInfoServiceImpl implements IMarketAdvertisementInfoService {
    @Autowired
    private MarketAdvertisementInfoMapper marketAdvertisementInfoMapper;

    @Override
    public Boolean add(MarketAdvertisementInfo marketAdvertisementInfo, Long adminId) throws ServiceException {
        Date now = new Date();
        marketAdvertisementInfo.setGmtCreate(now);
        marketAdvertisementInfo.setGmtUpdate(now);
        marketAdvertisementInfo.setAdminId(adminId);
        return marketAdvertisementInfoMapper.insert(marketAdvertisementInfo) > 0;
    }

    @Override
    public Page<MarketAdvertisementInfo> list(Long companyId, String advertisementName, String url, String status, Integer deleteFlag, Date gmtCreate, Date gmtUpdate, Long userId, Long adminId, Integer page, Integer limit) throws ServiceException {
        Wrapper<MarketAdvertisementInfo> wrapper = new EntityWrapper<MarketAdvertisementInfo>();
        if (!StringUtils.isEmpty(companyId)) {
            wrapper.eq("company_id", companyId);
        }
        if (!StringUtils.isEmpty(advertisementName)) {
            wrapper.eq("advertisement_name", advertisementName);
        }
        if (!StringUtils.isEmpty(url)) {
            wrapper.eq("url", url);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        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<MarketAdvertisementInfo> list = marketAdvertisementInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = marketAdvertisementInfoMapper.selectCount(wrapper);
        return new Page<MarketAdvertisementInfo>(list, page, limit, count);
    }

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

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

    @Override
    public MarketAdvertisementInfo get(Long id) throws ServiceException {
        return marketAdvertisementInfoMapper.selectById(id);
    }

    @Override
    public String export(Long companyId, String advertisementName, String url, String status, Integer deleteFlag, Date gmtCreate, Date gmtUpdate, Long userId, Long adminId, Integer page, Integer limit) throws ServiceException {
        Wrapper<MarketAdvertisementInfo> wrapper = new EntityWrapper<MarketAdvertisementInfo>();
        if (!StringUtils.isEmpty(companyId)) {
            wrapper.eq("company_id", companyId);
        }
        if (!StringUtils.isEmpty(advertisementName)) {
            wrapper.eq("advertisement_name", advertisementName);
        }
        if (!StringUtils.isEmpty(url)) {
            wrapper.eq("url", url);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        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<MarketAdvertisementInfo> list = marketAdvertisementInfoMapper.selectList(wrapper);
        ExcelUtil<MarketAdvertisementInfo> util = new ExcelUtil<MarketAdvertisementInfo>(MarketAdvertisementInfo.class);
        return util.exportExcel(list, "操作日志");
    }
}
