
package com.yami.shop.api.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.enums.ReturnMoneyStsType;
import com.yami.shop.bean.model.OrderRefund;
import com.yami.shop.bean.model.OrderRefundRecord;
import com.yami.shop.bean.model.RefundAppointment;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.util.R;
import com.yami.shop.dao.OrderRefundRecordMapper;
import com.yami.shop.service.OrderRefundService;
import com.yami.shop.service.RefundAppointmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("/p/appointment")
@Api(tags = "用户退款预约上门取件接口")
@Slf4j
@AllArgsConstructor
public class RefundAppointmentController {

    @Autowired
    private RefundAppointmentService refundAppointmentService;
    @Autowired
    private OrderRefundService orderRefundService;
    @Autowired
    private OrderRefundRecordMapper orderRefundRecordMapper;
    @PostMapping
    @ApiOperation ("保存上门取件信息")
    public R<Void> save(@RequestBody RefundAppointment refundAppointment) {
        //添加填写物流信息轨迹
        OrderRefund orderRefund = orderRefundService.getOne(new LambdaQueryWrapper<OrderRefund>().eq(OrderRefund::getRefundSn, refundAppointment.getRefundNumber()));
        if (ObjectUtil.isEmpty( orderRefund)){
            throw new GlobalException("该退款单不存在");
        }

        refundAppointmentService.save(refundAppointment);

        orderRefund.setReturnMoneySts(ReturnMoneyStsType.RECEIVE.value());
        orderRefund.setShipTime(new Date());
        orderRefund.setUpdateTime(new Date());
        orderRefund.setIsReturnLogistics(true);
        orderRefundService.updateById(orderRefund);

        OrderRefundRecord orderRefundRecord = new OrderRefundRecord();
        orderRefundRecord.setOrderRefundId(orderRefund.getRefundId());
        orderRefundRecord.setInstructions("商家收到商品井确认不影响二次销售后，将会为您处理退款");
        Date date = new Date();
        orderRefundRecord.setCreateTime(date);
        orderRefundRecord.setUpdateTime(date);
        orderRefundRecord.setAuditStatus(7);
        orderRefundRecord.setSort(5);
        orderRefundRecordMapper.insert(orderRefundRecord);

        //海博退款单状态同步65
        log.info("客户填写完成快递信息后海博退款单状态同步65");
        orderRefundService.changeStatus(orderRefund.getRefundSn(), ReturnMoneyStsType.RECEIVE.value());

        return R.SUCCESS();
    }

    @PutMapping
    @ApiOperation ("更新上门取件信息")
    public  R<Void> update(@RequestBody RefundAppointment refundAppointment) {
        refundAppointmentService.updateById(refundAppointment);
        return R.SUCCESS();
    }


    @GetMapping("/{refundNumber}")
    @ApiOperation ("通过退款单号查询信息")
    public R<RefundAppointment> getByRefundNumber(@PathVariable String refundNumber) {
        RefundAppointment info = refundAppointmentService.getOne(new LambdaQueryWrapper<RefundAppointment>()
                .eq(RefundAppointment::getRefundNumber, refundNumber)
                .eq(RefundAppointment::getStatus, 0));
        return R.SUCCESS(info);
    }



}
