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

import java.util.List;

import com.iotechn.unimall.admin.api.market.IMarketProdectInfoService;
import com.iotechn.unimall.data.domain.market.MarketProdectInfo;
import com.iotechn.unimall.data.mapper.market.MarketProdectInfoMapper;
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 MarketProdectInfoServiceImpl implements IMarketProdectInfoService {
    @Autowired
    private MarketProdectInfoMapper marketProdectInfoMapper;

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

    @Override
    public Page<MarketProdectInfo> list(Long companyId, String prodectName, String classificationKey, String classification, String url, String productDetails, Double commodityPrice, String status, Integer deleteFlag, Date gmtCreate, Date gmtUpdate, Long userId, Long adminId, Integer page, Integer limit) throws ServiceException {
        Wrapper<MarketProdectInfo> wrapper = new EntityWrapper<MarketProdectInfo>();
        if (!StringUtils.isEmpty(companyId)) {
            wrapper.eq("company_id", companyId);
        }
        if (!StringUtils.isEmpty(prodectName)) {
            wrapper.eq("prodect_name", prodectName);
        }
        if (!StringUtils.isEmpty(classificationKey)) {
            wrapper.eq("classification_key", classificationKey);
        }
        if (!StringUtils.isEmpty(classification)) {
            wrapper.eq("classification", classification);
        }
        if (!StringUtils.isEmpty(url)) {
            wrapper.eq("url", url);
        }
        if (!StringUtils.isEmpty(productDetails)) {
            wrapper.eq("product_details", productDetails);
        }
        if (!StringUtils.isEmpty(commodityPrice)) {
            wrapper.eq("commodity price", commodityPrice);
        }
        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<MarketProdectInfo> list = marketProdectInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = marketProdectInfoMapper.selectCount(wrapper);
        return new Page<MarketProdectInfo>(list, page, limit, count);
    }

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

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

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

    @Override
    public String export(Long companyId, String prodectName, String classificationKey, String classification, String url, String productDetails, Double commodityPrice, String status, Integer deleteFlag, Date gmtCreate, Date gmtUpdate, Long userId, Long adminId, Integer page, Integer limit) throws ServiceException {
        Wrapper<MarketProdectInfo> wrapper = new EntityWrapper<MarketProdectInfo>();
        if (!StringUtils.isEmpty(companyId)) {
            wrapper.eq("company_id", companyId);
        }
        if (!StringUtils.isEmpty(prodectName)) {
            wrapper.eq("prodect_name", prodectName);
        }
        if (!StringUtils.isEmpty(classificationKey)) {
            wrapper.eq("classification_key", classificationKey);
        }
        if (!StringUtils.isEmpty(classification)) {
            wrapper.eq("classification", classification);
        }
        if (!StringUtils.isEmpty(url)) {
            wrapper.eq("url", url);
        }
        if (!StringUtils.isEmpty(productDetails)) {
            wrapper.eq("product_details", productDetails);
        }
        if (!StringUtils.isEmpty(commodityPrice)) {
            wrapper.eq("commodity price", commodityPrice);
        }
        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<MarketProdectInfo> list = marketProdectInfoMapper.selectList(wrapper);
        ExcelUtil<MarketProdectInfo> util = new ExcelUtil<MarketProdectInfo>(MarketProdectInfo.class);
        return util.exportExcel(list, "操作日志");
    }
}
