package com.crm.custom.service.impl;

import com.crm.custom.dao.mapper.GoldenGoodsMapper;
import com.crm.custom.dao.repository.GoldenGoodsRepository;
import com.crm.custom.service.GoldenGoodsService;
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.GoldenGoodsListDto;
import com.crm.rely.backend.model.entity.golden.goods.GoldenGoodsAddEntity;
import com.crm.rely.backend.model.entity.golden.goods.GoldenGoodsDeleteEntitys;
import com.crm.rely.backend.model.entity.golden.goods.GoldenGoodsSearchEntity;
import com.crm.rely.backend.model.entity.golden.goods.GoldenGoodsUpdateEntity;
import com.crm.rely.backend.model.pojo.table.GoldenGoodsTable;
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.*;

@Service
public class GoldenGoodsServiceImpl extends BaseUploadServiceImpl implements GoldenGoodsService {

    @Autowired
    private GoldenGoodsRepository repository;
    @Autowired
    private GoldenGoodsMapper mapper;

    @Override
    public ResultWithPagerDto<GoldenGoodsListDto> customPageList(GoldenGoodsSearchEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }

        List<GoldenGoodsTable> tables = mapper.pageList(entity);
        if (tables == null || tables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }

        Map<Long,GoldenGoodsTable> map = new HashMap<>();
        if (entity.getCustomId() > 0){
            List<Long> goodsIds = new ArrayList<>();
            for (GoldenGoodsTable table : tables) {
                goodsIds.add(table.getId());
            }
            map = getCustomOrderGoods(goodsIds,entity.getCustomId());
        }

        List<GoldenGoodsListDto> dtos = new ArrayList<>(tables.size());
        for (GoldenGoodsTable table : tables) {
            GoldenGoodsListDto dto = transformSearchListDto(table);

            if (entity.getCustomId() < 0) {
                dto.setCustomType(0);
            }else{
                if(map.containsKey(table.getId())){
                    dto.setCustomType(2);
                }else{
                    dto.setCustomType(1);
                }
            }
            dtos.add(dto);
        }

        return ResultWithPagerDto.success(pageDto, dtos);
    }

    @Override
    public Map<Long,GoldenGoodsTable> getCustomOrderGoods(List<Long> goodsIds,Long customId) throws ServiceException {

        GoldenGoodsSearchEntity entity = new GoldenGoodsSearchEntity();
        entity.setGoodsIds(goodsIds);
        entity.setCustomId(customId);
        entity.setPage(null);
        List<GoldenGoodsTable> tables = mapper.customOrderList(entity);

        Map<Long,GoldenGoodsTable> goodsMap = new HashMap<>();
        for (GoldenGoodsTable table : tables) {
            goodsMap.put(table.getId(), table);
        }
        return goodsMap;

    }

    @Override
    public ResultWithPagerDto<GoldenGoodsListDto> customOrderList(GoldenGoodsSearchEntity entity) {
        Integer count = mapper.customOrderCount(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<GoldenGoodsTable> tables = mapper.customOrderList(entity);
        if (tables == null || tables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<GoldenGoodsListDto> dtos = new ArrayList<>(tables.size());

        for (GoldenGoodsTable table : tables) {
            GoldenGoodsListDto dto = transformSearchListDto(table);
            dtos.add(dto);
        }


        return ResultWithPagerDto.success(pageDto, dtos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(GoldenGoodsAddEntity entity) throws ServiceException {
        GoldenGoodsTable table = new GoldenGoodsTable();
        BeanUtils.copyProperties(entity, table);
        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(GoldenGoodsDeleteEntitys entity) throws ServiceException {
        List<GoldenGoodsTable> 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(GoldenGoodsUpdateEntity entity) throws ServiceException {
        GoldenGoodsTable 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<GoldenGoodsListDto> searchPageList(GoldenGoodsSearchEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<GoldenGoodsTable> tables = mapper.pageList(entity);
        if (tables == null || tables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<GoldenGoodsListDto> dtos = new ArrayList<>(tables.size());

        for (GoldenGoodsTable table : tables) {
            GoldenGoodsListDto dto = transformSearchListDto(table);
            dtos.add(dto);
        }

        return ResultWithPagerDto.success(pageDto, dtos);
    }

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

        return transformSearchListDto(table);
    }

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

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

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

        return path;
    }

}
