package com.sv.oms.controller;

import com.sv.entity.Venue;
import com.sv.service.oms.VenueService;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.common.dto.ResponseDTO;
import com.ydd.framework.core.common.utils.JsonMapper;
import com.ydd.framework.core.entity.enums.StatusEnum;
import com.ydd.oms.controller.OmsController;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.text.ParseException;

/**
 * Controller - 场馆
 *
 * @author zuojie.Liang
 * @since 2018-08-02
 */
@RestController
public class VenueController extends OmsController {

    private final Logger logger = LoggerFactory.getLogger(VenueController.class);

    @Resource
    private VenueService venueService;


    @Resource
    private RestTemplate restTemplate;

    /**
     * 分页查询场馆
     */
    @RequestMapping(value = "/venues", method = RequestMethod.GET)
    public ResponseDTO findPage(Pagination pagination) {
        return ResponseDTO.ok()
                .setPagination(venueService.findPage(pagination));
    }

    @RequestMapping(value = "/venues/type", method = RequestMethod.GET)
    public ResponseDTO findByVenueType(@RequestParam("type") Integer type) {
        return ResponseDTO.ok().addAttribute("venues", venueService.findByVenueType(type));
    }

    /**
     * 生成二维码
     */
    @RequestMapping(value = "/venue/generate", method = RequestMethod.POST)
    public ResponseDTO delete(@RequestParam("venueId") Integer venueId) {
        venueService.generateCodeUrl(venueId);
        return ResponseDTO.ok("生成成功");
    }

    /**
     * 查询场馆
     */
    @RequestMapping(value = "/venue/venue/venue", method = RequestMethod.GET)
    public ResponseDTO findVenue() {
        return ResponseDTO.ok().addAttribute("card", venueService.findAlls());
    }

    /**
     * 查询场馆信息
     */
    @RequestMapping(value = "/venue/{id}", method = RequestMethod.GET)
    public ResponseDTO findById(@PathVariable("id") Integer id) {
        return ResponseDTO.ok()
                .addAttribute("venue", venueService.findById(id));
    }

    /**
     * 保存场馆
     */
    @RequestMapping(value = "/venue", method = RequestMethod.POST)
    public ResponseDTO save(Venue venue, String[] venueImage) throws ParseException {
        venue.setPlatformId(1);
        venueService.save(venue, venueImage);
        return ResponseDTO.ok("保存成功");
    }

    /**
     * 删除场馆
     */
    @RequestMapping(value = "/venue/delete", method = RequestMethod.POST)
    public ResponseDTO delete(@RequestParam("id[]") Integer[] ids) {
        venueService.deleteByIds(ids);
        return ResponseDTO.ok("删除成功");
    }

    @RequestMapping(value = "/venue/status", method = RequestMethod.POST)
    public ResponseDTO updateStatus(@RequestParam("id") Integer id, @Param("status") Byte status) {
        venueService.updateStatus(id, status);
        String operationName = (StatusEnum.ENABLE.value.equals(status) ? "禁用" : "启用") + "成功";
        return ResponseDTO.ok(operationName);
    }

    @RequestMapping(value = "/venue/clear", method = RequestMethod.POST)
    public ResponseDTO clear(@RequestParam("id") Integer id) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("LingtekID", "5d14229fcb1f5c1a9046f429");
        param.add("Number", "0");
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(param, headers);
        ResponseEntity<Result> result = restTemplate.exchange("http://lingtek.jalasmart.com/api/v1/lingtek/number", HttpMethod.PUT, request, Result.class);
        venueService.clearNumber(id);
        return ResponseDTO.ok();
    }
}
