package cn.iocoder.yudao.module.inspection.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.inspection.entity.InspectionBusinessChannel;
import cn.iocoder.yudao.module.inspection.service.InspectionBusinessChannelService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/channel")
@RequiredArgsConstructor
public class InspectionBusinessChannelController {

    private final InspectionBusinessChannelService inspectionBusinessChannelService;

    /**
     * 获取业务渠道/客户来源树结构
     */
    @GetMapping("/tree")
    public CommonResult<List<InspectionBusinessChannel>> getChannelTree(InspectionBusinessChannel channel) {
        return CommonResult.success(inspectionBusinessChannelService.list(Wrappers.<InspectionBusinessChannel>lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(channel.getType()), InspectionBusinessChannel::getType, channel.getType())
                .like(ObjectUtil.isNotEmpty(channel.getName()), InspectionBusinessChannel::getName, channel.getName())
                .orderByAsc(InspectionBusinessChannel::getSort)));
    }

    /**
     * 新增业务渠道或客户来源
     */
    @PostMapping("/add")
    public boolean addChannel(@RequestBody InspectionBusinessChannel channel) {
        if (ObjectUtil.isNotEmpty(channel.getUserIdList())) {
            String userIds = channel.getUserIdList().stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(","));
            channel.setUserIds(userIds);

        }
        return inspectionBusinessChannelService.save(channel);
    }

    /**
     * 获取业务渠道或客户来源
     */
    @GetMapping("/{id}")
    public CommonResult<InspectionBusinessChannel> getChannelById(@PathVariable("id") Long id) {
        InspectionBusinessChannel info = inspectionBusinessChannelService.getById(id);

        //将字符串转为集合
        if (ObjectUtil.isNotEmpty(info.getUserIds())) {
            List<Long> userIdList = Arrays.stream(info.getUserIds().split(","))
                    .filter(s -> s != null && !s.isEmpty()) // 可选：避免空字符串
                    .map(Long::valueOf)
                    .collect(Collectors.toList());
            info.setUserIdList(userIdList);

        }
        return CommonResult.success(info);
    }

    /**
     * 修改业务渠道或客户来源
     */
    @PutMapping("/update")
    public CommonResult<?> updateChannel(@RequestBody InspectionBusinessChannel channel) {
        if (ObjectUtil.isNotEmpty(channel.getUserIdList())) {
            String userIds = channel.getUserIdList().stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(","));
            channel.setUserIds(userIds);
        }else if (ObjectUtil.isEmpty(channel.getUserIds())) {
            channel.setUserIds("");
        }
        return CommonResult.success(inspectionBusinessChannelService.updateById(channel));
    }

    /**
     * 删除业务渠道或客户来源
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult<?> deleteChannel(@PathVariable("id") Long id) {
        return CommonResult.success(inspectionBusinessChannelService.removeById(id));
    }

    /**
     * 获取业务渠道或客户来源列表(app)
     *
     * @return
     */
    @GetMapping("/list")
    public CommonResult<?> list() {
        return CommonResult.success(inspectionBusinessChannelService.getChannelTree());
    }
}
