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.BusinessTransferDTO;
import com.storlead.sales.pojo.entity.customer.BusinessEntity;
import com.storlead.sales.pojo.entity.customer.BusinessTransferEntity;
import com.storlead.sales.pojo.entity.customer.OrderFormEntity;
import com.storlead.sales.enums.ResourceTypeEnum;
import com.storlead.sales.mapper.customer.BusinessTransferMapper;
import com.storlead.sales.service.customer.BusinessService;
import com.storlead.sales.service.customer.BusinessTransferService;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import com.storlead.sales.service.customer.OrderFormService;
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-05-31
 */
@Service
public class BusinessTransferServiceImpl extends MyBaseServiceImpl<BusinessTransferMapper, BusinessTransferEntity> implements BusinessTransferService {

    @Resource
    private BusinessService businessService;
    @Resource
    private OrderFormService orderingFormService;

    @Override
    public Result<?> transfer(BusinessTransferDTO businessTransferDTO) {

        String types = "";
        if(!CollectionUtils.isEmpty(businessTransferDTO.getTypeList())){
            types = String.join(",", businessTransferDTO.getTypeList().stream()
                    .map(Object::toString) // 将Long转换为String
                    .collect(Collectors.toList())); // 收集为List<String>
//            return Result.error("转移类型不能为空");
        }
        Map<Long, BusinessEntity> businessMap = businessService.getBusinessMap(businessTransferDTO.getBusinessIdList());
        List<BusinessTransferEntity> businessTransferList = new ArrayList<>();
        for (Long businessId : businessTransferDTO.getBusinessIdList()){
            BusinessTransferEntity businessTransferEntity = new BusinessTransferEntity();
            businessTransferEntity.setBusinessId(businessId);
            businessTransferEntity.setOldOwnerBy(businessMap.get(businessId).getOwnerBy());
            businessTransferEntity.setNewOwnerBy(businessTransferDTO.getNewOwnerBy());
            businessTransferEntity.setRemark(businessTransferDTO.getRemark());
            businessTransferEntity.setTypes(types);
            businessTransferList.add(businessTransferEntity);
        }
        boolean saveOk = saveBatch(businessTransferList);
        if (saveOk){
            //更新商机负责人
            LambdaUpdateWrapper<BusinessEntity> wrapper = new LambdaUpdateWrapper<>();
            wrapper.in(BusinessEntity::getId,businessTransferDTO.getBusinessIdList())
                    .set(BusinessEntity::getOwnerBy,businessTransferDTO.getNewOwnerBy());
            businessService.update(wrapper);
            //更新订单负责人
            if (businessTransferDTO.getTypeList().contains(Integer.valueOf(ResourceTypeEnum.ORDER_FORM.code))) {
                LambdaUpdateWrapper<OrderFormEntity> orderWrapper = new LambdaUpdateWrapper<>();
                orderWrapper.in(OrderFormEntity::getBusinessId,businessTransferDTO.getBusinessIdList())
                        .set(OrderFormEntity::getOwnerBy,businessTransferDTO.getNewOwnerBy());
                orderingFormService.update(orderWrapper);
            }
            return Result.ok();
        }

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