package com.cyksj.web.controller.manage.corp;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.Jsons;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.corp.CorpInteractiveRadarUserRecordMapper;
import com.cyksj.model.entity.CorpInteractiveRadar;
import com.cyksj.model.entity.CorpInteractiveReaderLink;
import com.cyksj.model.views.CorpInteractiveRadarOrderDataView;
import com.cyksj.model.views.CorpInteractiveReaderOrderDetailView;
import com.cyksj.model.views.CorpInteractiveReaderView;
import com.cyksj.service.corp.CorpInteractiveRadarService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/*
 *项目名: netflix
 *文件名: CorpInteractiveRadarController
 *创建者: JavaZou
 *创建时间:2023/8/4 16:27
 */
@RestController
@RequestMapping("/manage/corp/interactive/reader")
@RequiredArgsConstructor
public class CorpInteractiveRadarController {
	private final CorpInteractiveRadarService corpInteractiveRadarService;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final CorpInteractiveRadarUserRecordMapper corpInteractiveRadarUserRecordMapper;

	/**
	 * 新增互动雷达
	 */
	@PostMapping
	public Result<String> saveInteractiveReader(@RequestBody @Validated CorpInteractiveRadar radar) {
		corpInteractiveRadarService.saveOrUpdateRadar(radar);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 编辑互动雷达
	 */
	@PutMapping
	public Result<String> updateInteractiveReader(@RequestBody @Validated CorpInteractiveRadar radar) {
		if (radar.getId() == null || corpInteractiveRadarService.getById(radar.getId()) == null) {
			throw BusinessRuntimeException.getInstance("雷达链接不存在");
		}
		corpInteractiveRadarService.saveOrUpdateRadar(radar);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除互动雷达
	 */
	@DeleteMapping("/{id}")
	public Result<String> deleteById(@PathVariable Long id) {
		corpInteractiveRadarService.removeById(id);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 互动雷达列表
	 */
	@GetMapping("/get")
	public Result<SearchResult<CorpInteractiveReaderView>> get() {
		SearchResult<CorpInteractiveReaderView> search = beanSearcher.search(CorpInteractiveReaderView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		search.getDataList().forEach(data -> {
			if (data.getType() == 1) {
				String linkInfoStr = data.getLinkInfoStr();
				try {
					CorpInteractiveReaderLink linkInfo = Jsons.parseObject(linkInfoStr, CorpInteractiveReaderLink.class);
					data.setLinkInfo(linkInfo);
				} catch (Exception e) {
				}
			}

			Map<String, Long> map = corpInteractiveRadarUserRecordMapper.selectClickRecord(data.getId());
			Integer clickNum = map.get("clickNum").intValue();
			data.setClickNum(clickNum);
			Integer clickUserNum = map.get("clickUserNum").intValue();
			data.setClickUserNum(clickUserNum);

			CorpInteractiveReaderOrderDetailView detailView = beanSearcher.searchFirst(CorpInteractiveReaderOrderDetailView.class, MapUtils.builder().put("id", String.format("id = %s", data.getId())).build());
			data.setPayNum(detailView.getTotal());
			data.setPayNewUserNum(detailView.getNumOfNewUser());
			data.setPayOldUserNum(detailView.getNumOfOldUser());
		});
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 购买人数详情
	 */
	@GetMapping("/get/payNum/detail/{id}")
	public Result<CorpInteractiveReaderOrderDetailView> getPayNumDetailById(@PathVariable Long id) {
		CorpInteractiveReaderOrderDetailView detailView = beanSearcher.searchFirst(CorpInteractiveReaderOrderDetailView.class, MapUtils.builder().put("id", String.format("id = %s", id)).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(detailView);
	}


	/**
	 * 雷达数据
	 */
	@GetMapping("/get/data/{id}")
	public Result<CorpInteractiveRadarOrderDataView> corpInteractiveRadarOrderDataViewResult(@PathVariable Long id) {
		CorpInteractiveRadarOrderDataView view = new CorpInteractiveRadarOrderDataView();
		DateTime beginOfDay = DateUtil.beginOfDay(DateTime.now());
		String idEq = String.format(" cr.radar_id = %s", id);
		String conditionSql = String.format(" %s and cr.created_time >= '%s'", idEq, beginOfDay.toString());
		CorpInteractiveRadarOrderDataView.CorpInteractiveRadarOrderData today = beanSearcher.searchFirst(CorpInteractiveRadarOrderDataView.CorpInteractiveRadarOrderData.class, MapUtils.builder().put("condition", conditionSql).build());

		CorpInteractiveRadarOrderDataView.CorpInteractiveRadarOrderData history = beanSearcher.searchFirst(CorpInteractiveRadarOrderDataView.CorpInteractiveRadarOrderData.class, MapUtils.builder().put("condition", idEq).build());

		view.setToday(today);
		view.setHistory(history);
		return GatewayResponse.SUCCESS.newBuilder().toResult(view);
	}
}
