package com.liquidnet.client.admin.zhengzai.goblin.service.imp;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.liquidnet.client.admin.zhengzai.goblin.service.IGoblinZhengzaiMarketService;
import com.liquidnet.client.admin.zhengzai.goblin.utils.GoblinMongoUtils;
import com.liquidnet.client.admin.zhengzai.goblin.utils.GoblinRedisUtils;
import com.liquidnet.commons.lang.util.DateUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.goblin.constant.GoblinStatusConst;
import com.liquidnet.service.goblin.dto.vo.GoblinZhengzaiStoreListVo;
import com.liquidnet.service.goblin.dto.vo.GoblinZhengzaiVo;
import com.liquidnet.service.goblin.entity.GoblinSelfMarketing;
import com.liquidnet.service.goblin.mapper.GoblinSelfMarketingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static com.liquidnet.commons.lang.util.DateUtil.DTF_YMD_HMS;

/**
 * 正在下单相关
 */
@Service
public class GoblinZhengzaiMarketServiceImpl implements IGoblinZhengzaiMarketService {

    @Autowired
    GoblinRedisUtils goblinRedisUtils;
    @Autowired
    GoblinMongoUtils goblinMongoUtils;
    @Autowired
    GoblinSelfMarketingMapper goblinSelfMarketingMapper;

    @Override
    public ResponseDto<List<GoblinSelfMarketing>> zhengzaiList(int page, String purchaseName, int status, String st, String et, String ct) {
        LambdaUpdateWrapper<GoblinSelfMarketing> queryMapper = Wrappers.lambdaUpdate(GoblinSelfMarketing.getNew()).eq(GoblinSelfMarketing::getDelFlag,0);
        switch (status) {
            case 0://未开始
                queryMapper.lt(GoblinSelfMarketing::getStartTime, DateUtil.getNowTime());
                break;
            case 1://进行中
                queryMapper.ge(GoblinSelfMarketing::getStartTime, DateUtil.getNowTime()).le(GoblinSelfMarketing::getEndTime, DateUtil.getNowTime());
                break;
            case 2://已结束
                queryMapper.gt(GoblinSelfMarketing::getEndTime, DateUtil.getNowTime());
                break;
            case 7://停用
                queryMapper.gt(GoblinSelfMarketing::getStatus, GoblinStatusConst.MarketingStatus.STORE_PURCHASE_STATUS_STOP);
                break;
        }

        if (st != null && et != null) {
            queryMapper.ge(GoblinSelfMarketing::getStartTime, st).le(GoblinSelfMarketing::getEndTime, et);
        }
        if (ct != null) {
            queryMapper.eq(GoblinSelfMarketing::getCreatedAt, ct);
        }
        return ResponseDto.success(goblinSelfMarketingMapper.selectList(queryMapper));
    }

    @Override
    public ResponseDto<Boolean> zhengzaiStatus(String marketId, int status) {
        GoblinSelfMarketing selfMarketing = GoblinSelfMarketing.getNew();
        selfMarketing.setStatus(status);
        selfMarketing.setUpdatedAt(LocalDateTime.now());
        //mysql
        goblinSelfMarketingMapper.update(selfMarketing, Wrappers.lambdaUpdate(GoblinSelfMarketing.getNew()).eq(GoblinSelfMarketing::getSelfMarketId, marketId));
        //mongo
        goblinMongoUtils.updateSelfMarket(marketId, selfMarketing);
        //redis
        goblinRedisUtils.setSelfMarket(marketId, selfMarketing);
        return ResponseDto.success();
    }

    @Override
    public ResponseDto<Boolean> zhengzaiInsert(String purchaseName, String st, String et) {
        GoblinSelfMarketing selfMarketing = GoblinSelfMarketing.getNew();
        String SelfMarketId = IDGenerator.nextTimeId2();
        selfMarketing.setSelfMarketId(SelfMarketId);
        selfMarketing.setName(purchaseName);
        selfMarketing.setType(GoblinStatusConst.MarketingStatus.SELF_TYPE_ZHENGZAI.getValue());
        selfMarketing.setStartTime(LocalDateTime.parse(st, DTF_YMD_HMS));
        selfMarketing.setEndTime(LocalDateTime.parse(et, DTF_YMD_HMS));
        //mysql
        goblinSelfMarketingMapper.insert(selfMarketing);
        //mongo
        goblinMongoUtils.insertSelfMarket(selfMarketing);
        //redis
        goblinRedisUtils.setSelfMarket(SelfMarketId, selfMarketing);
        return ResponseDto.success();
    }

    @Override
    public ResponseDto<Boolean> zhengzaiUpdate(String marketId, String purchaseName, String st, String et) {
        GoblinSelfMarketing selfMarketing = GoblinSelfMarketing.getNew();
        selfMarketing.setName(purchaseName);
        selfMarketing.setType(GoblinStatusConst.MarketingStatus.SELF_TYPE_ZHENGZAI.getValue());
        selfMarketing.setStartTime(LocalDateTime.parse(st, DTF_YMD_HMS));
        selfMarketing.setEndTime(LocalDateTime.parse(et, DTF_YMD_HMS));
        selfMarketing.setUpdatedAt(LocalDateTime.now());
        //mysql
        goblinSelfMarketingMapper.update(selfMarketing, Wrappers.lambdaUpdate(GoblinSelfMarketing.getNew()).eq(GoblinSelfMarketing::getSelfMarketId, marketId));
        //mongo
        goblinMongoUtils.updateSelfMarket(marketId, selfMarketing);
        //redis
        goblinRedisUtils.setSelfMarket(marketId, selfMarketing);
        return ResponseDto.success();
    }

    @Override
    public ResponseDto<GoblinZhengzaiVo> zhengzaiDetails(String marketId) {
        GoblinSelfMarketing data = goblinSelfMarketingMapper.selectOne(Wrappers.lambdaQuery(GoblinSelfMarketing.getNew()).eq(GoblinSelfMarketing::getSelfMarketId,marketId));
        GoblinZhengzaiVo vo = GoblinZhengzaiVo.getNew();
        vo.setMarketId(data.getSelfMarketId());
        vo.setName(data.getName());
        vo.setStartTime(DateUtil.Formatter.yyyyMMddHHmmss.format(data.getStartTime()));
        vo.setEndTime(DateUtil.Formatter.yyyyMMddHHmmss.format(data.getEndTime()));
        vo.setStatus(data.getStatus());
        vo.setCreatedAt(DateUtil.Formatter.yyyyMMddHHmmss.format(data.getCreatedAt()));
        return null;
    }

    @Override
    public ResponseDto<String> zhengzaiSkuList(String marketId, String storeId) {
        return null;
    }

    @Override
    public ResponseDto<Boolean> zhengzaiStoreList(String marketId) {
        return null;
    }

    @Override
    public ResponseDto<Boolean> zhengzaiSkuDel(String marketId, String skuId) {
        return null;
    }

    @Override
    public ResponseDto<List<GoblinZhengzaiStoreListVo>> zhengzaiStore(String marketId, String storeId, Integer delTag) {
        return null;
    }
}
