package shop.alien.storeplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.*;
import shop.alien.entity.store.vo.StoreDictionaryVo;
import shop.alien.entity.store.vo.StoreMainInfoVo;
import shop.alien.mapper.*;
import shop.alien.storeplatform.service.StorePlatformRenovationService;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
@RefreshScope
public class StorePlatformRenovationServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements StorePlatformRenovationService {

    private final StoreInfoMapper storeInfoMapper;
    private final StoreLabelMapper storeLabelMapper;
    private final StoreBusinessInfoMapper storeBusinessInfoMapper;
    private final StoreImgMapper storeImgMapper;
    private final StoreDictionaryMapper storeDictionaryMapper;

    @Override
    public List<StoreDictionaryVo> getBusinessSection() {
        List<StoreDictionary> businessSection = storeDictionaryMapper.selectList(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getTypeName, "business_section").ne(StoreDictionary::getDictId, 0));
        List<StoreDictionaryVo> voList = new ArrayList<>();
        for (StoreDictionary storeDictionary : businessSection) {
            StoreDictionaryVo vo = new StoreDictionaryVo();
            BeanUtils.copyProperties(storeDictionary, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<StoreDictionaryVo> getBusinessSectionTypes(String parentId) {
        StoreDictionary businessSection = storeDictionaryMapper.selectOne(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getTypeName, "business_section").eq(StoreDictionary::getDictId, parentId));
        List<StoreDictionary> storeDictionaries = storeDictionaryMapper.selectList(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getParentId, businessSection.getId()));
        List<StoreDictionaryVo> voList = new ArrayList<>();
        for (StoreDictionary storeDictionary : storeDictionaries) {
            StoreDictionaryVo vo = new StoreDictionaryVo();
            BeanUtils.copyProperties(storeDictionary, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public StoreMainInfoVo getDecorationDetail(Integer id) {
        StoreInfo storeInfo = storeInfoMapper.selectById(id);
        StoreMainInfoVo storeMainInfoVo = storeInfoMapper.getStoreInfo(id);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (storeMainInfoVo.getExpirationTime() != null) {
            String expirationDate = sdf.format(storeMainInfoVo.getExpirationTime());
            storeMainInfoVo.setExpirationDate(expirationDate);
        }
        //审核通过给前台反显未提交
        if (storeMainInfoVo.getRenewContractStatus() == 1) {
            storeMainInfoVo.setRenewContractStatus(0);
        }
        if (storeMainInfoVo.getStoreStatus() == -1) {
            LocalDateTime localDateTime = storeMainInfoVo.getLogoutTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            LocalDateTime future = localDateTime.plusDays(7);
            LocalDateTime now = LocalDateTime.now();
            Duration duration = Duration.between(now, future);
            long correct = duration.toMillis();
            storeMainInfoVo.setCountdown(correct);
        }
        //存入门店地址
        storeMainInfoVo.setStoreAddress(storeInfo.getStoreAddress());
        //经营种类
        if (storeInfo.getBusinessTypes() != null) {
            String[] strings = storeInfo.getBusinessTypes().split(",");
            storeMainInfoVo.setBusinessTypesList(Arrays.stream(strings).collect(Collectors.toList()));
        }
        // 分类
        if (storeInfo.getBusinessClassify() != null) {
            String[] strings = storeInfo.getBusinessClassify().split(",");
            storeMainInfoVo.setBusinessClassifyList(Arrays.stream(strings).collect(Collectors.toList()));
        }
        //门店标签
        storeMainInfoVo.setStoreLabel(storeLabelMapper.selectOne(new LambdaQueryWrapper<StoreLabel>().eq(StoreLabel::getStoreId, id)));
        //营业时间
        List<StoreBusinessInfo> storeBusinessInfoList = storeBusinessInfoMapper.selectList(new LambdaQueryWrapper<StoreBusinessInfo>().eq(StoreBusinessInfo::getStoreId, id));
        //storeMainInfoVo.setStoreBusinessInfo(storeBusinessInfoList);
        //营业执照
        storeMainInfoVo.setBusinessLicenseList(storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getImgType, 14).eq(StoreImg::getStoreId, id)));
        //合同照片
        storeMainInfoVo.setContractList(storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getImgType, 15).eq(StoreImg::getStoreId, id)));
        //经营许可证图片照片
        storeMainInfoVo.setFoodLicenceList(storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getImgType, 25).eq(StoreImg::getStoreId, id)));
        //是否连锁
        String isChain = (storeInfo.getIsChain() == 0) ? "否" : "是";
        storeMainInfoVo.setIsChainStr(isChain);
        storeMainInfoVo.setFoodLicenceExpirationTime(storeInfo.getFoodLicenceExpirationTime());
        return storeMainInfoVo;
    }

}
