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.MarketOrderDetail;
import com.iotechn.unimall.data.mapper.market.MarketOrderDetailMapper;
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;

/**
 * 订单详情Service业务层处理
 *
 * @author jlb
 * @date 2022-08-01
 */
@Service
public class MarketOrderDetailAppServiceImpl implements IMarketOrderDetailAppService {
    @Autowired
    private MarketOrderDetailMapper marketOrderDetailMapper;

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

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

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

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

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

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