package com.crm.custom.service.impl;

import com.crm.custom.dao.mapper.GoldenOrderMapper;
import com.crm.custom.dao.repository.GoldenOrderRepository;
import com.crm.custom.service.GoldenOrderService;
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.exception.ServiceException;
import com.crm.rely.backend.model.constant.PayConstants;
import com.crm.rely.backend.model.dto.golden.order.GoldenOrderDetailsDto;
import com.crm.rely.backend.model.dto.golden.order.GoldenOrderListDto;
import com.crm.rely.backend.model.entity.golden.order.GoldenOrderSearchEntity;
import com.crm.rely.backend.model.pojo.table.GoldenOrderTable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class GoldenOrderServiceImpl implements GoldenOrderService {

    @Autowired
    private GoldenOrderMapper mapper;

    @Autowired
    private GoldenOrderRepository repository;

    @Override
    public ResultWithPagerDto<GoldenOrderListDto> searchPageList(GoldenOrderSearchEntity entity) throws ServiceException {
        Integer count = mapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<GoldenOrderTable> tables = mapper.pageList(entity);
        if (tables == null || tables.size() <= 0) {
            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<String> serialList = new ArrayList<>();
        for (GoldenOrderTable table : tables) {
            serialList.add(table.getSerial());
        }

        Map<String,List<GoldenOrderDetailsDto>> map = new HashMap<>();
        List<GoldenOrderDetailsDto> detailsDtos = mapper.getListBySerial(serialList);
        for (GoldenOrderDetailsDto dto : detailsDtos) {
            List<GoldenOrderDetailsDto> dtos = null;
            if (map.containsKey(dto.getSerial())) {
                dtos = map.get(dto.getSerial());
            }else {
                dtos = new ArrayList<>();
            }
            dtos.add(dto);

            map.put(dto.getSerial(), dtos);
        }


        List<GoldenOrderListDto> dtos = new ArrayList<>(tables.size());
        for (GoldenOrderTable table : tables) {
            GoldenOrderListDto dto = transformSearchListDto(table);
            dto.setDetails(map.get(table.getSerial()));
            dtos.add(dto);
        }

        return ResultWithPagerDto.success(pageDto, dtos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelOrder(Long id) throws ServiceException {
        GoldenOrderTable table = repository.findFirstById(id);
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        if (table.getStatus() != PayConstants.ORDER_UP_PAY_STATUS) {
            throw ServiceException.exception("cancel_error");
        }

        table.setStatus(PayConstants.ORDER_PAY_CANCEL_STATUS);
        table.setModifyTime(new Date());
        repository.save(table);

    }

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


}
