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

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Assert;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.CorpTag;
import com.cyksj.model.entity.CorpTagRule;
import com.cyksj.model.entity.CorpTagRuleRelation;
import com.cyksj.model.excel.CorpUserTagExcelData;
import com.cyksj.model.request.CorpDelTagReq;
import com.cyksj.model.views.*;
import com.cyksj.service.corp.CorpTagService;
import com.cyksj.web.util.EasyExcelUtils;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/*
 *项目名: netflix
 *文件名: CorpTagController
 *创建者: JavaZou
 *创建时间:2023/6/20 16:01
 */
@RestController
@RequestMapping("/manage/corp")
@RequiredArgsConstructor
public class CorpTagController {

	private final CorpTagService corpTagService;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;


	/**
	 * 添加企业标签组
	 */
	@PostMapping("/addGroup")
	public Result<CorpTagGroupView> addGroup(@RequestBody CorpTagGroupView tagGroup) throws Exception {
		CorpTagGroupView corpTagGroupView = corpTagService.addGroup(tagGroup);
		return GatewayResponse.SUCCESS.newBuilder().toResult(corpTagGroupView);
	}

	/**
	 * 添加企业标签组及标签
	 */
	@PostMapping("/addCorpTagGroup")
	public Result<CorpTagGroupView> addCorpTagGroup(@RequestBody CorpTagGroupView tagGroup) throws Exception {
		CorpTagGroupView corpTagGroupView = corpTagService.addCorpTagGroup(tagGroup);
		return GatewayResponse.SUCCESS.newBuilder().toResult(corpTagGroupView);
	}

	/**
	 * 编辑企业标签
	 */
	@PutMapping("/editCorpTag")
	public Result<String> editCorpTag(@RequestBody CorpTagGroupView tagGroup) throws Exception {
		corpTagService.editCorpTag(tagGroup);

		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除企业标签组、标签
	 */
	@PutMapping("/del")
	public Result<String> delTags(@RequestBody CorpDelTagReq req) throws Exception {
		corpTagService.delTags(req);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 获取企业微信标签列表
	 *
	 * @author chan
	 * @date 2022-03-24 下午1:58
	 */
	@RequestMapping("/get/corpTagList")
	public Result<Page<CorpTagGroupView>> getCorpTagList(String groupName, @RequestParam(defaultValue = "0") Integer start, @RequestParam(defaultValue = "10") Integer limit) {
		Page<CorpTagGroupView> tagGroupList = corpTagService.getCorpTagList(groupName, start, limit);
		return GatewayResponse.SUCCESS.newBuilder().toResult(tagGroupList);
	}

	/**
	 * 规则列表展示
	 */
	@GetMapping("/get/ruleList")
	public Result<List<CorpTagRule>> rules() {
		List<CorpTagRule> corpTagRules = beanSearcher.searchAll(CorpTagRule.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(corpTagRules);
	}

	/**
	 * 新增规则
	 */
	@PostMapping("/post/rule")
	public Result<String> postRule(CorpTagRule rule) {
		corpTagService.saveOrUpdateRule(rule);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 编辑规则
	 */
	@PutMapping("/put/rule")
	public Result<String> updateRule(CorpTagRule rule) {
		corpTagService.saveOrUpdateRule(rule);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 标签管理详情
	 */
	@GetMapping("/get/corpTag")
	public Result<Page<CorpTagBackView>> getCorpTagBackView(@RequestParam(defaultValue = "0") Integer start, @RequestParam(defaultValue = "10") Integer limit, @RequestParam(required = false, defaultValue = "1") Integer type, String name, String[] groupIds, String sort, String order, String tagIdStr) throws Exception {
		Long adminId = StpUtil.getLoginIdAsLong();
		//指定管理员可查看标签
		//马慧文
		if (adminId == 75l) {
			tagIdStr = "etvj2DPQAAxqh9QlGJG4N1EbdQBNYGAA";
		}
		Page<CorpTagBackView> corpTagBackViews = corpTagService.getCorpTagBackView(start, limit, type, name, groupIds, sort, order, tagIdStr);
		return GatewayResponse.SUCCESS.newBuilder().toResult(corpTagBackViews);
	}

	/**
	 * 标签对应规则详情
	 */
	@GetMapping("/get/corpTag/rule/detail/{id}")
	public Result<List<CorpTagRuleRelation>> getCorpTagRuleDetail(@PathVariable Long id) {
		return GatewayResponse.SUCCESS.newBuilder().toResult(corpTagService.getCorpTagRuleDetail(id));
	}

	/**
	 * 客户详情
	 */
	@GetMapping("/get/corpUser/detail")
	public Result<Page<CorpUserView>> getCorpUserDetail(String tagId, String groupId, @RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "10") Integer limit) {
		Page<CorpUserView> result = corpTagService.getCorpUserDetail(tagId, groupId, start, limit);
		return GatewayResponse.SUCCESS.newBuilder().toResult(result);
	}

	/**
	 * 标签组内标签详情
	 */
	@GetMapping("/get/groupTag/detail/{groupId}")
	public Result<List<CorpTag>> getGroupTagDetail(@PathVariable String groupId) {
		List<CorpTag> corpTags = corpTagService.getGroupTagDetail(groupId);
		return GatewayResponse.SUCCESS.newBuilder().toResult(corpTags);
	}

	/**
	 * 导出标签下所有客户
	 */
	@GetMapping("/export")
	public void export(String[] tagId, String[] groupId, HttpServletResponse response) {
		List<CorpUserTagExcelData> list = corpTagService.exportUserTagDetail(tagId, groupId);
		EasyExcelUtils.createExcelStreamMutilByEasyExcel(response, CorpUserTagExcelData.class, list, "标签下客户详情", "标签下客户详情", ExcelTypeEnum.XLSX, null);
	}

	/**
	 * 企业微信标签 头部数据
	 */
	@GetMapping("/get/headData")
	public Result<CorpTagHeadDataView> getCorpTagHeadDataView() {
		CorpTagHeadDataView corpTagHeadDataView = corpTagService.getCorpTagHeadDataView();
		return GatewayResponse.SUCCESS.newBuilder().toResult(corpTagHeadDataView);
	}

	/**
	 * 按照标签时间，消费时间 查看用户总览
	 */
	@GetMapping("/get/tagUser/headData")
	public Result<UserOrderDetailView> tagUserHeadData(@RequestParam(required = true) String tagIdStr, String startTime, String endTime, String consumeStTime, String consumeEtTime) {
		Assert.notEmpty(tagIdStr, "请输入对应标签");
		return GatewayResponse.SUCCESS.newBuilder().toResult(corpTagService.tagUserHeadData(tagIdStr,startTime,endTime,consumeStTime,consumeEtTime));
	}

	/**
	 * 通过标签 查找进粉订单记录
	 */
	@GetMapping("/get/tagUser/fansData")
	public Result<Page<UserOrderDetailView> > getTagUserData(@RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "10") Integer limit, @RequestParam(required = true) String tagIdStr, String startTime, String endTime, String consumeStTime, String consumeEtTime) {
		Assert.notEmpty(tagIdStr, "请输入对应标签");
		Page<UserOrderDetailView>  page = corpTagService.getTagUserData(start, limit, tagIdStr, startTime, endTime, consumeStTime, consumeEtTime);
		return GatewayResponse.SUCCESS.newBuilder().toResult(page);
	}

	/**
	 * 通过标签 查找标签用户历史订单记录
	 */
	@GetMapping("/get/tagUser/pastData")
	public Result<Page<UserOrderDetailView>> getTagUserPastRecord(@RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "10") Integer limit, @RequestParam(required = true) String tagIdStr, String startTime, String endTime, String consumeStTime, String consumeEtTime) {
		Assert.notEmpty(tagIdStr, "请输入对应标签");
		Page<UserOrderDetailView> page = corpTagService.getTagUserPastRecord(start, limit, tagIdStr, startTime, endTime, consumeStTime, consumeEtTime);
		return GatewayResponse.SUCCESS.newBuilder().toResult(page);
	}

	/**
	 * 当日进粉户订单详情
	 */
	@GetMapping("/get/tagUser/time/orderData")
	public Result<Page<CorpTagUserOrderDataView>> getTagUserTimeOrderData(@RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "10") Integer limit, @RequestParam(required = true) String tagIdStr, String toDate, String startTime, String endTime) {
		Page<CorpTagUserOrderDataView> page = corpTagService.getTagUserTimeOrderData(start, limit, tagIdStr, toDate, startTime, endTime);
		return GatewayResponse.SUCCESS.newBuilder().toResult(page);
	}

	/**
	 * 当日标签用户 本月下单情况
	 */
	@GetMapping("/get/fans/toMonth/orders")
	public Result<UserOrderDetailView> getFansUserToMonthOrdersDetail(String tagId, String date) {
		UserOrderDetailView userOrderDetailView = corpTagService.getFansUserToMonthOrdersDetail(tagId, date);
		return GatewayResponse.SUCCESS.newBuilder().toResult(userOrderDetailView);
	}
}
