package cn.pluss.platform.controller.store;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.device.MercOrderDetailService;
import cn.pluss.platform.dto.MercOrderDetailDTO;
import cn.pluss.platform.entity.MercOrderDetail;
import cn.pluss.platform.vo.MercOrderExpressVO;
import cn.pluss.platform.yt.YtCreateOrder;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * TODO
 * 订单相关接口
 * @author crystal
 * @date 2022/3/28 14:10
 */
@RestController
@RequestMapping("/mercOrderDetail")
public class MercOrderDetailController {

    @Resource
    private MercOrderDetailService modService;

    /**
     * 订单分页查询接口
     * @param page
     * @param condition
     * @return
     */
//    @GetMapping("/page")
//    @ApiOperation(tags = {"设备商城-我的-我的订单"}, value = "查询我的订单", notes = "我的订单分页查询", httpMethod = "GET")
//    public Result<Page<MercOrderDetail>> page(Page<MercOrderDetail> page, MercOrderDetail condition) {
//        UserApp app = userAppService.checkApiToken();
//        condition.setUserId(app.getUserId());
//        page.addOrder(new OrderItem().setColumn("id").setAsc(false));
//        page = modService.page(page, new QueryWrapper<>(condition));
//        return ResultGenerator.genSuccessResult(page);
//    }

    /**
     * 删除订单接口
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    @ApiOperation(tags = {"设备商城-我的-我的订单"}, value = "删除我的订单", notes = "删除我的订单")
    public Result<Object> delete(@RequestParam(value = "ids[]") Long[] ids) {
        modService.removeByIds(Arrays.asList(ids));
        return ResultGenerator.genSuccessResult("删除成功",null);
    }

    /**
     * 确认收货接口
     * @param id
     * @return
     */
    @GetMapping("/receiving/{id}")
    @ApiOperation(tags = {"设备商城-我的-确认收货"}, value = "确认收货", notes = "确认收货接口")
    public Result<Object> receiving(@PathVariable(value = "id") Long id) {
        modService.receiving(id);
        return ResultGenerator.genSuccessResult("操作成功",null);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ApiOperation(tags = {"设备商城-我的-查询详情"}, value = "查询详情", notes = "查询详情")
    public Result<Object> get(@PathVariable(value = "id") Long id) {
        MercOrderDetail entity = modService.getById(id);
        return ResultGenerator.genSuccessResult("查询成功",entity);
    }

    /**
     * 设置退款物流单号
     * @param dto
     * @return
     */
    @PostMapping("/refundExpressNo")
    @ApiOperation(tags = {"设备商城-我的-设置退款物流单号"}, value = "设置退款物流单号", notes = "设置退款物流单号")
    public Result<Object> setRefundExpressNo(@RequestBody MercOrderDetailDTO dto) {
        modService.setRefundExpressNo(dto);
        return ResultGenerator.genSuccessResult("操作成功",null);
    }

    @GetMapping("/getRefundAddress")
    @ApiOperation(tags = {"设备商城-我的-获取退款地址"}, value = "获取退款地址", notes = "获取退款地址信息")
    public Result<Object> getRefundAddress() {
        YtCreateOrder createOrder = new YtCreateOrder();
        JSONObject result = new JSONObject(3);
        result.put("name",createOrder.getSenderName());
        result.put("phone",createOrder.getSenderMobile());
        result.put("address",createOrder.getSenderProvinceName() + createOrder.getSenderCityName() + createOrder.getSenderCountyName() + createOrder.getSenderAddress());
        return ResultGenerator.genSuccessResult("操作成功",result);
    }

    /**
     * 查看物流信息接口
     * @param id
     * @return
     */
    @GetMapping("/express/{id}")
    @ApiOperation(tags = {"设备商城-我的-查看物流"}, value = "查看物流", notes = "查看物流详情接口")
    public Result<Object> express(@PathVariable(value = "id") Long id) {
        MercOrderExpressVO express = modService.getOrderExpressInfo(id);
        String msg = express == null ? "暂无物流信息" : "获取成功";
        return ResultGenerator.genSuccessResult(msg,express);
    }

    /**
     * 申请退款
     * @param dto
     * @return
     */
    @PostMapping("/refund")
    @ApiOperation(tags = {"设备商城-我的-申请退款"}, value = "申请退款", notes = "申请退款接口",httpMethod = "POST")
    public Result<Object> refund(@RequestBody MercOrderDetailDTO dto) {
        modService.refund(dto);
        return ResultGenerator.genSuccessResult("退款申请已提交，请耐心等待","");
    }
}
