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.service.RefundAddrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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
@Api(tags = "商家退货地址管理")
@RequestMapping("/p/refundAddr")
public class RefundAddrApiController {

    private final RefundAddrService refundAddrService;

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


    /**
     * 通过店铺id查询收货地址
     *
     * @param shop id
     * @return 单个数据
     */
    @GetMapping("/info/{shop}")
    @ApiOperation("通过店铺id查询收货地址")
    public R<RefundAddr> getById(@PathVariable("shop") Long shop) {
        RefundAddr refundAddr = refundAddrService.getOne(new LambdaQueryWrapper<RefundAddr>()
        .eq(RefundAddr::getShopId, shop)
        .eq(RefundAddr::getDefaultAddr, 1)
        .eq(RefundAddr::getStatus, 1));
        return R.SUCCESS(refundAddr);
    }

    /**
     * 新增
     *
     * @param refundAddr
     * @return 是否新增成功
     */
    @SysLog("新增")
    @ApiOperation("新增")
    @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("修改")
    @ApiOperation("修改")
    @PutMapping
    public R<String> updateById(@RequestBody @Valid RefundAddr refundAddr) {
        refundAddr.setUpdateTime(new Date());
        refundAddrService.updateById( refundAddr);
        return R.SUCCESS("修改成功");
    }

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

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

}
