package com.ssj.aijia.common.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.ssj.bean.weixin.libmy.domain.LibVipBind;
import com.ssj.bean.weixin.problem.domain.CnArea;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.security.annotation.IgnoreSecurity;
import com.ssj.framework.core.util.ResponseConstant;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.conch.utils.MapUtils;
import com.ssj.service.weixin.library.service.LibVipBindService;
import com.ssj.service.weixin.problem.service.CnAreaService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author Mount Liu
 * @date 2019/4/9
 */
@Api(value = "工具类相关公共接口", tags = "工具类相关公共接口")
@RestController
@RequestMapping("/api/util")
public class UtilController extends BaseController {

    @Reference(version = "1.0", timeout = 60000)
    private CnAreaService cnAreaService;
    @Reference(version = "1.0", timeout = 60000)
    private LibVipBindService libVipBindService;

    @ApiOperation(value = "更新sys_cn_area的经纬度信息", httpMethod = "POST")
    @IgnoreSecurity
    @RequestMapping(value = "/updateLonAndLatOfCnArea", method = RequestMethod.POST)
    public ResponseEntity updateLonAndLatOfCnArea(@RequestParam(name = "createTime") String createTime) {
        ResponseEntity responseEntity = new ResponseEntity();
        try {
            List<CnArea> list = cnAreaService.findListByCreateTime(createTime);
            for (CnArea cnArea : list) {
                if (StringUtils.isNotBlank(cnArea.getSchoolAddress())) {
                    JSONObject location = MapUtils.getLngAndLat(cnArea.getSchoolAddress());
                    Thread.sleep(500);
                    if (location != null) {
                        cnArea.setSchoolLng(location.getString("lng"));
                        cnArea.setSchoolLat(location.getString("lat"));
                        cnAreaService.save(cnArea);
                    }
                }
            }
            responseEntity.success("成功");
        } catch (Exception e) {
            responseEntity.failure(ResponseConstant.CODE_500, e.getMessage());
        }
        return responseEntity;
    }

    @ApiOperation(value = "绑定老师和学生（问老师端）", httpMethod = "POST")
    @IgnoreSecurity
    @RequestMapping(value = "/bindTeacherAndVip", method = RequestMethod.POST)
    public ResponseEntity bindTeacherAndVip(@RequestParam(name = "vipIds") String vipIds,
                                            @RequestParam(name = "teacherId") String teacherId,
                                            @RequestParam(name = "subject") String subject) {
        ResponseEntity responseEntity = new ResponseEntity();
        try {
            List<String> vipIdList = Arrays.asList(vipIds.split(","));
            int i = 0;
            for (String vipId : vipIdList) {
                LibVipBind libVipBind = libVipBindService.findByVipIdAndSubject(vipId, subject);
                if (libVipBind == null) {
                    libVipBind = new LibVipBind();
                    libVipBind.setCreateTime(new Date());
                    libVipBind.setVipId(vipId);
                    libVipBind.setSubject(subject);
                    libVipBind.setTeacherId(teacherId);
                } else {
                    libVipBind.setTeacherId(teacherId);
                }
                libVipBindService.save(libVipBind);
            }
            responseEntity.success("成功");
        } catch (Exception e) {
            responseEntity.failure(ResponseConstant.CODE_500, e.getMessage());
        }
        return responseEntity;
    }

}
