/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.comment.model.DistributionWithdrawCashSet;
import com.yami.shop.distribution.comment.service.DistributionWithdrawCashSetService;
import com.yami.shop.security.platform.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * 提现设置
 *
 * @author xwc
 * @date 2019-04-13 15:45:12
 */
@RestController
@AllArgsConstructor
@RequestMapping("/platform/distributionWithdrawCashSet")
public class DistributionWithdrawCashSetController {

    private final DistributionWithdrawCashSetService distributionWithdrawCashSetService;

    /**
     * 分页查询
     *
     * @param page                        分页对象
     * @param distributionWithdrawCashSet 提现设置
     * @return 分页数据
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('plateform:distributionWithdrawCashSet:page')")
    public ResponseEntity<IPage<DistributionWithdrawCashSet>> getDistributionWithdrawCashSetPage(PageParam<DistributionWithdrawCashSet> page, DistributionWithdrawCashSet distributionWithdrawCashSet) {
        return ResponseEntity.ok(distributionWithdrawCashSetService.page(page, new LambdaQueryWrapper<DistributionWithdrawCashSet>()));
    }

    /**
     * 通过id查询提现设置
     *
     * @param id id
     * @return 单个数据
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('plateform:distributionWithdrawCashSet:info')")
    public ResponseEntity<DistributionWithdrawCashSet> getById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(distributionWithdrawCashSetService.getById(id));
    }

    /**
     * 通过shopId查询提现设置
     *
     * @return 单个数据
     */
    @GetMapping("/info")
    @PreAuthorize("@pms.hasPermission('plateform:distributionWithdrawCashSet:info')")
    public ResponseEntity<DistributionWithdrawCashSet> getByShopId() {
        return ResponseEntity.ok(distributionWithdrawCashSetService.getByShopId());
    }

    /**
     * 新增提现设置
     *
     * @param distributionWithdrawCashSet 提现设置
     * @return 是否新增成功
     */
    @SysLog("新增提现设置")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('plateform:distributionWithdrawCashSet:save')")
    public ResponseEntity<Boolean> save(@RequestBody @Valid DistributionWithdrawCashSet distributionWithdrawCashSet) {
        distributionWithdrawCashSet.setUpdateTime(new Date());
        distributionWithdrawCashSet.setShopId(Constant.PLATFORM_SHOP_ID);
        distributionWithdrawCashSet.setModifier(SecurityUtils.getSysUser().getUserId());
        return ResponseEntity.ok(distributionWithdrawCashSetService.save(distributionWithdrawCashSet));
    }

    /**
     * 修改提现设置
     *
     * @param distributionWithdrawCashSet 提现设置
     * @return 是否修改成功
     */
    @SysLog("修改提现设置")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('plateform:distributionWithdrawCashSet:update')")
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid DistributionWithdrawCashSet distributionWithdrawCashSet) {
        distributionWithdrawCashSet.setUpdateTime(new Date());
        distributionWithdrawCashSet.setModifier(SecurityUtils.getSysUser().getUserId());
        distributionWithdrawCashSet.setShopId(Constant.PLATFORM_SHOP_ID);
        distributionWithdrawCashSetService.updateById(distributionWithdrawCashSet);

        // 移除缓存
        distributionWithdrawCashSetService.removeCacheByShopId(Constant.PLATFORM_SHOP_ID);
        return ResponseEntity.ok().build();
    }

    /**
     * 通过id删除提现设置
     *
     * @param id id
     * @return 是否删除成功
     */
    @SysLog("删除提现设置")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('plateform:distributionWithdrawCashSet:delete')")
    public ResponseEntity<Boolean> removeById(@PathVariable Long id) {
        return ResponseEntity.ok(distributionWithdrawCashSetService.removeById(id));
    }

}
