package com.rj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rj.entity.SopManagementMy;
import com.rj.mapper.SopManagementMyMapper;
import com.rj.service.ISopManagementMyService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * SOP知识库管理服务实现
 */
@Service
public class SopManagementMyServiceImpl extends ServiceImpl<SopManagementMyMapper, SopManagementMy>
        implements ISopManagementMyService {

    private static final String DEFAULT_STATUS = "已发布";

    @Override
    public Map<String, Object> add(SopManagementMy entity) {
        Map<String, Object> result = new HashMap<>();
        try {
            entity.setId(UUID.randomUUID().toString().replace("-", ""));
            LocalDateTime now = LocalDateTime.now();
            entity.setCreateTime(now);
            entity.setUpdateTime(now);
            if (entity.getStatus() == null || entity.getStatus().trim().isEmpty()) {
                entity.setStatus(DEFAULT_STATUS);
            }
            if (entity.getViewCount() == null) {
                entity.setViewCount(0);
            }
            if (entity.getLikeCount() == null) {
                entity.setLikeCount(0);
            }
            if (entity.getSortOrder() == null) {
                entity.setSortOrder(0);
            }
            if (entity.getIsTop() == null) {
                entity.setIsTop(false);
            }
            boolean ok = this.save(entity);
            result.put("success", ok);
            result.put("message", ok ? "添加成功" : "添加失败");
            if (ok) {
                result.put("data", entity);
            }
            return result;
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加异常：" + e.getMessage());
            return result;
        }
    }

    @Override
    public Map<String, Object> update(SopManagementMy entity) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (entity.getId() == null || entity.getId().trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "ID不能为空");
                return result;
            }
            entity.setUpdateTime(LocalDateTime.now());
            boolean ok = this.updateById(entity);
            result.put("success", ok);
            result.put("message", ok ? "更新成功" : "更新失败");
            if (ok) {
                result.put("data", this.getById(entity.getId()));
            }
            return result;
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新异常：" + e.getMessage());
            return result;
        }
    }

    @Override
    public Map<String, Object> deleteById(String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean ok = this.removeById(id);
            result.put("success", ok);
            result.put("message", ok ? "删除成功" : "删除失败");
            return result;
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除异常：" + e.getMessage());
            return result;
        }
    }

    @Override
    public Map<String, Object> pageQuery(Integer current, Integer size, String title, String category, String industry,
                                         String creator, String status) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (current == null || current < 1) {
                current = 1;
            }
            if (size == null || size < 1) {
                size = 10;
            }
            Page<SopManagementMy> page = new Page<>(current, size);
            LambdaQueryWrapper<SopManagementMy> q = new LambdaQueryWrapper<>();
            if (title != null && !title.trim().isEmpty()) {
                q.like(SopManagementMy::getTitle, title.trim());
            }
            if (category != null && !category.trim().isEmpty()) {
                q.eq(SopManagementMy::getCategory, category.trim());
            }
            if (industry != null && !industry.trim().isEmpty()) {
                q.eq(SopManagementMy::getIndustry, industry.trim());
            }
            if (creator != null && !creator.trim().isEmpty()) {
                q.like(SopManagementMy::getCreator, creator.trim());
            }
            if (status != null && !status.trim().isEmpty()) {
                q.eq(SopManagementMy::getStatus, status.trim());
            }
            q.orderByDesc(SopManagementMy::getIsTop)
                    .orderByDesc(SopManagementMy::getSortOrder)
                    .orderByDesc(SopManagementMy::getUpdateTime);

            Page<SopManagementMy> data = this.page(page, q);
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", data.getRecords());
            result.put("total", data.getTotal());
            result.put("current", data.getCurrent());
            result.put("size", data.getSize());
            result.put("pages", data.getPages());
            return result;
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            return result;
        }
    }
}
