package com.yami.shop.multishop.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.security.multishop.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("/shop/refundAddr")
public class RefundAddrController {

    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>()
                .eq(RefundAddr::getShopId, SecurityUtils.getShopUser().getShopId())
                .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 ResponseEntity<String> save(@RequestBody @Valid RefundAddr refundAddr) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        Date now = new Date();
        refundAddr.setShopId(shopId);
        refundAddr.setCreateTime(now);
        refundAddr.setUpdateTime(now);
        refundAddrService.addRefundAddr(refundAddr);
        return ResponseEntity.ok().body("添加成功");
    }

    /**
     * 修改
     *
     * @param refundAddr
     * @return 是否修改成功
     */
    @SysLog("修改")
    @PutMapping
    public ResponseEntity<String> updateById(@RequestBody @Valid RefundAddr refundAddr) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        refundAddr.setUpdateTime(new Date());
        refundAddrService.updateRefundAddr(shopId, 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() {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        List<RefundAddr> list = refundAddrService.list(new LambdaQueryWrapper<RefundAddr>()
                .eq(RefundAddr::getShopId, shopId)
                .eq(RefundAddr::getStatus, 1)
                .orderByDesc(RefundAddr::getUpdateTime));
        return ResponseEntity.ok(list);
    }

}
