package com.ycwl.basic.biz;

import com.ycwl.basic.mapper.CouponMapper;
import com.ycwl.basic.mapper.CouponRecordMapper;
import com.ycwl.basic.model.pc.coupon.entity.CouponEntity;
import com.ycwl.basic.model.pc.couponRecord.entity.CouponRecordEntity;
import com.ycwl.basic.model.pc.couponRecord.resp.CouponRecordQueryResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Component
public class CouponBiz {
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private CouponRecordMapper couponRecordMapper;

    public CouponRecordQueryResp queryUserCouponRecord(Long scenicId, Long memberId, Long faceId, String goodsId) {
        CouponRecordQueryResp resp = new CouponRecordQueryResp();
        List<CouponRecordEntity> recordList = couponRecordMapper.queryByUserWithGoodsId(scenicId, memberId, goodsId);
        if (recordList != null && !recordList.isEmpty()) {
            Optional<CouponRecordEntity> record = recordList.stream().filter(item -> item.getStatus() == 0).filter(item -> item.getFaceId() == null || item.getFaceId().equals(faceId)).findAny();
            if (record.isPresent()) {
                CouponRecordEntity recordEntity = record.get();
                resp.setExist(true);
                resp.setId(recordEntity.getId());
                resp.setCouponId(recordEntity.getCouponId());
                CouponEntity coupon = couponMapper.getById(recordEntity.getCouponId());
                if (coupon != null) {
                    resp.setMemberId(recordEntity.getMemberId());
                    resp.setFaceId(recordEntity.getFaceId());
                    resp.setStatus(recordEntity.getStatus());
                    resp.setCreateTime(recordEntity.getCreateTime());
                    resp.setUsedTime(recordEntity.getUsedTime());
                    resp.setUsedOrderId(recordEntity.getUsedOrderId());
                    resp.setCoupon(coupon);
                } else {
                    resp.setExist(false);
                }
            } else {
                Optional<CouponRecordEntity> usedRecord = recordList.stream().filter(item -> item.getStatus() != 0).filter(item -> item.getFaceId() == null || item.getFaceId().equals(faceId)).findAny();
                if (usedRecord.isPresent()) {
                    CouponRecordEntity recordEntity = usedRecord.get();
                    resp.setExist(true);
                    resp.setId(recordEntity.getId());
                    resp.setCouponId(recordEntity.getCouponId());
                    CouponEntity coupon = couponMapper.getById(recordEntity.getCouponId());
                    if (coupon != null) {
                        resp.setMemberId(recordEntity.getMemberId());
                        resp.setFaceId(recordEntity.getFaceId());
                        resp.setStatus(recordEntity.getStatus());
                        resp.setCreateTime(recordEntity.getCreateTime());
                        resp.setUsedTime(recordEntity.getUsedTime());
                        resp.setUsedOrderId(recordEntity.getUsedOrderId());
                        resp.setCoupon(coupon);
                    } else {
                        resp.setExist(false);
                    }
                }
            }
        }
        return resp;
    }

    public boolean userGetCoupon(Long memberId, Long faceId, Integer couponId) {
        CouponEntity coupon = couponMapper.getById(couponId);
        if (coupon == null) {
            return false;
        }
        CouponRecordEntity entity = new CouponRecordEntity();
        entity.setCouponId(couponId);
        entity.setFaceId(faceId);
        entity.setMemberId(memberId);
        entity.setStatus(0);
        entity.setCreateTime(new Date());
        return couponRecordMapper.insert(entity) > 0;
    }

    public boolean userUseCoupon(Long memberId, Long faceId, Integer couponRecordId, Long orderId) {
        CouponRecordEntity entity = new CouponRecordEntity();
        entity.setId(couponRecordId);
        entity.setStatus(1);
        entity.setUsedTime(new Date());
        entity.setUsedOrderId(orderId);
        return couponRecordMapper.updateById(entity) > 0;
    }
}
