package com.storlead.sales.controller.customer;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.customer.CustomerTransferDTO;
import com.storlead.sales.pojo.entity.customer.CustomerEntity;
import com.storlead.sales.service.customer.BusinessService;
import com.storlead.sales.service.customer.CustomerService;
import com.storlead.sales.service.customer.CustomerTransferService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户转移表 前端控制器
 * </p>
 *
 * @author YPZ
 * @since 2024-06-03
 */
@RestController
@RequestMapping("/customer")
@Api(tags = "销售: 客户管理相关接口")
public class CustomerTransferController {

    @Resource
    private CustomerTransferService customerTransferService;

    @Resource
    private CustomerService customerService;
    @Resource
    private BusinessService businessService;

    @PostMapping(value = "/transfer")
    @ApiOperation(value = "客户管理-转移客户", notes = "客户管理-转移客户")
    public Result<?> transfer(@RequestBody CustomerTransferDTO customerTransferDTO) {
        return customerTransferService.transfer(customerTransferDTO);
    }

    @PostMapping(value = "/resignTransfer")
    @ApiOperation(value = "客户管理-离职继承", notes = "客户管理-离职继承")
    public Result<?> resignTransfer(@RequestBody CustomerTransferDTO customerTransferDTO) {
        if (CollectionUtils.isEmpty(customerTransferDTO.getOldOwnerByIdls())) {
            return Result.error("参数错误");
        }
        //转移客户
        LambdaQueryWrapper<CustomerEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(CustomerEntity::getId);
        lambdaQueryWrapper.eq(CustomerEntity::getCustomerForm, 10);
        lambdaQueryWrapper.in(CustomerEntity::getOwnerBy,customerTransferDTO.getOldOwnerByIdls());
        lambdaQueryWrapper.eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        List<CustomerEntity> customerls = customerService.list(lambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(customerls)) {
            List<Long> cusIds =  customerls.stream().map(CustomerEntity::getId).collect(Collectors.toList());
            customerTransferDTO.setIdList(cusIds);
            customerTransferDTO.setResourceType("10");
            customerTransferDTO.setRemark("离职转移");
            customerTransferDTO.setOperatingType(5);
            customerTransferService.transfer(customerTransferDTO);
        }

        //转移线索
        LambdaQueryWrapper<CustomerEntity> lambdaQueryClusWrapper = new LambdaQueryWrapper<>();
        lambdaQueryClusWrapper.select(CustomerEntity::getId);
        lambdaQueryClusWrapper.eq(CustomerEntity::getCustomerForm, 11);
        lambdaQueryClusWrapper.in(CustomerEntity::getOwnerBy,customerTransferDTO.getOldOwnerByIdls());
        lambdaQueryClusWrapper.eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        List<CustomerEntity> clusls = customerService.list(lambdaQueryClusWrapper);
        if (!CollectionUtils.isEmpty(clusls)) {
            List<Long> cusIds =  clusls.stream().map(CustomerEntity::getId).collect(Collectors.toList());
            customerTransferDTO.setIdList(cusIds);
            customerTransferDTO.setResourceType("11");
            customerTransferDTO.setRemark("离职转移");
            customerTransferDTO.setOperatingType(5);
            customerTransferService.transfer(customerTransferDTO);
        }

        //替换客户协作人员
        customerService.replaceCollaborator(customerTransferDTO);
        //替换未完成商机的协作人员
        businessService.replaceCollaborator(customerTransferDTO);

        return Result.ok();
    }

}
