package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.vo.StoreSalesDetailsVo;
import shop.alien.mapper.StoreSalesDetailsMapper;
import shop.alien.store.service.StoreSalesDetailsService;

import java.time.ZoneId;
import java.util.Date;

@Service
@RequiredArgsConstructor
public class StoreSalesDetailsServiceImpl implements StoreSalesDetailsService {

    private final StoreSalesDetailsMapper storeSalesDetailsMapper;

    @Override
    public IPage<StoreSalesDetailsVo> getSalesDetails(StoreSalesDetailsVo storeSalesDetailsVo) {
        IPage<StoreSalesDetailsVo> ipage = new Page<>(storeSalesDetailsVo.getPage(), storeSalesDetailsVo.getSize());
        QueryWrapper<StoreSalesDetailsVo> wrapper = new QueryWrapper<>();
        wrapper.eq("lorder.delete_flag", 0);
        wrapper.eq("coupon.delete_flag", 0);
        wrapper.eq("luser.delete_flag", 0);
        wrapper.eq(null != storeSalesDetailsVo.getStoreId(), "lorder.store_id", storeSalesDetailsVo.getStoreId());
        wrapper.in(CollectionUtils.isNotEmpty(storeSalesDetailsVo.getTypeList()), "coupon.type", storeSalesDetailsVo.getTypeList());
        wrapper.ge(null != storeSalesDetailsVo.getPayStartTime(), "lorder.pay_time", storeSalesDetailsVo.getPayStartTime());
        if (null != storeSalesDetailsVo.getPayEndTime()) {
            wrapper.le(null != storeSalesDetailsVo.getPayEndTime(), "lorder.pay_time", storeSalesDetailsVo.getPayEndTime());
        }else{
            wrapper.le("lorder.pay_time", Date.from(storeSalesDetailsVo.getPayEndTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().plusHours(23).plusMinutes(59).plusSeconds(59).atZone(ZoneId.systemDefault()).toInstant()));
        }
        wrapper.eq(StringUtils.isNotEmpty(storeSalesDetailsVo.getStatus()), "lorder.status", storeSalesDetailsVo.getStatus());
        wrapper.like(StringUtils.isNotEmpty(storeSalesDetailsVo.getOrderNo()), "lorder.order_no", storeSalesDetailsVo.getOrderNo());
        wrapper.in(CollectionUtils.isNotEmpty(storeSalesDetailsVo.getCouponNameList()), "coupon.name", storeSalesDetailsVo.getCouponNameList());
        wrapper.orderByDesc("lorder.pay_time");
        return storeSalesDetailsMapper.getSalesDetails(ipage, wrapper);
    }

    @Override
    public IPage<StoreSalesDetailsVo> getCouponDetails(StoreSalesDetailsVo storeSalesDetailsVo) {
        IPage<StoreSalesDetailsVo> ipage = new Page<>(storeSalesDetailsVo.getPage(), storeSalesDetailsVo.getSize());
        QueryWrapper<StoreSalesDetailsVo> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_flag", 0);
        wrapper.eq(null != storeSalesDetailsVo.getType(), "type", storeSalesDetailsVo.getType());
        wrapper.apply(StringUtils.isNotEmpty(storeSalesDetailsVo.getCouponCode()), "coupon_code like {0} or name like {0}", "%" + storeSalesDetailsVo.getCouponCode() + "%");
        wrapper.orderByDesc("created_time");
        return storeSalesDetailsMapper.getCouponDetails(ipage, wrapper);
    }
}
