package com.storlead.trade.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.common.result.Result;
import com.storlead.trade.dto.CustomerAnalysisResultDTO;
import com.storlead.trade.dto.CustomerSingleAnalysisRequestDTO;
import com.storlead.trade.entity.CustomerAnalysisResultEntity;
import com.storlead.trade.entity.SopEntity;
import com.storlead.trade.service.CustomerAiAnalysisService;
import com.storlead.trade.service.CustomerAnalysisResultEntityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer/analysis")
@Api(tags = "营销: 客户AI分析")
public class CustomerAiAnalysisController {

    @Resource
    private CustomerAiAnalysisService customerAiAnalysisService;

    @Resource
    private CustomerAnalysisResultEntityService customerAnalysisResultEntityService;

    @PostMapping("/single")
    @ApiOperation("单营销: 客户AI分析并保存结果")
    public Result<Object> analyzeSingle(@RequestBody CustomerSingleAnalysisRequestDTO request) {
        if (request == null || request.getCustomerId() == null) {
            return Result.error("customerId不能为空");
        }
        CustomerAnalysisResultEntity result = customerAiAnalysisService
                .analyzeSingleCustomer(request.getCustomerId(), request.getAnalysisScene());
        return Result.result(result);
    }

    @RequestMapping("/list")
    @ApiOperation("客户AI分析结果列表")
    public Result<Object> list(@RequestBody CustomerAnalysisResultDTO dto) {
        Page<CustomerAnalysisResultEntity> page = new Page<>(dto.getPageIndex(), dto.getPageSize());
        LambdaQueryWrapper<CustomerAnalysisResultEntity> wrapper = new LambdaQueryWrapper<>();
        // 按客户ID筛选
        if (!ObjectUtils.isEmpty(dto.getCustomerId())) {
            wrapper.eq(CustomerAnalysisResultEntity::getCustomerId, dto.getCustomerId());
        }
        // 按分析场景筛选
        if (!ObjectUtils.isEmpty(dto.getAnalysisScene())) {
            wrapper.eq(CustomerAnalysisResultEntity::getAnalysisScene, dto.getAnalysisScene());
        }
        // 按客户等级筛选
        if (!ObjectUtils.isEmpty(dto.getCustomerLevel())) {
            wrapper.eq(CustomerAnalysisResultEntity::getCustomerLevel, dto.getCustomerLevel());
        }
        // 按分析状态筛选
        if (!ObjectUtils.isEmpty(dto.getAnalysisStatus())) {
            wrapper.eq(CustomerAnalysisResultEntity::getAnalysisStatus, dto.getAnalysisStatus());
        }
        // 按优先级筛选
        if (!ObjectUtils.isEmpty(dto.getPriority())) {
            wrapper.eq(CustomerAnalysisResultEntity::getPriority, dto.getPriority());
        }
        // 按分析时间倒序
        wrapper.orderByDesc(CustomerAnalysisResultEntity::getAnalysisTime);
        // 分页查询
        IPage<CustomerAnalysisResultEntity> iPage = customerAnalysisResultEntityService.page(page, wrapper);
        return Result.result(page);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "客户AI分析结果添加", notes = "客户AI分析结果添加")
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> add(@RequestBody CustomerAnalysisResultDTO dto) {
        CustomerAnalysisResultEntity entity = new CustomerAnalysisResultEntity();
        BeanUtils.copyProperties(dto, entity);
        boolean isOk = customerAnalysisResultEntityService.save(entity);
        if (!isOk) {
            return Result.error("添加失败");
        }
        return Result.result(entity);
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "客户AI分析结果修改", notes = "客户AI分析结果修改")
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> edit(@RequestBody CustomerAnalysisResultDTO dto) {
        if (ObjectUtils.isEmpty(dto.getId())) {
            return Result.error("id不能为空");
        }
        CustomerAnalysisResultEntity entity = new CustomerAnalysisResultEntity();
        BeanUtils.copyProperties(dto, entity);
        boolean isOk = customerAnalysisResultEntityService.updateById(entity);
        if (!isOk) {
            return Result.error("修改失败");
        }
        return Result.result(entity);
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "客户AI分析结果删除", notes = "客户AI分析结果删除")
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> delete(@RequestBody CustomerAnalysisResultDTO dto) {
        boolean isOk = false;
        Long id = dto.getId();
        if (!ObjectUtils.isEmpty(id)) {
            isOk = customerAnalysisResultEntityService.removeById(id);
        }
        List<Long> idList = dto.getIdList();
        if (!CollectionUtils.isEmpty(idList)) {
            isOk = customerAnalysisResultEntityService.removeByIds(idList);
        }
        if (!isOk) {
            return Result.error("删除失败");
        }
        return Result.ok();
    }

    @RequestMapping("/statistics/by-level")
    @ApiOperation("按客户等级分类统计")
    public Result<Object> statisticsByCustomerLevel(@RequestBody(required = false) CustomerAnalysisResultDTO dto) {
        String analysisScene = null;
        Integer analysisStatus = null;
        if (dto != null) {
            analysisScene = dto.getAnalysisScene();
            analysisStatus = dto.getAnalysisStatus();
        }
        List<Map<String, Object>> statistics;
        if (analysisScene != null || analysisStatus != null) {
            statistics = customerAnalysisResultEntityService.getStatisticsByCustomerLevel(analysisScene, analysisStatus);
        } else {
            statistics = customerAnalysisResultEntityService.getStatisticsByCustomerLevel();
        }
        return Result.result(statistics);
    }
}
