package com.zksy.base.warning.service.impl;

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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.base.warning.domain.WarningItem;
import com.zksy.base.warning.mapper.WarningItemMapper;
import com.zksy.base.warning.service.WarningItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class WarningItemServiceImpl extends ServiceImpl<WarningItemMapper, WarningItem> implements WarningItemService {

    @Override
    public IPage<WarningItem> queryWarningItems(Page<WarningItem> page, String itemCode, String itemName, 
                                                 String warningType, String warningLevel, String warningSpecial, String status) {
        LambdaQueryWrapper<WarningItem> wrapper = new LambdaQueryWrapper<>();
        
        if (itemCode != null && !itemCode.isEmpty()) {
            wrapper.like(WarningItem::getItemCode, itemCode);
        }
        if (itemName != null && !itemName.isEmpty()) {
            wrapper.like(WarningItem::getItemName, itemName);
        }
        if (warningType != null && !warningType.isEmpty()) {
            wrapper.eq(WarningItem::getWarningType, warningType);
        }
        if (warningLevel != null && !warningLevel.isEmpty()) {
            wrapper.eq(WarningItem::getWarningLevel, warningLevel);
        }
        if (warningSpecial != null && !warningSpecial.isEmpty()) {
            wrapper.like(WarningItem::getWarningSpecial, warningSpecial);
        }
        if (status != null && !status.isEmpty()) {
            wrapper.eq(WarningItem::getStatus, status);
        }
        
        wrapper.orderByDesc(WarningItem::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addWarningItem(WarningItem warningItem) {
        if (warningItem.getItemCode() == null || warningItem.getItemCode().isEmpty()) {
            warningItem.setItemCode(generateItemCode(warningItem.getWarningType(), warningItem.getWarningLevel()));
        }
        if (warningItem.getStatus() == null) {
            warningItem.setStatus("ENABLED");
        }
        return this.save(warningItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateWarningItem(WarningItem warningItem) {
        return this.updateById(warningItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateItemStatus(String itemId, String status) {
        WarningItem item = new WarningItem();
        item.setItemId(itemId);
        item.setStatus(status);
        return this.updateById(item);
    }

    @Override
    public WarningItem getItemDetail(String itemId) {
        return this.getById(itemId);
    }

    @Override
    public WarningItem getItemByCode(String itemCode) {
        LambdaQueryWrapper<WarningItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WarningItem::getItemCode, itemCode);
        return this.getOne(wrapper);
    }

    @Override
    public String generateItemCode(String warningType, String warningLevel) {
        String dateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String prefix = "WI" + warningType.substring(0, Math.min(3, warningType.length())).toUpperCase() + warningLevel;
        
        LambdaQueryWrapper<WarningItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(WarningItem::getItemCode, prefix + dateStr);
        wrapper.orderByDesc(WarningItem::getItemCode);
        wrapper.last("LIMIT 1");
        
        WarningItem lastItem = this.getOne(wrapper);
        int sequence = 1;
        
        if (lastItem != null) {
            String lastCode = lastItem.getItemCode();
            String lastSeqStr = lastCode.substring(lastCode.length() - 4);
            try {
                sequence = Integer.parseInt(lastSeqStr) + 1;
            } catch (NumberFormatException e) {
                sequence = 1;
            }
        }
        
        return prefix + dateStr + String.format("%04d", sequence);
    }
}
