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

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.GoodsDonQaRelateMapper;
import com.cyksj.model.entity.GoodsDonQa;
import com.cyksj.model.entity.GoodsDonQaRelate;
import com.cyksj.model.views.CustomerConsultationRecordView;
import com.cyksj.model.views.CustomerConsultationStatisticsDataView;
import com.cyksj.model.views.GoodsDonQaView;
import com.cyksj.service.mange.GoodsDonQaService;
import com.cyksj.service.mange.RefreshCacheService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapBuilder;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

/** admin/平台管理/常见问题
 * 项目名: yhlxj2
 * 文件名: GoodsDonQuestionController
 * 创建者: JavaZou
 * 创建时间:2024/5/24 15:52
 */
@RequestMapping("/manage/goods/question")
@RestController
@RequiredArgsConstructor
public class GoodsDonQuestionController {

	private final GoodsDonQaService goodsDonQaService;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final RefreshCacheService refreshCacheService;

	private final GoodsDonQaRelateMapper goodsDonQaRelateMapper;


	/**
	 * 新增问题
	 */
	@PostMapping("/post")
	public Result<String> post(@RequestBody GoodsDonQa goodsDonQa) {
		goodsDonQaService.saveOrdUpdateQa(goodsDonQa);
		refreshCacheService.goodsQuestionCache();
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}


	/**
	 * 删除问题
	 */
	@DeleteMapping("/del/{id}")
	public Result<String> delById(@PathVariable Long id) {
		GoodsDonQa byId = goodsDonQaService.getById(id);
		if (byId != null && byId.getDeleted()) {
			byId.setDeleted(false);
			goodsDonQaService.updateById(byId);
			goodsDonQaRelateMapper.update(null, Wrappers.lambdaUpdate(GoodsDonQaRelate.class)
					.set(GoodsDonQaRelate::getDeleted, 0)
					.eq(GoodsDonQaRelate::getQuestionId, id)
					.eq(GoodsDonQaRelate::getDeleted, 1));
			refreshCacheService.goodsQuestionCache();
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 更新问题
	 */
	@PutMapping("/update")
	public Result<String> update(@RequestBody GoodsDonQa goodsDonQa) {
		goodsDonQaService.saveOrdUpdateQa(goodsDonQa);
		refreshCacheService.goodsQuestionCache();
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}


	/**
	 * 上下架
	 */
	@PutMapping("/update/status/{id}")
	public Result<String> updateStatusById(@PathVariable Long id) {
		GoodsDonQa byId = goodsDonQaService.getById(id);
		if (byId != null) {
			byId.setStatus(!byId.getStatus());
			goodsDonQaService.updateById(byId);
			refreshCacheService.goodsQuestionCache();
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 批量上下架
	 */
	@PutMapping("/update/status/batch")
	public Result<String> batchUpdateStatusByIds(@RequestParam("ids") List<Long> ids, Boolean status) {
		goodsDonQaService.update(null, Wrappers.lambdaUpdate(GoodsDonQa.class)
				.set(GoodsDonQa::getStatus, status)
				.ne(GoodsDonQa::getStatus, status)
				.in(GoodsDonQa::getId, ids)
				.eq(GoodsDonQa::getDeleted, 1));
		refreshCacheService.goodsQuestionCache();
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}


	/**
	 * 问题列表
	 */
	@GetMapping("/get")
	public Result<SearchResult<GoodsDonQaView>> get() {
		SearchResult<GoodsDonQaView> search = beanSearcher.search(GoodsDonQaView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 问题详情
	 */
	@GetMapping("/get/detail/{id}")
	public Result<GoodsDonQa> getDetailById(@PathVariable Long id) {
		GoodsDonQa byId = goodsDonQaService.getDetailById(id);
		return GatewayResponse.SUCCESS.newBuilder().toResult(byId);
	}

	/**
	 * 排序
	 */
	@PutMapping("/sorted")
	public Result<String> sorted(@RequestBody GoodsDonQa goodsDonQa) {
		Long id = goodsDonQa.getId();
		Integer sorted = goodsDonQa.getSorted();
		GoodsDonQa byId = goodsDonQaService.getById(id);
		if (byId != null) {
			byId.setSorted(sorted);
			goodsDonQaService.updateById(byId);
			refreshCacheService.goodsQuestionCache();
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}


	/**
	 * 客户咨询问题记录列表
	 */
	@GetMapping("/get/consultation/record")
	public Result<SearchResult<CustomerConsultationRecordView>> getConsultationRecord() {
		SearchResult<CustomerConsultationRecordView> search = beanSearcher.search(CustomerConsultationRecordView.class, MapUtils.flatBuilder(request.getParameterMap())
				.orderBy(CustomerConsultationRecordView::getUpdateTime).desc()
				.build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}


	/**
	 * 客户咨询问题记录列表 数据统计
	 */
	@GetMapping("/get/consultation/statistics/data")
	public Result<List<CustomerConsultationStatisticsDataView>> getConsultationStatisticsData(String startTime, String endTime) {
		String timeSql = StrUtil.EMPTY;
		if (startTime != null && endTime != null) {
			timeSql = String.format(" ccr.created_time >= '%s' and ccr.created_time < '%s'", startTime, endTime);
		}
		MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
		if (StrUtil.isNotBlank(timeSql)) {
			builder.put("time", timeSql);
		}
		List<CustomerConsultationStatisticsDataView> customerConsultationStatisticsDataViews = beanSearcher.searchAll(CustomerConsultationStatisticsDataView.class, builder.build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(customerConsultationStatisticsDataViews);
	}
}
