package com.storlead.sales.service.impl.customer;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.customer.OrderFormTransferDTO;
import com.storlead.sales.pojo.entity.customer.BusinessEntity;
import com.storlead.sales.pojo.entity.customer.OrderFormTransferEntity;
import com.storlead.sales.pojo.entity.customer.OrderFormEntity;
import com.storlead.sales.enums.ResourceTypeEnum;
import com.storlead.sales.mapper.customer.OrderFormTransferMapper;
import com.storlead.sales.service.customer.BusinessService;
import com.storlead.sales.service.customer.OrderFormService;
import com.storlead.sales.service.customer.OrderFormTransferService;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单转移表 服务实现类
 * </p>
 *
 * @author YPZ
 * @since 2024-06-21
 */
@Service
public class OrderFormTransferServiceImpl extends MyBaseServiceImpl<OrderFormTransferMapper, OrderFormTransferEntity> implements OrderFormTransferService {

    @Resource
    private OrderFormService orderFormService;
    @Resource
    private BusinessService businessService;
    @Override
    public Result<?> transfer(OrderFormTransferDTO orderFormTransferDTO) {

        String types = "";
        if(!CollectionUtils.isEmpty(orderFormTransferDTO.getTypeList())){
            types = String.join(",", orderFormTransferDTO.getTypeList().stream()
                    .map(Object::toString) // 将Long转换为String
                    .collect(Collectors.toList())); // 收集为List<String>
//            return Result.error("转移类型不能为空");
        }
        Map<Long, OrderFormEntity> orderFormMap = orderFormService.getOrderFormMap(orderFormTransferDTO.getOrderFormIdList());
        List<OrderFormTransferEntity> orderFormTransferList = new ArrayList<>();
        for (Long orderFormId : orderFormTransferDTO.getOrderFormIdList()){
            OrderFormTransferEntity orderFormTransferEntity = new OrderFormTransferEntity();
            orderFormTransferEntity.setOrderFormId(orderFormId);
            orderFormTransferEntity.setOldOwnerBy(orderFormMap.get(orderFormId).getOwnerBy());
            orderFormTransferEntity.setNewOwnerBy(orderFormTransferDTO.getNewOwnerBy());
            orderFormTransferEntity.setRemark(orderFormTransferDTO.getRemark());
            orderFormTransferEntity.setTypes(types);
            orderFormTransferList.add(orderFormTransferEntity);
        }
        boolean saveOk = saveBatch(orderFormTransferList);
        if (saveOk){
            //更新订单负责人
            LambdaUpdateWrapper<OrderFormEntity> wrapper = new LambdaUpdateWrapper<>();
            wrapper.in(OrderFormEntity::getId,orderFormTransferDTO.getOrderFormIdList())
                    .set(OrderFormEntity::getOwnerBy,orderFormTransferDTO.getNewOwnerBy());
            orderFormService.update(wrapper);
            //更新商机负责人
            if (orderFormTransferDTO.getTypeList().contains(Integer.valueOf(ResourceTypeEnum.BUSINESS.code))) {
                LambdaUpdateWrapper<BusinessEntity> businessWrapper = new LambdaUpdateWrapper<>();
                businessWrapper.in(BusinessEntity::getCustomerId,orderFormTransferDTO.getOrderFormIdList())
                        .set(BusinessEntity::getOwnerBy,orderFormTransferDTO.getNewOwnerBy());
                businessService.update(businessWrapper);
            }
            return Result.ok();
        }

        return Result.error("转移失败");
    }
}
