package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeUserPersonalizationSetting;
import shop.alien.store.service.LifeUserPersonalizationSettingService;

@Api(tags = {"用户个性化设置"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/lifeUserPersonalizationSetting")
@RequiredArgsConstructor
public class LifeUserPersonalizationSettingController {

    private final LifeUserPersonalizationSettingService lifeUserPersonalizationSettingService;

    @ApiOperation("新增个性化设置")
    @ApiOperationSupport(order = 1)
    @PostMapping("/add")
    public R<String> add(@RequestBody LifeUserPersonalizationSetting setting) {
        return lifeUserPersonalizationSettingService.add(setting);
    }

    @ApiOperation("根据主键删除（逻辑删除）")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/deleteById")
    public R<String> deleteById(@RequestParam Integer id) {
        return lifeUserPersonalizationSettingService.deleteById(id);
    }

    @ApiOperation("更新个性化设置；传 id 或传 userId 均可定位记录（无 userId 对应记录时会先插入默认再更新）")
    @ApiOperationSupport(order = 3)
    @PostMapping("/update")
    public R<String> update(@RequestBody LifeUserPersonalizationSetting setting) {
        return lifeUserPersonalizationSettingService.update(setting);
    }

    @ApiOperation("根据主键查询")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getById")
    public R<LifeUserPersonalizationSetting> getById(@RequestParam Integer id) {
        return lifeUserPersonalizationSettingService.getInfoById(id);
    }

    @ApiOperation("根据用户ID查询；若无记录则自动插入一条默认值后返回")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getByUserId")
    public R<LifeUserPersonalizationSetting> getByUserId(@RequestParam Integer userId) {
        return lifeUserPersonalizationSettingService.getByUserId(userId);
    }

    @ApiOperation("分页列表，可按 userId 筛选")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/list")
    public R<IPage<LifeUserPersonalizationSetting>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) Integer userId) {
        return lifeUserPersonalizationSettingService.list(pageNum, pageSize, userId);
    }
}
