package cn.com.countrygarden.bi.main.controller.mediaResource;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dto.mediaResource.MediaCycleDto;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.service.mediaResource.MediaCommonService;
import cn.com.countrygarden.bi.main.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/media/common")
public class MediaCommonController {
    @Autowired
    private MediaCommonService mediaCommonService;

    /**
     * 校验开始时间结束时间期间是否未使用
     */
    @GetMapping("/check")
    public Result checkBill(MediaCycleDto param) {
        try {
            if (param == null) {
                throw new ServiceException("参数不能为空");
            }
            if (!StringUtils.hasText(param.getMediaId())) {
                throw new ServiceException("媒体资源id不能为空");
            }
            if (param.getCycleStartDateStr() == null) {
                throw new ServiceException("开始时间不能为空");
            } else {
                param.setCycleStartDate(DateUtil.parseDate(param.getCycleStartDateStr()));
            }
            if (param.getCycleEndDateStr() == null) {
                throw new ServiceException("结束时间不能为空");
            } else {
                param.setCycleEndDate(DateUtil.parseDate(param.getCycleEndDateStr()));
            }
            return Response.response(mediaCommonService.checkBill(param));
        } catch (ServiceException e) {
            return Response.responseFail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Response.responseFail("校验订单异常！");
        }
    }

    /**
     * 校验开始时间结束时间期间是否未使用
     */
    @GetMapping("/getOrderType")
    public Result getOrderType() {
        try {
            return Response.response(mediaCommonService.getOrderStatusType());
        } catch (ServiceException e) {
            return Response.responseFail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Response.responseFail("锁定订单异常！");
        }
    }

    /**
     * 判断当前用户是否有权限
     */
    @GetMapping("/isAdmin")
    public Result isAdmin() {
        Map<String, Boolean> map = new HashMap<>();
        map.put("isAdmin",mediaCommonService.isAdminRole());
        return Response.response(map);
    }
}
