package com.yami.shop.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.RefundAddr;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.R;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.RefundAddrService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@AllArgsConstructor
@RequestMapping("/p/refundAddr")
public class RefundAddrApiController {

    private final RefundAddrService refundAddrService;

    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param refundAddr
     * @return 分页数据
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<RefundAddr>> getRefundAddrPage(PageParam<RefundAddr> page, RefundAddr refundAddr) {
        return ResponseEntity.ok(refundAddrService.page(page, new LambdaQueryWrapper<RefundAddr>()
                .ne(RefundAddr::getStatus, -1)));
    }


    /**
     * 通过id查询
     *
     * @param refundAddrId id
     * @return 单个数据
     */
    @GetMapping("/info/{refundAddrId}")
    public ResponseEntity<RefundAddr> getById(@PathVariable("refundAddrId") Long refundAddrId) {
        RefundAddr refundAddr = refundAddrService.getById(refundAddrId);
        return ResponseEntity.ok(refundAddr);
    }

    /**
     * 新增
     *
     * @param refundAddr
     * @return 是否新增成功
     */
    @SysLog("新增")
    @PostMapping
    public R<String> save(@RequestBody @Valid RefundAddr refundAddr) {
        Date now = new Date();
        refundAddr.setCreateTime(now);
        refundAddr.setUpdateTime(now);
        refundAddrService.addRefundAddr(refundAddr);
        return R.SUCCESS("添加成功");
    }

    /**
     * 修改
     *
     * @param refundAddr
     * @return 是否修改成功
     */
    @SysLog("修改")
    @PutMapping
    public ResponseEntity<String> updateById(@RequestBody @Valid RefundAddr refundAddr) {
        refundAddr.setUpdateTime(new Date());
        refundAddrService.updateById( refundAddr);
        return ResponseEntity.ok().body("修改成功");
    }

    /**
     * 通过id删除
     *
     * @param refundAddrId id
     * @return 是否删除成功
     */
    @SysLog("删除")
    @DeleteMapping("/{refundAddrId}")
    public ResponseEntity<Boolean> removeById(@PathVariable Long refundAddrId) {
        return ResponseEntity.ok(refundAddrService.removeById(refundAddrId));
    }

    /**
     * 获取店铺的所有收获地址
     */
    @GetMapping("/list")
    public ResponseEntity<List<RefundAddr>> list() {
        List<RefundAddr> list = refundAddrService.list(new LambdaQueryWrapper<RefundAddr>()
                .eq(RefundAddr::getStatus, 1)
                .orderByDesc(RefundAddr::getUpdateTime));
        return ResponseEntity.ok(list);
    }

}
