package com.crm.custom.service.impl;

import com.crm.custom.dao.mapper.GoldenGoodsVideoMapper;
import com.crm.custom.dao.repository.GoldenGoodsRepository;
import com.crm.custom.dao.repository.GoldenGoodsVideoRepository;
import com.crm.custom.service.GoldenGoodsService;
import com.crm.custom.service.GoldenGoodsVideoService;
import com.crm.custom.service.impl.base.BaseUploadServiceImpl;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.dto.golden.goods.GoldenGoodsVideoListDto;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.golden.goods.GoldenGoodsVideoAddEntity;
import com.crm.rely.backend.model.entity.golden.goods.GoldenGoodsVideoDeleteEntitys;
import com.crm.rely.backend.model.entity.golden.goods.GoldenGoodsVideoSearchEntity;
import com.crm.rely.backend.model.entity.golden.goods.GoldenGoodsVideoUpdateEntity;
import com.crm.rely.backend.model.pojo.table.GoldenGoodsTable;
import com.crm.rely.backend.model.pojo.table.GoldenGoodsVideoTable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoldenGoodsVideoServiceImpl extends BaseUploadServiceImpl implements GoldenGoodsVideoService {

    @Autowired
    private GoldenGoodsVideoRepository repository;
    @Autowired
    private GoldenGoodsVideoMapper mapper;
    @Autowired
    private GoldenGoodsRepository goldenGoodsRepository;
    @Autowired
    private GoldenGoodsService goldenGoodsService;

    @Override
    public ResultWithPagerDto<GoldenGoodsVideoListDto> customPageList(GoldenGoodsVideoSearchEntity entity, CustomInfoEntity infoEntity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<GoldenGoodsVideoTable> tables = mapper.pageList(entity);
        if (tables == null || tables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }

        Map<Long,GoldenGoodsTable> map = new HashMap<>();
        if (infoEntity != null && infoEntity.getId() != null) {
            List<Long> goodsIds = new ArrayList<>();
            goodsIds.add(entity.getGoodsId());
            map =  goldenGoodsService.getCustomOrderGoods(goodsIds, infoEntity.getId());
        }

        List<GoldenGoodsVideoListDto> dtos = new ArrayList<>(tables.size());

        for (GoldenGoodsVideoTable table : tables) {
            GoldenGoodsVideoListDto dto = transformSearchListDto(table);

            if (map.containsKey(table.getGoodsId()) || table.getPayType() == 0) {
                dto.setPayType(0);
            }else{
                dto.setFileUrl(null);
                dto.setLinkUrl(null);
            }

            dtos.add(dto);
        }

        return ResultWithPagerDto.success(pageDto, dtos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(GoldenGoodsVideoAddEntity entity) throws ServiceException {
        GoldenGoodsTable goldenGoodsTable = goldenGoodsRepository.getFirstById(entity.getGoodsId());
        if (goldenGoodsTable == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        GoldenGoodsVideoTable table = new GoldenGoodsVideoTable();
        BeanUtils.copyProperties(entity, table);
        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(GoldenGoodsVideoDeleteEntitys entity) throws ServiceException {
        List<GoldenGoodsVideoTable> tables = repository.findAllByIdIn(entity.getIds());
        if (tables == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        repository.delete(tables);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(GoldenGoodsVideoUpdateEntity entity) throws ServiceException {
        GoldenGoodsVideoTable table = repository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);
        repository.save(table);
    }

    @Override
    public ResultWithPagerDto<GoldenGoodsVideoListDto> searchPageList(GoldenGoodsVideoSearchEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<GoldenGoodsVideoTable> tables = mapper.pageList(entity);
        if (tables == null || tables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<GoldenGoodsVideoListDto> dtos = new ArrayList<>(tables.size());

        for (GoldenGoodsVideoTable table : tables) {
            GoldenGoodsVideoListDto dto = transformSearchListDto(table);
            dtos.add(dto);
        }

        return ResultWithPagerDto.success(pageDto, dtos);
    }

    @Override
    public GoldenGoodsVideoListDto searchSingle(SingleLongEntity entity) throws ServiceException {
        GoldenGoodsVideoTable table = repository.getFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        return transformSearchListDto(table);
    }

    private GoldenGoodsVideoListDto transformSearchListDto(GoldenGoodsVideoTable table) {
        GoldenGoodsVideoListDto dto = new GoldenGoodsVideoListDto();
        BeanUtils.copyProperties(table, dto);
        return dto;
    }

    @Override
    public String uploadFile(Long goodsId, MultipartFile file) throws Exception {

        String middlePath = "/goods/video/" + goodsId;
        String path = saveFile(middlePath, file);

        return path;
    }

}
