package com.iotechn.unimall.admin.api.shop.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.shop.ShopLogMapper;
import com.iotechn.unimall.data.domain.shop.ShopLog;
import com.iotechn.unimall.admin.api.shop.IShopLogService;
import com.iotechn.unimall.data.model.Page;

import java.util.Date;

import org.springframework.transaction.annotation.Transactional;

/**
 * 同步日志Service业务层处理
 *
 * @author jlb
 * @date 2022-06-08
 */
@Service
public class ShopLogServiceImpl implements IShopLogService {
    @Autowired
    private ShopLogMapper shopLogMapper;

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

    @Override
    public Page<ShopLog> list(String title, String content, Long deleteFlag, Date gmtCreate, Date gmtUpdate, Long userId, Long adminId, Integer page, Integer limit) throws ServiceException {
        Wrapper<ShopLog> wrapper = new EntityWrapper<ShopLog>();
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(content)) {
            wrapper.like("content", content);
        }
        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);
		wrapper.orderBy("gmt_create", false);
        List<ShopLog> list = shopLogMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = shopLogMapper.selectCount(wrapper);
        return new Page<ShopLog>(list, page, limit, count);
    }

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

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

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

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