package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.*;
import shop.alien.entity.store.vo.StoreMainInfoVo;
import shop.alien.entity.store.vo.WebAuditVo;
import shop.alien.mapper.*;
import shop.alien.store.service.WebAuditService;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: fcw
 * @CreateTime: 2025-04-01
 * @Description: 审核
 */

@Service
@RequiredArgsConstructor
public class WebAuditServiceImpl extends ServiceImpl<WebAuditMapper, WebAudit> implements WebAuditService {

    private final WebAuditMapper webAuditMapper;

    private final StoreInfoMapper storeInfoMapper;

    private final LifeUserOrderMapper lifeUserOrderMapper;

    private final StoreCommentAppealMapper storeCommentAppealMapper;

    private final LifeCouponMapper lifeCouponMapper;

    private final LifeGroupPackageMapper lifeGroupPackageMapper;

    private final LifeGroupBuyMainMapper lifeGroupBuyMainMapper;

    private final LifeActivityMapper lifeActivityMapper;


    @Override
    public IPage<WebAudit> getOrderList(int page, int size, String status, String type) {
        IPage<WebAudit> storePage = new Page<>(page, size);
        QueryWrapper<WebAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!status.isEmpty() && !"null".equals(status), "status", status);
        queryWrapper.like(!type.isEmpty() && !"null".equals(type), "type", type);
        queryWrapper.orderByDesc("created_time");
        IPage<WebAudit> webAuditIPage = webAuditMapper.selectPage(storePage, queryWrapper);
        webAuditIPage.getRecords().forEach(item -> {
            if ("0".equals(item.getStatus())) {
                item.setStatus("待审核");
            } else if ("1".equals(item.getStatus())) {
                item.setStatus("已审核");
            } else {
                item.setStatus("未知状态");
            }
            if ("1".equals(item.getType())) {
                item.setType("店铺入驻");
            } else if ("2".equals(item.getType())) {
                item.setType("处理退款");
            } else if ("3".equals(item.getType())) {
                item.setType("商家申诉");
            } else if ("4".equals(item.getType())) {
                item.setType("套餐");
            } else if ("5".equals(item.getType())) {
                item.setType("活动");
            }else if ("6".equals(item.getType())) {
                item.setType("代金券");
            } else if ("7".equals(item.getType())) {
                item.setType("经营许可证");
            } else {
                item.setType("未知类型");
            }
        });
        return webAuditIPage;
    }

    @Override
    public List<WebAuditVo> getAuditSum() {
        QueryWrapper<WebAuditVo> queryWrapper = new QueryWrapper<>();
        return webAuditMapper.getAuditSum(queryWrapper);
    }

    @Override
    public Integer platformAuditRefundTrack(WebAudit webAudit) {
        // 输入验证
        if (Objects.isNull(webAudit) && Objects.isNull(webAudit.getId()) && Objects.isNull(webAudit.getType())) {
            log.error("传入的 LifeUserExpertWorks 对象或其 id 为空，无法进行更新操作");
            return 0;
        }

        WebAudit countColumn = getAuditColumn(webAudit);
        if (Objects.isNull(countColumn)) {
            log.error("未找到有效的计数类型，无法进行更新操作");
            return 0;
        }

        // 创建一个新的 WebAudit 对象
        WebAudit newWebAudit = new WebAudit();

        // 除了 id 以外的属性赋值，添加空值检查
        if (countColumn.getType() != null) {
            newWebAudit.setType(countColumn.getType());
        }
        if (countColumn.getStoreInfoId() != null) {
            newWebAudit.setStoreInfoId(countColumn.getStoreInfoId());
            StoreMainInfoVo storeInfo = storeInfoMapper.getStoreInfo(Integer.valueOf(countColumn.getStoreInfoId()));
            newWebAudit.setContent(storeInfo.getStoreName());
        }
        if (countColumn.getLifeUserOrderId() != null) {
            newWebAudit.setLifeUserOrderId(countColumn.getLifeUserOrderId());
            LifeUserOrder lifeUserOrder = lifeUserOrderMapper.selectById(Integer.valueOf(countColumn.getLifeUserOrderId()));
            newWebAudit.setContent(lifeUserOrder.getOrderNo());
        }
        if (countColumn.getStoreCommentAppealId() != null) {
            newWebAudit.setStoreCommentAppealId(countColumn.getStoreCommentAppealId());
            StoreCommentAppeal storeCommentAppeal = storeCommentAppealMapper.selectById(countColumn.getStoreCommentAppealId());
            newWebAudit.setContent(storeCommentAppeal.getAppealReason());
        }
        if (countColumn.getLifeGroupPackageId() != null) {
            newWebAudit.setLifeGroupPackageId(countColumn.getLifeGroupPackageId());
            LifeGroupBuyMain lifeGroupBuyMain = lifeGroupBuyMainMapper.selectById(Integer.valueOf(countColumn.getLifeGroupPackageId()));
            newWebAudit.setContent(lifeGroupBuyMain.getGroupName());

        }
        if (countColumn.getLifeActivityId() != null) {
            newWebAudit.setLifeActivityId(countColumn.getLifeActivityId());
            LifeActivity lifeActivity = lifeActivityMapper.selectById(countColumn.getLifeActivityId());
            newWebAudit.setContent(lifeActivity.getName());
        }

        if (countColumn.getCouponId() != null) {
            newWebAudit.setCouponId(countColumn.getCouponId());
            LifeCoupon lifeCoupon = lifeCouponMapper.selectById(countColumn.getCouponId());
            newWebAudit.setContent(lifeCoupon.getName());
        }

        newWebAudit.setCreatedTime(new Date());
        newWebAudit.setDeleteFlag(0);
        newWebAudit.setStatus("0");

        try {
            // 执行新增操作
            int result = webAuditMapper.insert(newWebAudit);
            if (!(result > 0)) {
                log.warn("新的 WebAudit 记录插入失败，");
            }
            return result;
        } catch (Exception e) {
            log.error("插入新的 WebAudit 记录时发生异常");
            return 0;
        }
    }

    // 根据传入的对象确定要更新的计数列名
    private WebAudit getAuditColumn(WebAudit webAudit) {
        if ("1".equals(webAudit.getType())) {
            webAudit.setStoreInfoId(webAudit.getId());
            return webAudit;
        } else if ("2".equals(webAudit.getType())) {
            webAudit.setLifeUserOrderId(webAudit.getId());
            return webAudit;
        } else if ("3".equals(webAudit.getType())) {
            webAudit.setStoreCommentAppealId(webAudit.getId());
            return webAudit;
        } else if ("4".equals(webAudit.getType())) {
            webAudit.setLifeGroupPackageId(webAudit.getId());
            return webAudit;
        } else if ("5".equals(webAudit.getType())) {
            webAudit.setLifeActivityId(webAudit.getId());
            return webAudit;
        } else if("6".equals(webAudit.getType())){
            webAudit.setCouponId(webAudit.getId());
            return webAudit;
        }
        return null;
    }
}
