package com.tzy.controller.mall;

import com.github.pagehelper.PageInfo;
import com.tzy.annotation.ApiLog;
import com.tzy.common.config.handle.ApiVersion;
import com.tzy.common.constant.Constants;
import com.tzy.common.core.controller.BaseController;
import com.tzy.common.dto.InDto;
import com.tzy.common.dto.OutDTO;
import com.tzy.common.enums.BusinessType;
import com.tzy.common.utils.StringUtils;
import com.tzy.pojo.item.EvaluationRecord;
import com.tzy.pojo.item.dto.EvaluationRecordDTO;
import com.tzy.sportcard.api.service.MerchantApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.teasoft.bee.osql.Condition;
import org.teasoft.bee.osql.Op;
import org.teasoft.bee.osql.OrderType;
import org.teasoft.bee.osql.SuidRich;
import org.teasoft.honey.osql.core.ConditionImpl;

import javax.annotation.Resource;
import javax.persistence.Version;
import java.util.List;

/**
 * @author by po'yi
 * @Classname EvaluationController
 * @Description Evaluation
 * @Date 2022/2/28 11:56
 */
@Slf4j
@Api("Evaluation")
@RestController
@RequestMapping("/api/{version}/sku")
public class EvaluationController extends BaseController {

	@Resource
	private SuidRich suidRich;
	@Resource
	private MerchantApiService merchantApiService;

	@ApiLog(title = "根据type和refId评论查询",businessType = BusinessType.UPDATE,indto = "{{inDto}}")
	@ResponseBody
	@ApiOperation("根据type和refId评论查询")
	@RequestMapping(value = "/getEvaluation", method = RequestMethod.POST)
	public OutDTO getEvaluation(@RequestBody InDto inDto){
		EvaluationRecord evaluationRecord = new EvaluationRecord();
		evaluationRecord.setType(inDto.getString("type"));
		//组团id,订单id
		if(Strings.isNotEmpty(inDto.getString("refId"))){
			evaluationRecord.setRefId(Long.valueOf(inDto.getString("refId")));
		}
		int page=inDto.getPageNo()!=null&&inDto.getPageNo()>0?inDto.getPageNo():1;
		int size=inDto.getPageSize()!=null&&inDto.getPageSize()>0?inDto.getPageSize():10;
		//middle,bad,good
		String level = inDto.getString("level");
		Condition condition=new ConditionImpl();
		if(Constants.EVALUATION_LEVEL_GOOD.equals(level)){
			condition.op("value", Op.eq,5);
		}else if(Constants.EVALUATION_LEVEL_BAD.equals(level)){
			condition.op("value", Op.lt,3);
		}else if (Constants.EVALUATION_LEVEL_MIDDLE.equals(level)){
			condition.op("value", Op.gt,2);
			condition.op("value", Op.lt,5);
		}
		int offSet=(page-1)*size;
		condition.start(offSet).size(size);
		condition.orderBy("id", OrderType.DESC);
		evaluationRecord.setStatus(1); // 查询显示的评价
		List<EvaluationRecord> evaluations = suidRich.select(evaluationRecord, condition);
		if(!CollectionUtils.isEmpty(evaluations)){
			evaluations.forEach(evaluation ->{
				String nickname = evaluation.getNickName();
				if(Strings.isNotEmpty(nickname)){
					// 是否匿名：0否 1是
					if(evaluation.getAnonymous() == 0){
						evaluation.setNickName(StringUtils.desensitizeName(nickname));
					}else if(evaluation.getAnonymous() == 1){
						evaluation.setNickName("匿名用户");
						evaluation.setAvatar("https://static.public.hobbystock.cn/applet/share/share_logo2.png");
					}
					//商家展示勾选未有文字内容的评价时，默认为：ta未作出评价内容
					if(Strings.isEmpty(evaluation.getContent())){
						evaluation.setContent("ta未作出评价内容");
					}
				}
			});
		}
		return OutDTO.ok().put("evaluations",evaluations);
	}

	@ApiLog(title = "根据type和merchantId查询评论",businessType = BusinessType.SEARCH,indto = "{{inDto}}")
	@ResponseBody
	@ApiVersion(2.0)
	@ApiOperation("根据type和merchantId查询评论")
	@RequestMapping(value = "/getEvaluation", method = RequestMethod.POST)
	public OutDTO getEvaluationV2(@RequestBody EvaluationRecordDTO evaluationRecordDTO){
		// 商家id
		if(evaluationRecordDTO == null || evaluationRecordDTO.getId() == null){
			return OutDTO.error500("参数为空");
		}
		evaluationRecordDTO.setId(evaluationRecordDTO.getId());
		int page = evaluationRecordDTO.getPageNo()!=null&&evaluationRecordDTO.getPageNo()>0?evaluationRecordDTO.getPageNo():1;
		int size = evaluationRecordDTO.getPageSize()!=null&&evaluationRecordDTO.getPageSize()>0?evaluationRecordDTO.getPageSize():10;
		PageInfo<EvaluationRecord> pageInfo = merchantApiService.getEvaluationV2(evaluationRecordDTO, page, size);
		return OutDTO.ok().put("evaluations", pageInfo);
	}

}
