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

import java.util.List;

import com.iotechn.unimall.admin.api.market.IMarketOrderInfoService;
import com.iotechn.unimall.data.domain.market.MarketOrderInfo;
import com.iotechn.unimall.data.mapper.market.MarketOrderInfoMapper;
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 MarketOrderInfoServiceImpl implements IMarketOrderInfoService {
    @Autowired
    private MarketOrderInfoMapper marketOrderInfoMapper;

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

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

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

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

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

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