package com.template.controller;


import com.template.api.AlumniHeartControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.enumModel.eTopType;
import com.template.model.pojo.AlumniHeart;
import com.template.model.request.clubTopupRequest;
import com.template.model.request.insertHeartRequest;
import com.template.model.request.updateHeartRequest;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.services.AlumniHeartService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2025-01-13
 */
@RestController
public class AlumniHeartController implements AlumniHeartControllerAPI {

    @Autowired
    private AlumniHeartService alumniHeartService;

    private static Logger logger = LoggerFactory.getLogger(AlumniHeartController.class);

    @Override
    public CommonResult HeartTopup(clubTopupRequest ctr, BindingResult bindingResult) throws Exception {
        if (ctr == null) {
            return CommonResult.fail("请传递参数");
        }
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        AlumniHeart ac = alumniHeartService.queryDataById(ctr.getId());
        if (ac == null) {
            return CommonResult.fail("数据已失效，无法进行置顶操作");
        }

        try {
            if (ctr.getIsTop().intValue() == eTopType.Top.getValue()) {//置顶
                //将其他置顶数据取消置顶
                List<AlumniHeart> acs = alumniHeartService.findTopHearts();
                if (acs != null && acs.size() > 0) {
                    for (AlumniHeart acData : acs) {
                        acData.setIsTop(eTopType.UnTop.getValue());
                    }
                    boolean insertBatch = alumniHeartService.updateBatchById(acs);
                    if (!insertBatch) {
                        throw new Exception("心链更新失败，置顶失败！");
                    }
                }

            } else {
                ac.setIsTop(ctr.getIsTop());
            }

            ac.setIsTop(ctr.getIsTop());
            int result = alumniHeartService.updateAlumniHeart(ac);
            if (result <= 0) {
                throw new Exception("心链更新失败，" + (ctr.getIsTop().intValue() == eTopType.Top.getValue() ? "置顶" : "取消置顶") + "失败！");
            }
        } catch (Exception e) {
            logger.info("心链置顶异常信息：" + e.getMessage());
            throw new Exception("心链置顶失败！");
        }
        return CommonResult.ok((ctr.getIsTop().intValue() == eTopType.Top.getValue() ? "置顶" : "取消置顶") + "成功");
    }

    @Override
    public CommonResult insertHeartData(insertHeartRequest icr, BindingResult bindingResult) {
        if (icr == null) {
            return CommonResult.fail("请传递参数");
        }
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        String content = StringEscapeUtils.escapeHtml4(icr.getContent());

        AlumniHeart ah = new AlumniHeart();
        ah.setEquityName(icr.getEquityName());
        ah.setCoverImage(icr.getCoverImage());
        ah.setContent(content);
        ah.setIsTop(icr.getIsTop() != null ? icr.getIsTop() : eTopType.UnTop.getValue());

        int result = alumniHeartService.insertAlumniHeart(ah);
        if (result <= 0) {
            return CommonResult.fail("创建失败");
        }

        return CommonResult.ok("创建成功");
    }

    @Override
    public CommonResult updateHeartData(updateHeartRequest ucr, BindingResult bindingResult) {
        if (ucr == null) {
            return CommonResult.fail("请传递参数");
        }
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        AlumniHeart ac = alumniHeartService.queryDataById(ucr.getId());
        if (ac == null) {
            return CommonResult.fail("数据已失效，编辑失败");
        }

        ac.setIsTop(ucr.getIsTop() != null ? ucr.getIsTop() : ac.getIsTop());
        ac.setEquityName(ucr.getEquityName());
        ac.setCoverImage(ucr.getCoverImage());
        String content = StringEscapeUtils.escapeHtml4(ucr.getContent());
        ac.setContent(content);

        int result = alumniHeartService.updateAlumniHeart(ac);
        if (result <= 0) {
            return CommonResult.fail("编辑失败");
        }

        return CommonResult.ok("编辑成功");
    }

    @Override
    public CommonResult queryHeartPage(int currentPage, int pageCount, String name, String startTime, String endTime) {
        PageUtils<AlumniHeart> result = alumniHeartService.queryPageList(currentPage, pageCount, name, startTime, endTime);
        for (AlumniHeart r : result.getList()) {
            r.setContent(StringEscapeUtils.unescapeHtml4(r.getContent()));
        }
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult deleteHeartById(int id) {
        AlumniHeart ae = alumniHeartService.queryDataById(id);
        if (ae == null) {
            return CommonResult.fail("心链数据已失效，无法删除");
        }

        int result = alumniHeartService.deleteDataById(id);
        if (result <= 0) {
            return CommonResult.fail("删除失败");
        }
        return CommonResult.ok("删除成功");
    }
}

