package com.yc.ship.module.marketing.service.couponProduct;

import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.marketing.controller.admin.couponProduct.vo.CouponProductRespVo;
import com.yc.ship.module.marketing.controller.admin.couponProduct.vo.CouponProductSaveReqVo;
import com.yc.ship.module.marketing.controller.admin.couponProduct.vo.RelationReqVo;
import com.yc.ship.module.marketing.controller.admin.couponProduct.vo.RelationRespVo;
import com.yc.ship.module.marketing.dal.dataobject.couponPorduct.CouponProductDo;
import com.yc.ship.module.marketing.dal.mysql.coupon.CouponMapper;
import com.yc.ship.module.marketing.dal.mysql.couponProduct.CouponProductMapper;
import com.yc.ship.module.marketing.enums.coupon.CouponProductTypeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.marketing.enums.ErrorCodeConstants.COUPON_NOT_EXISTS;


/**
 * 优惠券关联产品 Service 实现类
 *
 * @author lfy
 */
@Service
@Validated
public class CouponProductServiceImpl implements CouponProductService {

    @Resource
    private CouponMapper couponMapper;

    @Resource
    private CouponProductMapper couponProductMapper;

    //TODO:待完善
//    @Resource
//    private ProductApi productApi;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createCouponProduct(CouponProductSaveReqVo reqVO) {
        Long couponId = reqVO.getCouponId();
        validateCouponExists(couponId);
        Integer type = reqVO.getType();


        //删除之前的记录
        couponProductMapper.deleteByCouponId(couponId);
        //再新增现在的
        if(CouponProductTypeEnum.ALL.getType().equals(type)) {
            CouponProductDo couponProductDo = BeanUtils.toBean(reqVO, CouponProductDo.class);
            couponProductMapper.insert(couponProductDo);
        }else if(CouponProductTypeEnum.CATEGORY.getType().equals(type)){
            List<Long> relationIds = reqVO.getRelationIds();
            List<CouponProductDo> list = new ArrayList<>();
            relationIds.stream().forEach(relationId -> {
                CouponProductDo couponProductDo = new CouponProductDo();
                couponProductDo.setCouponId(couponId);
                couponProductDo.setRelationId(relationId);
                couponProductDo.setType(type);
                list.add(couponProductDo);
            });
            couponProductMapper.insertBatch(list);
        }else if(CouponProductTypeEnum.CUSTOM.getType().equals(type)){
            List<RelationReqVo> relationList = reqVO.getRelationList();
            List<CouponProductDo> list = new ArrayList<>();
            relationList.stream().forEach(relation -> {
                CouponProductDo couponProductDo = new CouponProductDo();
                couponProductDo.setCouponId(couponId);
                couponProductDo.setRelationId(relation.getRelationId());
                couponProductDo.setRelationChildId(relation.getRelationChildId());
                couponProductDo.setRelationSkuId(String.join(",",relation.getRelationSkuIds()));
                couponProductDo.setType(type);
                list.add(couponProductDo);
            });
            couponProductMapper.insertBatch(list);
        }
        return true;
    }

    @Override
    public CouponProductRespVo getCouponProductByCouponId(Long couponId) {
        validateCouponExists(couponId);
        List<CouponProductDo> list = couponProductMapper.selectList(CouponProductDo::getCouponId, couponId);
        CouponProductRespVo vo = new CouponProductRespVo();
        if(list != null && list.size() > 0) {
            CouponProductDo couponProductDo = list.get(0);
            vo.setCouponId(couponProductDo.getCouponId());
            vo.setType(couponProductDo.getType());
            if(CouponProductTypeEnum.CATEGORY.getType().equals(couponProductDo.getType())) {
                List<Long> relationIds = CollectionUtils.convertList(list, CouponProductDo::getRelationId);
                vo.setRelationIds(relationIds);
            }else if(CouponProductTypeEnum.CUSTOM.getType().equals(couponProductDo.getType())) {
                List<RelationRespVo> relationList = couponProductMapper.selectRelationProductList(couponId);
                relationList.stream().forEach(item -> item.setRelationSkuIds(Arrays.asList(item.getRelationSkuIdsStr().split(","))));
                vo.setRelationList(relationList);
            }
            return vo;
        }
        return null;
    }

    @Override
    public List<CouponProductDo> getList(List<Long> couponIds) {
        if(couponIds.isEmpty()) {
            return Collections.emptyList();
        }
        return couponProductMapper.selectBatchIds(couponIds);
    }

    @Override
    public List<CouponProductDo> getListByCouponIds(List<Long> couponIds) {
        if(couponIds.isEmpty()) {
            return Collections.emptyList();
        }
        return couponProductMapper.selectListByCouponIds(couponIds);
    }

    private void validateCouponExists(Long id) {
        if (couponMapper.selectById(id) == null) {
            throw exception(COUPON_NOT_EXISTS);
        }
    }
}