package shop.alien.second.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import shop.alien.entity.second.vo.SecondGoodsVo;
import shop.alien.entity.second.vo.SecondUserViolationDetailVo;
import shop.alien.entity.second.vo.SecondUserViolationVo;
import shop.alien.entity.store.LifeUserViolation;
import shop.alien.entity.store.StoreImg;
import shop.alien.mapper.LifeUserViolationMapper;
import shop.alien.mapper.StoreImgMapper;
import shop.alien.mapper.second.SecondGoodsMapper;
import shop.alien.mapper.second.SecondUserViolationMapper;
import shop.alien.second.service.PlatformUserViolationService;
import shop.alien.second.service.SecondGoodsService;
import shop.alien.util.common.Constants;

import java.util.*;

@Slf4j
@Service
public class PlatformUserViolationServiceImpl extends ServiceImpl<SecondUserViolationMapper, SecondUserViolationVo> implements PlatformUserViolationService {

    List<String> videoFileType = Arrays.asList("mp4", "avi", "flv", "mkv", "rmvb", "wmv", "3gp", "mov");

    @Autowired
    private SecondUserViolationMapper mapper;

    @Autowired
    private LifeUserViolationMapper lifeUserViolationMapper;

    @Autowired
    private SecondGoodsMapper secondGoodsMapper;

    @Autowired
    private StoreImgMapper storeImgMapper;

    @Lazy
    @Autowired
    private SecondGoodsService secondGoodsService;


    @Override
    public IPage<SecondUserViolationVo> getUserViolationByPage(IPage<SecondUserViolationVo> page, String reportingUserName,
                                                               String reportingDate, String processingStatus, String reportContextType) throws Exception {
        try {
            return mapper.getUserViolationByPage(page, reportingUserName, reportingDate, processingStatus, reportContextType);
        } catch (Exception e) {
            log.error("PlatformUserViolationServiceImpl.getUserViolationByPage Error Mgs={}", e.getMessage());
            throw new Exception(e);
        }
    }

    @Override
    public SecondUserViolationDetailVo getUserViolationDetail(Integer id) throws Exception {
        try {
            // 查询举报信息
            SecondUserViolationDetailVo item =mapper.getUserViolationInfo(id);

            // 商品的时候查询商品信息
            if ("4".equals(item.getReportContextType())) {
                SecondGoodsVo secondGoods = secondGoodsService.dealSecondGoodsRecordInfo(item.getBusinessId());
                item.setSecondGoods(secondGoods);
            }

            // 图片list
            List<Map<String, Object>> list = new ArrayList<>();

            // 查询图片信息
            LambdaQueryWrapper<StoreImg> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(StoreImg::getStoreId, item.getId());
            wrapper.eq(StoreImg::getImgType, Constants.ImageType.SECOND_HAND_REPORT);
            List<StoreImg> imgList = storeImgMapper.selectList(wrapper);

            for (StoreImg img : imgList) {
                Map<String, Object> map = new HashMap<>();
                String fileType = img.getImgUrl().substring(img.getImgUrl().lastIndexOf(".") + 1);
                if (videoFileType.contains(fileType.toLowerCase())) {
                    map.put("type", "video");
                } else {
                    map.put("type", "image");
                }
                map.put("imgUrl", img.getImgUrl());
                list.add(map);
            }
            item.setImgList(list);
            return item;
        } catch (Exception e) {
            log.error("PlatformUserViolationServiceImpl.getUserViolationByPage Error Mgs={}", e.getMessage());
            throw new Exception(e);
        }
    }

    @Override
    public int updateUserViolation(LifeUserViolation row) throws Exception {
        LambdaUpdateWrapper<LifeUserViolation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(LifeUserViolation::getId, row.getId())
                .set(LifeUserViolation::getProcessingStatus, row.getProcessingStatus())
                .set(LifeUserViolation::getProcessingTime, new Date())
                .set(LifeUserViolation::getReportResult, row.getReportResult());
        return lifeUserViolationMapper.update(null, updateWrapper);
    }

    private static boolean isVideoUrl(String url) {
        if (url == null) return false;
        url = url.toLowerCase();
        return url.endsWith(".mp4") ||
                url.endsWith(".avi") ||
                url.endsWith(".mov") ||
                url.endsWith(".flv") ||
                url.endsWith(".wmv") ||
                url.endsWith(".mkv");
    }

}
