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.custom.info.CustomInfoEntity;
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 com.crm.rely.backend.model.pojo.view.GoldenGoodsListView;
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.List;

@Service
public class GoldenGoodsServiceImpl extends BaseUploadServiceImpl implements GoldenGoodsService {

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

    @Override
    public ResultWithPagerDto<GoldenGoodsListDto> customPageList(GoldenGoodsSearchEntity entity, CustomInfoEntity infoEntity) throws ServiceException {
        if (infoEntity == null || infoEntity.getId() == null) {
            entity.setCustomId(-1l);
        }else {
            entity.setCustomId(infoEntity.getId());
        }

        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<GoldenGoodsListView> views = mapper.customPageList(entity);
        if (views == null || views.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<GoldenGoodsListDto> dtos = new ArrayList<>(views.size());

        for (GoldenGoodsListView view : views) {
            GoldenGoodsListDto dto = new GoldenGoodsListDto();
            BeanUtils.copyProperties(view, dto);

            if (infoEntity == null || infoEntity.getId() == null) {
                dto.setCustomType(0);
            }else{
                if(view.getCustomId() == null){
                    dto.setCustomType(1);
                }else{
                    dto.setCustomType(2);
                }
            }

            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;
    }

}
