package com.yami.shop.api.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.OrderRefund;
import com.yami.shop.bean.model.OrderRefundRecord;
import com.yami.shop.bean.model.RefundDelivery;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.OrderRefundRecordMapper;
import com.yami.shop.dao.ShopDetailMapper;
import com.yami.shop.service.OrderRefundService;
import com.yami.shop.service.RefundDeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

/**
 * 退货物流信息接口
 */
@RestController
@RequestMapping("/refund/delivery")
@Api(tags = "退货物流信息接口")
@AllArgsConstructor
public class RefundDeliveryController {

    private final RefundDeliveryService refundDeliveryService;
    private final OrderRefundService orderRefundService;
    private final OrderRefundRecordMapper orderRefundRecordMapper;
    private final ShopDetailMapper shopDetailMapper;

    /**
     * 分页获取退货物流信息列表
     */
    @GetMapping("/page")
    @ApiOperation("分页获取退货物流信息列表")
    public ResponseEntity<IPage<RefundDelivery>> page(PageParam<RefundDelivery> pageParam, RefundDelivery refundDelivery) {
        IPage<RefundDelivery> refundDeliveryPage = refundDeliveryService.page(pageParam, new LambdaQueryWrapper<RefundDelivery>());
        return ResponseEntity.ok(refundDeliveryPage);
    }

    /**
     * 根据退款编号获取退货物流信息
     */
    @GetMapping("/getByRefundSn/{refundSn}")
    @ApiOperation("根据退款编号获取退货物流信息")
    public ResponseEntity<RefundDelivery> getByRefundSn(@PathVariable("refundSn") String refundSn) {
        RefundDelivery refundDelivery = refundDeliveryService.getOne(new LambdaQueryWrapper<RefundDelivery>().eq(RefundDelivery::getRefundSn, refundSn));
        return ResponseEntity.ok(refundDelivery);
    }

    /**
     * 根据ID获取退货物流信息
     */
    @GetMapping("/getById/{id}")
    @ApiOperation("根据ID获取退货物流信息")
    public ResponseEntity<RefundDelivery> getById(@PathVariable Long id) {
        RefundDelivery refundDelivery = refundDeliveryService.getById(id);
        return ResponseEntity.ok(refundDelivery);
    }

    /**
     * 保存退货物流信息
     */
    @PostMapping("/save")
    @ApiOperation("保存退货物流信息")
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<?> save(@Valid @RequestBody RefundDelivery refundDelivery) {


        //获取门店信息
        ShopDetail shopDetail = shopDetailMapper.selectById(refundDelivery.getShopId());
        if (ObjectUtil.isEmpty(shopDetail)){
            throw new GlobalException("门店信息不存在");
        }
        //门店信息获取地址
        String shopAddress = shopDetail.getProvince() + shopDetail.getCity() + shopDetail.getArea() + shopDetail.getShopAddress();
        refundDelivery.setReceiverAddr(shopAddress);
        refundDelivery.setReceiverName(shopDetail.getTel());
        refundDelivery.setReceiverMobile(shopDetail.getShopOwner());
        refundDelivery.setReceiverTelephone(shopDetail.getMobile());
        refundDeliveryService.save(refundDelivery);
        //添加填写物流信息轨迹
        OrderRefund one = orderRefundService.getOne(new LambdaQueryWrapper<OrderRefund>().eq(OrderRefund::getRefundSn, refundDelivery.getRefundSn()));

        if (ObjectUtil.isEmpty( one)){
            throw new GlobalException("该退款单不存在");
        }
        OrderRefundRecord orderRefundRecord = new OrderRefundRecord();
        orderRefundRecord.setOrderRefundId(one.getRefundId());
        orderRefundRecord.setInstructions("商家收到商品井确认不影响二次销售后，将会为您处理退款");
        Date date = new Date();
        orderRefundRecord.setCreateTime(date);
        orderRefundRecord.setUpdateTime(date);
        orderRefundRecord.setAuditStatus(7);
        orderRefundRecord.setSort(5);
        orderRefundRecordMapper.insert(orderRefundRecord);
        return ResponseEntity.ok().build();
    }

    /**
     * 更新退货物流信息
     */
    @PutMapping("/update")
    @ApiOperation("更新退货物流信息")
    public ResponseEntity<String> update(@Valid @RequestBody RefundDelivery refundDelivery) {
        RefundDelivery dbRefundDelivery = refundDeliveryService.getById(refundDelivery.getRefundDeliveryId());
        if (dbRefundDelivery == null) {
            return ResponseEntity.badRequest().body("退货物流信息不存在");
        }

        refundDeliveryService.updateById(refundDelivery);
        return ResponseEntity.ok("更新成功");
    }


    /**
     * 删除退货物流信息
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除退货物流信息")
    public ResponseEntity<String> delete(@PathVariable Long id) {
        RefundDelivery refundDelivery = refundDeliveryService.getById(id);
        if (refundDelivery == null) {
            return ResponseEntity.badRequest().body("退货物流信息不存在");
        }

        refundDeliveryService.removeById(id);
        return ResponseEntity.ok("删除成功");
    }
}
