package com.iformall.controller.market;

import com.alibaba.fastjson.JSONObject;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.WxQuestion;
import com.iformall.domain.po.WxQuestionConfig;
import com.iformall.enums.EnumQuestionConfigStatus;
import com.iformall.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("wxQuestion")
@Api(description="问券调查")
public class WxQuestionController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
    private WxQuestionService wxQuestionService;


    @ApiOperation("查寻问券配置")
    @GetMapping("getConfig")
    @SystemControllerLog(description = "营销-问券调查-配置获取")
    public Result getQuestionConfig() {
		logger.debug("[" + getIpAddr() + "] WxQuestionController::getQuestionConfig");
		WxQuestionConfig wxQuestionConfig = new WxQuestionConfig();
		WxQuestion wxQuestion = new WxQuestion();
		wxQuestionConfig.setTenantId(getTenantId());
		wxQuestion.setTenantId(getTenantId());

		List<WxQuestionConfig>  list = wxQuestionService.findConfigList(wxQuestionConfig);
		if (list.size() > 0) {
			list.get(0).setQuestions(wxQuestionService.findList(wxQuestion));
			return new ResultData(list.get(0));
		}
		wxQuestionConfig.setQuestions(wxQuestionService.findList(wxQuestion));
		wxQuestionConfig.setQuestionList("");
		wxQuestionConfig.setCouponTypeList("");
		wxQuestionConfig.setStatus(EnumQuestionConfigStatus.OFF.getCode());
    	return new ResultData(wxQuestionConfig);
    }


	@ApiOperation("设置问券配置")
	@PostMapping("setConfig")
    @SystemControllerLog(description = "营销-问券调查-设置")
	public Result setQuestionConfig(@RequestBody WxQuestionConfig wxQuestionConfig) {
		logger.debug("[" + getIpAddr() + "] WxQuestionController::setQuestionConfig");
    	if (wxQuestionConfig == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
		if (wxQuestionConfig.getQuestionList() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
		if (wxQuestionConfig.getCouponTypeList() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);

		String[] arys1 = wxQuestionConfig.getQuestionList().split(",");
		List<Long> qs = new ArrayList<>();
		for (int i = 0; i < arys1.length; i++) {
			if (!arys1[i].isEmpty())
				qs.add(Long.parseLong(arys1[i]));
		}
		String[] arys2 = wxQuestionConfig.getCouponTypeList().split(",");
		List<Long> ct = new ArrayList<>();
		for (int i = 0; i < arys2.length; i++) {
			if (!arys2[i].isEmpty())
				ct.add(Long.parseLong(arys2[i]));
		}
		wxQuestionConfig.setQuestionList(JSONObject.toJSONString(qs));
		wxQuestionConfig.setCouponTypeList(JSONObject.toJSONString(ct));
		wxQuestionConfig.setTenantId(getTenantId());
		wxQuestionService.saveOrUpdateConfig(wxQuestionConfig);
		return new ResultData();
	}

}
