package com.repair.controller;


import com.repair.api.RepairNoticeSettingControllerAPI;
import com.repair.common.utils.paramUtils;
import com.repair.model.enumModel.eIsTop;
import com.repair.model.pojo.RepairNoticeSetting;
import com.repair.model.result.CommonResult;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.NoticePageVo;
import com.repair.model.vo.NoticeVo;
import com.repair.services.RepairNoticeSettingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-07-11
 */
@RestController
public class RepairNoticeSettingController implements RepairNoticeSettingControllerAPI {

    @Autowired
    private RepairNoticeSettingService repairNoticeSettingService;

    /**
     * 新增公告数据
     * @param repairNoticeSetting
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult InsertRepairNoticeSetting(RepairNoticeSetting repairNoticeSetting, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        int result = repairNoticeSettingService.insertRepairNoticeSetting(repairNoticeSetting);

        return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
    }

    /**
     * 编辑公告数据
     * @param ra
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult updateRepairNoticeSettingById(RepairNoticeSetting ra, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        int result = repairNoticeSettingService.updateRepairNoticeSetting(ra);

        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    /**
     * 查询公告分页数据
     * @param currentPage 当前页
     * @param pageCount 一页数据条数
     * @param createTime 创建时间
     * @return
     */
    @Override
    public CommonResult queryPageRepairNoticeSettings(int currentPage, int pageCount, String createTime) throws ParseException {
        PageUtils<NoticePageVo> result = repairNoticeSettingService.queryPageRepairNoticeSettings(currentPage, pageCount, createTime);

        return CommonResult.ok(result);
    }

    /**
     * 取消或置顶公告数据
     * @param id 数据ID
     * @param isTop 是否置顶
     *               未置顶：0
     *               置顶：1
     * @return
     */
    @Override
    public CommonResult cancelOrTopNoticeById(int id, int isTop) {
        RepairNoticeSetting data = repairNoticeSettingService.getRepairById(id);
        if (data == null) {
            return CommonResult.fail("当前数据不存在，操作失败！");
        }
        if (data.getIsTop() == isTop) {
            String Message = isTop == eIsTop.Top.getValue() ? "当前处于置顶状态，请勿重复操作！" : "当前处于未置顶状态，请勿重复操作！";
            return CommonResult.fail(Message);
        }
        String success = isTop == eIsTop.Top.getValue() ? "置顶成功！" : "取消置顶成功";
        String fail = isTop == eIsTop.Top.getValue() ? "置顶失败！" : "取消置顶失败";
        RepairNoticeSetting ra = new RepairNoticeSetting();
        ra.setId(id);
        ra.setIsTop(isTop);
        int result = repairNoticeSettingService.updateRepairNoticeSetting(ra);
        return result > 0 ? CommonResult.ok(success) : CommonResult.fail(fail);
    }

    /**
     * 删除公告数据
     * @param id 数据ID
     * @return
     */
    @Override
    public CommonResult deleteRepairNoticeSettingById(int id) {
        RepairNoticeSetting data = repairNoticeSettingService.getRepairById(id);
        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = repairNoticeSettingService.deleteRepairNoticeSettingById(id);

        return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
    }

    /**
     * 获取首页公告展示数据
     * @return
     */
    @Override
    public CommonResult queryRepairNoticeSettingByTop() {
        List<RepairNoticeSetting> result = repairNoticeSettingService.getRepairNotices();

        NoticeVo data = new NoticeVo();

        List<String> NotriceStr = result.stream().map(RepairNoticeSetting::getContent).collect(Collectors.toList());

        data.setNoticeStr(StringUtils.join(NotriceStr, "    "));

        return CommonResult.ok(data);
    }
}

