package com.wzj.soopin.order.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.order.convert.AftersaleConvert;
import com.wzj.soopin.order.domain.bo.AftersaleBo;
import com.wzj.soopin.order.domain.entity.Aftersale;
import com.wzj.soopin.order.domain.form.DealWithAftersaleForm;
import com.wzj.soopin.order.domain.vo.*;
import com.wzj.soopin.order.service.impl.AftersaleServiceImpl;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.SecurityUtils;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.redis.redis.RedisService;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单售后Controller
 *
 * @author zcc
 * @date 2022-12-29
 */
@Tag(name ="订单售后接口列表")
@RestController
@RequestMapping("/oms/aftersale")
@Slf4j
@RequiredArgsConstructor
public class AftersaleController extends BaseController {

    private final AftersaleServiceImpl service;
    private final AftersaleConvert convert;
    private final RedisService redisService;


    @Tag(name ="查询订单售后列表")
    @PostMapping("/list")
    public R<IPage<AftersaleVO>> list(@RequestBody AftersaleBo query, Page<Aftersale> page) {
        return R.ok(service.getlist(page,query));
    }


    @Tag(name ="导出订单售后列表")
    @Log(title = "订单售后", businessType = BusinessType.EXPORT)
    @PostMapping("export")
    public R<String> export(AftersaleBo query) {
        List<Aftersale> list = service.list(query.toWrapper());
        ExcelUtil<AftersaleVO> util = new ExcelUtil<>(AftersaleVO.class);
        return R.ok(util.writeExcel(convert.toVO(list), "订单售后数据"));
    }

    @Tag(name ="获取订单售后详细信息")
    @GetMapping(value = "/{id}")
    public ResponseEntity<ManagerRefundOrderDetailVO> getInfo(@PathVariable("id") Long orderId) {
        return ResponseEntity.ok(service.selectById(orderId));
    }

    @Tag(name ="新增订单售后")
    @Log(title = "订单售后", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R add(@RequestBody Aftersale aftersale) {
        Long tenantId = Long.valueOf(LoginHelper.getTenantId());
        aftersale.setTenantId(tenantId);
        return R.ok(service.save(aftersale));
    }

    @Tag(name ="修改订单售后")
    @Log(title = "订单售后", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public R edit(@RequestBody Aftersale aftersale) {
       return R.ok(service.updateById(aftersale));
    }

    @Tag(name = "删除订单售后")
    @Log(title = "订单售后", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Long id) {
        return R.ok(service.removeById(id));
    }


    @Tag(name ="售后订单操作")
    @PostMapping("/dealWith")
    public ResponseEntity<Boolean> updateStatus(@RequestBody DealWithAftersaleForm request){
        LoginUser user = SecurityUtils.getLoginUser();
        String redisKey = "manager_oms_order_updateOrderStatus_" + user.getUserId();
        String redisValue = user.getUserId() + "_" + System.currentTimeMillis();
        try {
            redisService.lock(redisKey, redisValue, 60);
            service.dealWith(request, user.getUserId(), user.getNickname());
            return ResponseEntity.ok(true);
        } catch (Exception e) {
            log.error("售后订单操作发生异常", e);
            throw new RuntimeException(e.getMessage());
        } finally {
            try {
                redisService.unLock(redisKey, redisValue);
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }

    @Tag(name ="查看日志")
    @GetMapping("/log/{orderId}")
    public R log(@PathVariable Long orderId){
        return R.ok(service.log(orderId));
    }

    @Tag(name ="退款审批")
    @Log(title = "退款审批", businessType = BusinessType.UPDATE)
    @GetMapping("/audit")
    public R audit( @RequestParam Long id,
                    @RequestParam Integer authFlag,
                    @RequestParam(required = false) String cause) {
        return R.ok(service.audit(id,authFlag,cause));
    }


}
