package com.liquidnet.client.admin.web.controller.zhengzai.kylin;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.liquidnet.client.admin.common.annotation.Log;
import com.liquidnet.client.admin.common.core.controller.BaseController;
import com.liquidnet.client.admin.common.core.domain.AjaxResult;
import com.liquidnet.client.admin.common.core.page.TableDataInfo;
import com.liquidnet.client.admin.common.enums.BusinessType;
import com.liquidnet.client.admin.zhengzai.kylin.service.impl.KylinPerformancesAdminServiceImpl;
import com.liquidnet.client.admin.zhengzai.kylin.service.impl.KylinRefundPerformancesAdminServiceImpl;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.kylin.dao.OrderRefundBatchDao;
import com.liquidnet.service.kylin.dao.PerformanceOrderStatisticalDao;
import com.liquidnet.service.kylin.dto.param.RefundBatchApplyParam;
import com.liquidnet.service.kylin.dto.param.RefundBatchSearchParam;
import com.liquidnet.service.kylin.dto.vo.returns.KylinOrderRefundBatchesPerformanceVo;
import com.liquidnet.service.kylin.dto.vo.returns.KylinOrderRefundBatchesVo;
import com.liquidnet.service.kylin.entity.KylinPerformanceStatus;
import com.liquidnet.service.kylin.entity.KylinPerformances;
import com.liquidnet.service.kylin.mapper.KylinPerformanceStatusMapper;
import com.liquidnet.service.kylin.mapper.KylinPerformancesMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台按演出批量退款 服务实现类
 * </p>
 *
 * @author jiangxiulong
 * @since 2021-05-25 11:07 上午
 */

@Controller
@RequestMapping("/kylin/refundBatch")
public class KylinOrderRefundBatchAdminController extends BaseController {

    private String prefix = "zhengzai/kylin/refundBatch";

    @Autowired
    private KylinRefundPerformancesAdminServiceImpl kylinRefundPerformancesAdminServiceImpl;

    @Autowired
    private KylinPerformancesAdminServiceImpl kylinPerformancesService;

    @Autowired
    private KylinPerformanceStatusMapper performanceStatusMapper;

    @Autowired
    private KylinPerformancesMapper performancesMapper;

    @RequiresPermissions("kylin:refundBatch:view")
    @GetMapping()
    public String operlog()
    {
        return prefix + "/refundBatch";
    }

    @Log(title = "演出退款管理", businessType = BusinessType.DETAIL)
    @RequiresPermissions("kylin:refundBatch:detail")
    @GetMapping(value = "/details/{refundBatchId}")
    public String detail(@PathVariable("refundBatchId") String refundBatchId, ModelMap mmap) {
        KylinOrderRefundBatchesVo result = null;
        result = kylinRefundPerformancesAdminServiceImpl.detail(refundBatchId);
        mmap.put("KylinOrderRefundsBatchVo", result);
        return prefix + "/details";
    }

    @Log(title = "演出退款管理", businessType = BusinessType.LIST)
    @RequiresPermissions("kylin:refundBatch:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo refundList(RefundBatchSearchParam refundBatchSearchParam) {
        PageInfo<OrderRefundBatchDao> result = kylinRefundPerformancesAdminServiceImpl.refundList(refundBatchSearchParam);
        return getDataTable(result.getList());
    }

    @GetMapping("applyView")
    public String applyView() {
        return prefix +  "/applyView";
    }


    @Log(title = "演出退款管理", businessType = BusinessType.OTHER)
    @PostMapping("applyPerformance")
    @ResponseBody
    public AjaxResult applyPerformance(RefundBatchApplyParam refundBatchApplyParam) {
        String performancesId = refundBatchApplyParam.getTargetId();
        //获取演出详情
        KylinPerformances kylinPerformances = performancesMapper.selectOne(Wrappers.lambdaQuery(KylinPerformances.class)
                .eq(KylinPerformances::getPerformancesId, performancesId)
                .select(KylinPerformances::getTitle)
        );
        KylinPerformanceStatus kylinPerformanceStatus = performanceStatusMapper.selectOne(Wrappers.lambdaQuery(KylinPerformanceStatus.class)
                .eq(KylinPerformanceStatus::getPerformanceId, performancesId)
                .select(KylinPerformanceStatus::getStatus)
        );
        if (null == kylinPerformances) {
            return error("未找到该演出数据");
        }
        if (kylinPerformanceStatus.getStatus() != 7) {
            return error("当前演出未下架，不可退款");
        }
        List<PerformanceOrderStatisticalDao> result = kylinPerformancesService.getPerformanceOrderStatisticalList(performancesId);
        KylinOrderRefundBatchesPerformanceVo kylinOrderRefundBatchesPerformanceVo = new KylinOrderRefundBatchesPerformanceVo();
        kylinOrderRefundBatchesPerformanceVo.setTitle(kylinPerformances.getTitle());
        kylinOrderRefundBatchesPerformanceVo.setPerformanceOrderStatisticalDao(result);
        return AjaxResult.success(kylinOrderRefundBatchesPerformanceVo);
    }

    @Log(title = "演出退款管理", businessType = BusinessType.APPLY)
    @RequiresPermissions("kylin:refundBatch:apply")
    @PostMapping("apply")
    @ResponseBody
    public AjaxResult refundBatchApply(RefundBatchApplyParam refundBatchApplyParam) {
        try {
            ResponseDto res = kylinRefundPerformancesAdminServiceImpl.refundBatchApply(refundBatchApplyParam);
            if (res.isSuccess()) {
                return success();
            } else {
                return error(res.getMessage());
            }
        } catch (Exception e) {
            logger.info("refundBatchApplyException", e);
            return error(e.getMessage());
        }
    }

    @Log(title = "演出退款管理", businessType = BusinessType.REAPPLY)
    @RequiresPermissions("kylin:refundBatch:reapply")
    @PostMapping("reapply")
    @ResponseBody
    public AjaxResult refundBatchReApply(RefundBatchApplyParam refundBatchApplyParam) {
        String ids = refundBatchApplyParam.getIds();
        refundBatchApplyParam.setRefundBatchId(ids);
        refundBatchApplyParam.setType("reapply");
        try {
            ResponseDto res = kylinRefundPerformancesAdminServiceImpl.refundBatchCheckStatus(refundBatchApplyParam);
            if (res.isSuccess()) {
                return success();
            } else {
                return error(res.getMessage());
            }
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }


    @Log(title = "演出退款管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("kylin:refundBatch:cancel")
    @PostMapping("cancel")
    @ResponseBody
    public AjaxResult refundBatchCancel(RefundBatchApplyParam refundBatchApplyParam) {
        String ids = refundBatchApplyParam.getIds();
        refundBatchApplyParam.setRefundBatchId(ids);
        refundBatchApplyParam.setType("cancel");
        try {
            ResponseDto res = kylinRefundPerformancesAdminServiceImpl.refundBatchCheckStatus(refundBatchApplyParam);
            if (res.isSuccess()) {
                return success();
            } else {
                return error(res.getMessage());
            }
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @Log(title = "演出退款管理", businessType = BusinessType.AUDIT)
    @RequiresPermissions("kylin:refundBatch:review")
    @PostMapping("review")
    @ResponseBody
    public AjaxResult refundBatchReview(RefundBatchApplyParam refundBatchApplyParam) {
        refundBatchApplyParam.setType("review");
        try {
            ResponseDto res = kylinRefundPerformancesAdminServiceImpl.refundBatchCheckStatus(refundBatchApplyParam);
            if (res.isSuccess()) {
                return success();
            } else {
                return error(res.getMessage());
            }
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    // 批量没有快递数据 所以没有此功能 如果加的话就用演出id查订单再查退款
    @Log(title = "演出退款管理", businessType = BusinessType.AUDIT3)
    @RequiresPermissions("kylin:refundBatch:ticketAudit")
    @PostMapping("ticketAudit")
    @ResponseBody
    public AjaxResult ticketAudit(RefundBatchApplyParam refundBatchApplyParam) {
        try {
            ResponseDto res = kylinRefundPerformancesAdminServiceImpl.ticketAuditAll(refundBatchApplyParam);
            if (res.isSuccess()) {
                return success();
            } else {
                return error(res.getMessage());
            }
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @Log(title = "演出退款管理", businessType = BusinessType.AUDIT2)
    @RequiresPermissions("kylin:refundBatch:execute")
    @PostMapping("execute")
    @ResponseBody
    public AjaxResult refundBatchExecute(RefundBatchApplyParam refundBatchApplyParam) {
        refundBatchApplyParam.setType("execute");
        try {
            ResponseDto res = kylinRefundPerformancesAdminServiceImpl.refundBatchCheckStatus(refundBatchApplyParam);
            if (res.isSuccess()) {
                return success();
            } else {
                return error(res.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

}
