package com.winsun.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.bean.Product;
import com.winsun.bean.School;
import com.winsun.bean.UniversityInfo;
import com.winsun.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 单宽办理接口
 * @Author xuede
 * @Date 2020/3/9 16:52
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/dankuan")
public class DankuanController {

     private static ProductMapper productMapper;

     private static SchoolMapper schoolMapper;

     private static AppMapper appMapper;

     private static UniversityInfoMapper universityInfoMapper;


     @Autowired
     public DankuanController(ProductMapper productMapper,SchoolMapper schoolMapper,AppMapper appMapper,UniversityInfoMapper universityInfoMapper ){
          DankuanController.productMapper=productMapper;
          DankuanController.schoolMapper=schoolMapper;
          DankuanController.appMapper=appMapper;
          DankuanController.universityInfoMapper=universityInfoMapper;
     }

     /**
      * 查询单宽办理接口
      * @return
      */
     @RequestMapping("/data")
     public ResponseData<Product> getdankuanData(@RequestParam("id") Integer id){
          //空，直接返回
          if (id==null){
               return  ResponseData.error("请输入合伙人id");
          }
          String schoolId = appMapper.selectSchoolId(id.toString());
          School school = schoolMapper.selectById(schoolId);
          String bandproductId = school.getBandproductId();
          if (StringUtils.isBlank(bandproductId)){
               return ResponseData.error("敬请期待");
          }
          Product product = productMapper.selectById(bandproductId);
          return  ResponseData.success(product);
     }

     @RequestMapping("/DomainName")
     public ResponseData<String> getDomainName(@RequestParam("schoolName") String schoolName){
          //空，直接返回
          if (StringUtils.isBlank(schoolName)){
               return ResponseData.error("请输入学校名称");
          }
          EntityWrapper<UniversityInfo> universityInfowrapper = new EntityWrapper<>();
          universityInfowrapper.setSqlSelect("university_domain_name as domainName",schoolName);
          universityInfowrapper.eq("university_name",schoolName);
          List<Map<String,Object>> universityInfos = universityInfoMapper.selectMaps(universityInfowrapper);
          if (CollectionUtils.isEmpty(universityInfos)){
               return ResponseData.error("查找不到学校相应数据");
          }
          if ( universityInfos.get(0).get("domainName")==null){
               return ResponseData.error("查找不到学校相应数据");
          }
          return  ResponseData.success(universityInfos.get(0).get("domainName").toString());
     }
}
