/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.smartapplication.capture.controller;

import cn.hutool.json.JSONUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.smartapplication.capture.entity.CaptureRecords;
import org.springblade.smartapplication.capture.vo.CaptureRecordsVO;
import org.springblade.smartapplication.capture.service.ICaptureRecordsService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * 出入记录 控制器
 *
 * @author BladeX
 * @since 2020-11-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("capturerecord/capturerecords")
@Api(value = "出入记录", tags = "出入记录接口")
public class CaptureRecordsController extends BladeController {

	private ICaptureRecordsService captureRecordsService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入captureRecords")
	public R<CaptureRecords> detail(CaptureRecords captureRecords) {
		CaptureRecords detail = captureRecordsService.getOne(Condition.getQueryWrapper(captureRecords));
		return R.data(detail);
	}

	/**
	 * 分页 出入记录
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入captureRecords")
	public R<IPage<CaptureRecords>> list(@RequestParam Map<String,Object> captureRecords, Query query, BladeUser bladeUser) {
		IPage<CaptureRecords> pages = captureRecordsService.page(Condition.getPage(query), Condition.getQueryWrapper(captureRecords,CaptureRecords.class).lambda().eq(CaptureRecords::getTenantId, bladeUser.getTenantId()));
		return R.data(pages);
	}

	/**
	 * 自定义分页 出入记录
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入captureRecords")
	public R<IPage<CaptureRecordsVO>> page(CaptureRecordsVO captureRecords, Query query) {
		IPage<CaptureRecordsVO> pages = captureRecordsService.selectCaptureRecordsPage(Condition.getPage(query), captureRecords);
		return R.data(pages);
	}

	/**
	 * 新增 出入记录
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入captureRecords")
	public R save(@Valid @RequestBody CaptureRecords captureRecords) {
		return R.status(captureRecordsService.save(captureRecords));
	}

	/**
	 * 修改 出入记录
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入captureRecords")
	public R update(@Valid @RequestBody CaptureRecords captureRecords) {
		return R.status(captureRecordsService.updateById(captureRecords));
	}

	/**
	 * 新增或修改 出入记录
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入captureRecords")
	public R submit(@Valid @RequestBody CaptureRecords captureRecords) {
		return R.status(captureRecordsService.saveOrUpdate(captureRecords));
	}


	/**
	 * 删除 出入记录
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(captureRecordsService.removeByIds(Func.toLongList(ids)));
	}


	@GetMapping(value = "/getSessionId")
	public R<String> getSessionId() {
		String sessionId = null;

		RestTemplate restTemplate = new RestTemplate();
//		HttpHeaders httpHeaders =
		ResponseEntity responseEntity = null;
		HttpHeaders headers = new HttpHeaders();
		headers.add(HttpHeaders.ACCEPT, "application/json, text/plain, */*");
		headers.add(HttpHeaders.CONTENT_TYPE,"application/json;charset=UTF-8");
//        headers.add(HttpHeaders.);
		headers.add(HttpHeaders.HOST, "qiotapi.qinlinkeji.com");
		headers.add("Cookie", "acw_tc=acw_tc=2760824216043762523671945eb979bec7bac43e69959c0cde59aa79eb9e0f");
		Map<String, Object> map = new HashMap<>();
//       {
//      iv: "2846983943071693"
//      key: ""
//      loginName: "银川刘浪"
//      loginPassword: "CC3EF666DD655CEB271B5BA8E1C5C04F"
//      mfaCode: ""
//      scene: "nc_login"
//      sessionId: ""
//      sig: ""
//      token: ""
//}
		map.put("iv", "1156283113535473");
		map.put("key","");
		map.put("loginName", "银川刘浪");
		map.put("loginPassword", "CB2A6259A46B0A89A4634A49D3EAD8E6");
		map.put("mfaCode","");
		map.put("sessionId","");
		map.put("scene", "nc_login");
		map.put("sig","");
		map.put("token","");


		HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtil.toJsonStr(map), headers);
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
		};
		String url = "https://qiotapi.qinlinkeji.com/admin/user/login";
		URI uri = URI.create(url);
		responseEntity = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);


		Map map1 = (Map) responseEntity.getBody();
		Map data = (Map)map1.get("data");
		sessionId = data.get("sessionId").toString();
		return R.data(sessionId);
	}
}
