package com.cyksj.web.controller.manage.market;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.market.sign.SignConfigMapper;
import com.cyksj.model.entity.SignConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/manage/market/sign")
@RequiredArgsConstructor
public class SignController {
    private final SignConfigMapper signConfigMapper;

    /**
     * 每日签到设置明细
     */
    @GetMapping("/get")
    public Result<List<SignConfig>> getSign() {
        return GatewayResponse.SUCCESS.newBuilder().toResult(signConfigMapper.selectList(Wrappers.lambdaQuery(SignConfig.class).orderByAsc(SignConfig::getId)));
    }

    /**
     * 编辑每日签到设置
     */
    @PutMapping("/put")
    public Result<String> getSign(@RequestBody List<SignConfig> signConfigs) {
        signConfigs.forEach(signConfig->{
            if (signConfig.getId() == null || signConfig.getId() < 1) {
                throw BusinessRuntimeException.getInstance("id为空");
            }
            signConfigMapper.updateById(signConfig);
        });
        return GatewayResponse.SUCCESS.newBuilder().toResult("编辑成功");
    }
}
