package com.app.ydl.ydl.controller;

import cn.hutool.core.collection.ListUtil;
import com.app.framework.core.support.controller.CrudBaseControllerSupport;
import com.app.framework.core.support.view.form.AjaxResult;
import com.app.framework.core.support.view.grid.Paging;
import com.app.ydl.ydl.service.CallRecordService;
import com.app.ydl.ydl.view.CallRecordView;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 录音文件下载、解冻
 */
@Slf4j
@Controller
@RequestMapping("/call/call_record")
public class CallRecordController extends CrudBaseControllerSupport<CallRecordView> {

    @Resource
    CallRecordService callRecordService;

    private boolean isStandardOrder(String id) {
        if (id.startsWith("97") || id.startsWith("99")) {
            return true;
        }
        return false;
    }

    /**
     * 查询列表的具体实现
     * 请求：list
     *
     * @param request HttpServletRequest
     * @param view    查询条件
     * @param paging  分页条件
     * @return 查询结果
     */
    @Override
    public List<CallRecordView> list(HttpServletRequest request, CallRecordView view, Paging paging) {
        List<CallRecordView> list = null;
        if (StringUtils.isNotBlank(view.getOrderId())) {
            if (this.isStandardOrder(view.getOrderId())) {
                return callRecordService.getByStandardOrderId(Long.parseLong(view.getOrderId()));
            } else {
                list = callRecordService.getByRelationId(view.getOrderId());
            }
        } else if (StringUtils.isNotBlank(view.getRelationId())) {
            if (this.isStandardOrder(view.getRelationId())) {
                return callRecordService.getByStandardOrderServiceId(Long.parseLong(view.getRelationId()));
            } else {
                list = callRecordService.getByRelationId(view.getRelationId());
            }
        } else if (view.getId() != null) {
            CallRecordView byId = callRecordService.getById(view.getId());
            if (this.isStandardOrder(byId.getRelationId())) {
                return callRecordService.getByStandardOrderAndCallRecordId(view.getId());
            } else {
                list = ListUtil.toList(byId);
            }
        }
        if (list == null) {
            list = new ArrayList<>();
        }
        for (CallRecordView callRecordView : list) {
            callRecordView.setOrderId(callRecordView.getRelationId());
        }
        return list;
    }

    /**
     * 保存记录的具体实现
     * 请求：saveEntity
     *
     * @param request HttpServletRequest
     * @param entity  实体
     * @return 影响结果数
     */
    @Override
    public AjaxResult saveEntity(HttpServletRequest request, CallRecordView entity) {
        return AjaxResult.wrap(false);
    }

    /**
     * 删除记录,根据Id 具体实现
     * 请求：deleteById 参数oid
     * 请求：batchDelete 参数oid数组
     *
     * @param request HttpServletRequest
     * @param oids    实体
     * @return 影响结果数
     */
    @Override
    public AjaxResult batchDelete(HttpServletRequest request, Long oids[]) {
        return AjaxResult.wrap(false);
    }

    @RequestMapping(value = "/golist")
    public ModelAndView info(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        return new ModelAndView("admin/business/call_record/list.html", result);
    }

}
