package com.storlead.sales.controller.customized;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.entity.mail.ColumnPersonalizeRecordEntity;
import com.storlead.sales.service.ColumnPersonalizeRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-08-30 16:23
 */
@RestController
@RequestMapping("/personalize")
@Api(tags = "销售: 个性化设置")
public class ListColumnPersonalizeApiController {

    @Resource
    private ColumnPersonalizeRecordService personalizeRecordService;

    @ApiOperation(value = "保存列排序配置")
    @PostMapping( "/column/save")
    public Result<?> save(@RequestBody ColumnPersonalizeRecordEntity record) {
        Long currentUserId = LoginUserUtil.getCurrentUserId();
        record.setOwnerBy(currentUserId);
        ColumnPersonalizeRecordEntity columnPersonalize = personalizeRecordService.getOne(new LambdaQueryWrapper<ColumnPersonalizeRecordEntity>().eq(ColumnPersonalizeRecordEntity::getCode, record.getCode()).last("limit 1"));
        if (Objects.nonNull(columnPersonalize)) {
            record.setId(columnPersonalize.getId());
        }
        personalizeRecordService.saveOrUpdate(record);
        return Result.ok();
    }

    @ApiOperation(value = "查询列排序配置")
    @PostMapping( "/column/getList")
    public Result<?> list() {
        Long currentUserId = LoginUserUtil.getCurrentUserId();
        if (Objects.isNull(currentUserId)) {
            return Result.ok();
        }
        LambdaQueryWrapper<ColumnPersonalizeRecordEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ColumnPersonalizeRecordEntity::getOwnerBy,currentUserId);
        List<ColumnPersonalizeRecordEntity> columnPersonalizels = personalizeRecordService.list(wrapper);
        if (CollectionUtils.isEmpty(columnPersonalizels)) {
            return Result.ok();
        }
        Map<String, String> mapColumn = columnPersonalizels.stream()
                .filter(column -> column.getColumnJson() != null)  // 过滤掉 columnJson 为空的条目
                .collect(Collectors.toMap(
                        ColumnPersonalizeRecordEntity::getCode,
                        ColumnPersonalizeRecordEntity::getColumnJson,
                        (existing, replacement) -> existing  // 处理重复键的情况，保留已有的值
                ));


        return Result.result(mapColumn);
    }

    @ApiOperation(value = "获取指定配置")
    @PostMapping( "/column/getPersonalize")
    public Result<?> getPersonalizeList(String code) {
        Long currentUserId = LoginUserUtil.getCurrentUserId();
        if (Objects.isNull(currentUserId)) {
            return Result.ok();
        }
        if (StrUtil.isBlank(code)) {
            return Result.ok();
        }
        LambdaQueryWrapper<ColumnPersonalizeRecordEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ColumnPersonalizeRecordEntity::getOwnerBy,currentUserId);
        wrapper.eq(ColumnPersonalizeRecordEntity::getCode,code);
        List<ColumnPersonalizeRecordEntity> columnPersonalizels = personalizeRecordService.list(wrapper);

        if (Objects.isNull(columnPersonalizels)) {
            return Result.ok();
        }
        Map<String, String> mapColumn = columnPersonalizels.stream()
                .collect(Collectors.toMap(
                        ColumnPersonalizeRecordEntity::getCode,
                        ColumnPersonalizeRecordEntity::getColumnJson,
                        (existingValue, newValue) -> existingValue // 或 newValue, 或其他逻辑
                ));
        return Result.result(mapColumn);
    }
}
