package com.template.controller;


import com.template.api.SmartScreenshotControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.enumModel.ePunish;
import com.template.model.pojo.SmartScreenshot;
import com.template.model.request.insertScreenshotRequest;
import com.template.model.request.updateScreenshotRequest;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.vo.SmartEnumVo;
import com.template.services.SmartScreenshotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;

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

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-12-04
 */
@RestController
public class SmartScreenshotController implements SmartScreenshotControllerAPI {

    @Autowired
    private SmartScreenshotService smartScreenshotService;

    /**
     * 新增截屏风控
     *
     * @param isr    截屏风控数据
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult insertSmartScreenshot(insertScreenshotRequest isr, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        //查看是否存在相同次数的数据
        SmartScreenshot existData = smartScreenshotService.getSmartByTimes(isr.getScreenshotNum());
        if (existData != null) {
            return CommonResult.fail("近一年截屏次数" + isr.getScreenshotNum() + "次数据已存在");
        }

        SmartScreenshot ss = new SmartScreenshot();
        ss.setScreenshotNum(isr.getScreenshotNum());
        ss.setTriggers(isr.getTrigger());

        int result = smartScreenshotService.insertSmartScreenshot(ss);

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

    /**
     * 更新截屏风控
     *
     * @param usr  截屏风控数据
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult updateSmartScreenshotById(updateScreenshotRequest usr, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        SmartScreenshot oldData = smartScreenshotService.getSmartById(usr.getId());
        if(oldData == null){
            return CommonResult.fail("风控截屏信息无效，修改失败");
        }

        if(usr.getScreenshotNum() != null && !usr.getScreenshotNum().equals(oldData.getScreenshotNum())){
            //查看是否存在相同次数的数据
            SmartScreenshot existData = smartScreenshotService.getSmartByTimes(usr.getScreenshotNum());
            if (existData != null) {
                return CommonResult.fail("近一年截屏次数" + usr.getScreenshotNum() + "次数据已存在");
            }
        }

        oldData.setTriggers(usr.getTrigger() == null ? oldData.getTriggers() : usr.getTrigger());
        oldData.setScreenshotNum(usr.getScreenshotNum() == null ? oldData.getScreenshotNum() : usr.getScreenshotNum());

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

    /**
     * 截屏风控分页数据查询
     *
     * @param currentPage 当前页数
     * @param pageCount   一页数据条数
     * @param name        查询名称
     * @return
     */
    @Override
    public CommonResult queryPageSmartScreenshots(int currentPage, int pageCount, String name) {
        PageUtils<SmartScreenshot> result = smartScreenshotService.queryPageSmartScreenshots(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }


    @Override
    public CommonResult deleteSmartScreenshotById(int id) {

        SmartScreenshot data = smartScreenshotService.getSmartById(id);

        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = smartScreenshotService.deleteSmartScreenshotById(id);

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

    @Override
    public CommonResult queryPunishs() {
        List<SmartEnumVo> result = new ArrayList<>();
        for (int i = 0; i <= 5; i++) {
            SmartEnumVo data = new SmartEnumVo();
            data.setId(i);
            data.setName(ePunish.stringOf(i));
            result.add(data);
        }

        return CommonResult.ok(result);
    }
}

