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

import java.util.List;

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

import java.util.Date;

import org.springframework.transaction.annotation.Transactional;

/**
 * 商店信息Service业务层处理
 *
 * @author jlb
 * @date 2022-08-01
 */
@Service
public class MarketShopInfoServiceImpl implements IMarketShopInfoService {
    @Autowired
    private MarketShopInfoMapper marketShopInfoMapper;


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


    @Override
    public Page<MarketShopInfo> list(Long companyId, String name, String postion, String url, String phone, String status, Integer deleteFlag, Date gmtCreate, Date gmtUpdate, Long userId, Long adminId, String level, String caseWorld, String lon, String lat, Integer page, Integer limit) throws ServiceException {
        Wrapper<MarketShopInfo> wrapper = new EntityWrapper<MarketShopInfo>();
        if (!StringUtils.isEmpty(companyId)) {
            wrapper.eq("company_id", companyId);
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.eq("name", name);
        }
        if (!StringUtils.isEmpty(postion)) {
            wrapper.eq("postion", postion);
        }
        if (!StringUtils.isEmpty(url)) {
            wrapper.eq("url", url);
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.eq("phone", phone);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(caseWorld)) {
            wrapper.eq("caseWorld", caseWorld);
        }
        if (!StringUtils.isEmpty(lon)) {
            wrapper.eq("lon", lon);
        }
        if (!StringUtils.isEmpty(lat)) {
            wrapper.eq("lat", lat);
        }

        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<MarketShopInfo> list = marketShopInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = marketShopInfoMapper.selectCount(wrapper);
        return new Page<MarketShopInfo>(list, page, limit, count);
    }

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

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

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

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