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

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.GoblinZhengzaiStoreDetailsDto;
import com.liquidnet.service.goblin.dto.GoblinZhengzaiStoreListDto;
import com.liquidnet.service.goblin.dto.vo.GoblinMarketingZhengzaiRelationVo;
import com.liquidnet.service.goblin.dto.vo.GoblinSelfMarketingVo;
import com.liquidnet.service.goblin.dto.vo.GoblinZhengzaiStoreListVo;
/*import com.liquidnet.service.goblin.dto.vo.GoblinZhengzaiVo;*/
import com.liquidnet.service.goblin.entity.GoblinMarketingZhengzaiRelation;
import com.liquidnet.service.goblin.entity.GoblinSelfMarketing;
import com.liquidnet.service.goblin.mapper.GoblinMarketingZhengzaiRelationMapper;
import com.liquidnet.service.goblin.mapper.GoblinSelfMarketingMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
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;
    @Autowired
    GoblinMarketingZhengzaiRelationMapper goblinMarketingZhengzaiRelationMapper;

    @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();
        if (status == -1) {
            selfMarketing.setStatus(0);
            selfMarketing.setDelFlag(1);
            selfMarketing.setUpdatedAt(LocalDateTime.now());
            //mysql
            goblinSelfMarketingMapper.update(selfMarketing, Wrappers.lambdaUpdate(GoblinSelfMarketing.getNew()).eq(GoblinSelfMarketing::getSelfMarketId, marketId));
            //mongo
            goblinMongoUtils.delSelfMarket(marketId);
            //redis
            goblinRedisUtils.delSelfMarket(marketId);
        } else {
            selfMarketing.setStatus(status);
            selfMarketing.setUpdatedAt(LocalDateTime.now());
            //mysql
            goblinSelfMarketingMapper.update(selfMarketing, Wrappers.lambdaUpdate(GoblinSelfMarketing.getNew()).eq(GoblinSelfMarketing::getSelfMarketId, marketId));
            //mongo
            GoblinSelfMarketingVo vo = GoblinSelfMarketingVo.getNew();
            BeanUtils.copyProperties(selfMarketing, vo);
            goblinMongoUtils.updateSelfMarket(marketId, vo);
            //redis
            goblinRedisUtils.setSelfMarket(marketId, vo);
        }
        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));
        selfMarketing.setCreatedAt(LocalDateTime.now());
        //mysql
        goblinSelfMarketingMapper.insert(selfMarketing);
        //mongo
        GoblinSelfMarketingVo vo = GoblinSelfMarketingVo.getNew();
        BeanUtils.copyProperties(selfMarketing, vo);
        vo.setStartTime(st);
        vo.setEndTime(et);
        vo.setCreatedAt(DateUtil.Formatter.yyyyMMddHHmmss.format(selfMarketing.getCreatedAt()));
        goblinMongoUtils.insertSelfMarket(vo);
        //redis
        goblinRedisUtils.setSelfMarket(SelfMarketId, vo);
        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
        GoblinSelfMarketingVo vo = GoblinSelfMarketingVo.getNew();
        BeanUtils.copyProperties(selfMarketing, vo);
        vo.setStartTime(st);
        vo.setEndTime(et);
        goblinMongoUtils.updateSelfMarket(marketId, vo);
        //redis
        goblinRedisUtils.setSelfMarket(marketId, vo);
        return ResponseDto.success();
    }

    @Override
    public ResponseDto<GoblinSelfMarketingVo> zhengzaiDetails(String marketId) {
        GoblinSelfMarketing bean = goblinSelfMarketingMapper.selectOne(Wrappers.lambdaUpdate(GoblinSelfMarketing.getNew()).eq(GoblinSelfMarketing::getSelfMarketId, marketId));
        GoblinSelfMarketingVo vo = GoblinSelfMarketingVo.getNew();
        BeanUtils.copyProperties(bean, vo);
        vo.setStartTime(DateUtil.Formatter.yyyyMMddHHmmss.format(bean.getStartTime()));
        vo.setEndTime(DateUtil.Formatter.yyyyMMddHHmmss.format(bean.getEndTime()));
        vo.setCreatedAt(DateUtil.Formatter.yyyyMMddHHmmss.format(bean.getCreatedAt()));
        return ResponseDto.success(vo);
    }

    /*@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<List<GoblinZhengzaiStoreListVo>> zhengzaiStoreList(String marketId) {
        List<GoblinZhengzaiStoreListDto> dtoList = goblinSelfMarketingMapper.getZhengzaiStoreList(marketId);
        List<GoblinZhengzaiStoreListVo> voList = new ArrayList<>();
        for (GoblinZhengzaiStoreListDto item : dtoList) {
            GoblinZhengzaiStoreListVo vo = GoblinZhengzaiStoreListVo.getNew();
            vo.setStoreId(item.getStoreId());
            vo.setStoreName(item.getStoreName());
            vo.setSpuCount(item.getSpuCount());
            vo.setSkuCount(item.getSkuCount());
        }
        return ResponseDto.success(voList);
    }

    @Override
    public ResponseDto<Boolean> zhengzaiStore(String marketId, String storeId, String showTime) {
        GoblinMarketingZhengzaiRelation entity = GoblinMarketingZhengzaiRelation.getNew();
        entity.setSelfMarketId(marketId);
        entity.setStoreId(storeId);
        entity.setShowTime(LocalDateTime.parse(showTime, DTF_YMD_HMS));
        //mysql
        goblinMarketingZhengzaiRelationMapper.insert(entity);
        //mongo
        GoblinMarketingZhengzaiRelationVo vo = GoblinMarketingZhengzaiRelationVo.getNew();
        BeanUtils.copyProperties(entity, vo);
        vo.setShowTime(showTime);
        vo.setCreatedAt(DateUtil.Formatter.yyyyMMddHHmmss.format(entity.getCreatedAt()));
        goblinMongoUtils.insertZhengzaiRelation(vo);
        //redis
        goblinRedisUtils.setZhengzaiRelation(marketId, vo);
        return ResponseDto.success();
    }

    @Override
    public ResponseDto<Boolean> zhengzaiStore(String marketId, String storeId, String showTime, Integer delTag) {
        if (delTag == 0) {
            GoblinMarketingZhengzaiRelation entity = GoblinMarketingZhengzaiRelation.getNew();
            entity.setShowTime(LocalDateTime.parse(showTime, DTF_YMD_HMS));
            entity.setUpdatedAt(LocalDateTime.now());
            //mysql
            goblinMarketingZhengzaiRelationMapper.update(entity, Wrappers.lambdaUpdate(GoblinMarketingZhengzaiRelation.getNew()).eq(GoblinMarketingZhengzaiRelation::getSelfMarketId, marketId).eq(GoblinMarketingZhengzaiRelation::getStoreId, storeId));
            //mongo
            GoblinMarketingZhengzaiRelationVo vo = GoblinMarketingZhengzaiRelationVo.getNew();
            BeanUtils.copyProperties(entity, vo);
            vo.setShowTime(showTime);
            goblinMongoUtils.updateZhengzaiRelation(marketId, storeId, vo);
            //redis
            goblinRedisUtils.setZhengzaiRelation(marketId, vo);
        } else if (delTag == 1) {
            GoblinMarketingZhengzaiRelation entity = GoblinMarketingZhengzaiRelation.getNew();
            entity.setDelTag(1);
            entity.setUpdatedAt(LocalDateTime.now());
            //mysql
            goblinMarketingZhengzaiRelationMapper.update(entity, Wrappers.lambdaUpdate(GoblinMarketingZhengzaiRelation.getNew()).eq(GoblinMarketingZhengzaiRelation::getSelfMarketId, marketId).eq(GoblinMarketingZhengzaiRelation::getStoreId, storeId));
            //mongo
            goblinMongoUtils.delZhengzaiRelation(marketId, storeId);
            //redis
            goblinRedisUtils.delZhengzaiRelation(marketId, storeId);
        }
        return ResponseDto.success();
    }

    @Override
    public ResponseDto<String> zhengzaiSkuList(String marketId, String storeId) {
        List<GoblinZhengzaiStoreDetailsDto> dtoList = goblinSelfMarketingMapper.getZhengzaiStoreDetails(marketId, storeId);
        return null;
    }

    @Override
    public ResponseDto<Boolean> zhengzaiSkuDel(String marketId, String skuId) {
        //调用goblin商铺配置商铺接口
        return null;
    }
}
