package jnpf.cultivate.controller.web.rule;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.base.ActionResult;
import jnpf.base.entity.SuperBaseEntity;
import jnpf.cultivate.service.FtbCultivateRuleService;
import jnpf.model.cultivate.dto.rule.FtbCultivateRuleDto;
import jnpf.model.cultivate.po.FtbCultivateRule;
import jnpf.model.cultivate.vo.rule.FtbCultivateRuleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * web规则配置
 * @Author: peng.hao
 * @create: 2024/9/9:15:23
 */
@RestController
@Slf4j
@RequestMapping("/web/cultivate-rule")
public class FtbCultivateRuleController {

    @Resource
    private FtbCultivateRuleService ftbCultivateRuleService;

    /**
     * 获取规则列表
     * @return
     */
    @RequestMapping("/getRuleList")
    public ActionResult<FtbCultivateRuleVO> getRuleList(){
        FtbCultivateRule entity = ftbCultivateRuleService.lambdaQuery().eq(FtbCultivateRule::getAttachments, 0).one();
        return ActionResult.success(FtbCultivateRuleVO.covert(entity));
    }

    /**
     * 开关附件下载规则
     * @param cultivateRuleDto
     * @return
     */
    @PutMapping("/updateRuleInfo")
    public ActionResult<Void> updateRuleInfo(@RequestBody FtbCultivateRuleDto cultivateRuleDto){
        LambdaUpdateWrapper<FtbCultivateRule> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.set(FtbCultivateRule::getAttachmentsRule, cultivateRuleDto.getAttachmentsRule());
        updateWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId,cultivateRuleDto.getId());
        ftbCultivateRuleService.update(updateWrapper);
        return ActionResult.success();
    }
}
