package com.huiju.estateagents.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.entity.TaMiniapp;
import com.huiju.estateagents.entity.TaOrgCity;
import com.huiju.estateagents.entity.TdCity;
import com.huiju.estateagents.mapper.TaOrgCityMapper;
import com.huiju.estateagents.service.ITaMiniappService;
import com.huiju.estateagents.service.ITdCityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-08-07
 */
@RestController
@RequestMapping("/api")
public class TdCityController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TdCityController.class);

    @Autowired
    public ITdCityService iTdCityService;

    @Autowired
    private TaOrgCityMapper taOrgCityMapper;

    @Autowired
    private ITaMiniappService iTaMiniappService;

    /**
     * 中心端城市
     *
     * @return
     */
    @RequestMapping(value = "/center/tdCity", method = RequestMethod.GET)
    public ResponseBean tdCityList(@RequestParam(value = "leveltype", required = false) String leveltype) {
        ResponseBean responseBean = new ResponseBean();
        try {
            List<TdCity> result = iTdCityService.selectCity(leveltype, "");
            responseBean.addSuccess(result);
        } catch (Exception e) {
            logger.error("tdCityList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 业务端城市
     */
    @RequestMapping(value = "/admin/tdCityList/tdCity", method = RequestMethod.GET)
    public ResponseBean adminTdCityList(HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean<>();
        List<TaOrgCity> taOrgCityList = taOrgCityMapper.selectAdminTdCityList(getOrgId(request));
        responseBean.addSuccess(taOrgCityList);
        return responseBean;
    }

    /**
     * 业务端城市
     */
    @RequestMapping(value = "/admin/tdCityList/tdAreaCity", method = RequestMethod.GET)
    public ResponseBean adminTdAreaCityList(@RequestParam(value = "cityId", required = false) Integer cityId,
                                    HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean<>();
        List<TaOrgCity> taOrgCityList = taOrgCityMapper.selectAdminTdAreaCityList(getOrgId(request),cityId);
        responseBean.addSuccess(taOrgCityList);
        return responseBean;
    }


    /**
     * 查询列表
     *
     * @return
     */
    @RequestMapping(value = "/wx/tdCity", method = RequestMethod.GET)
    public ResponseBean tdCityListWx(@RequestParam(value = "leveltype", required = false) String leveltype, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        String appId = request.getHeader("appId");
        try {
            //通过appid获取orgId
            QueryWrapper<TaMiniapp> taMiniappQueryWrapper = new QueryWrapper<>();
            taMiniappQueryWrapper.eq("miniapp_id", appId);
            TaMiniapp taMiniapp = iTaMiniappService.getOne(taMiniappQueryWrapper);
            /**leveltype,暂时保留*/
            List<TdCity> result = iTdCityService.selectWxCity(leveltype, taMiniapp.getOrgId());
            responseBean.addSuccess(result);
        } catch (Exception e) {
            logger.error("tdCityList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     *
     * @param tdCity 实体对象
     * @return
     */
    @RequestMapping(value = "/tdCity", method = RequestMethod.POST)
    public ResponseBean tdCityAdd(@RequestBody TdCity tdCity) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTdCityService.save(tdCity)) {
                responseBean.addSuccess(tdCity);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            logger.error("tdCityAdd -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     */
    @ResponseBody
    @RequestMapping(value = "/tdCity/{id}", method = RequestMethod.DELETE)
    public ResponseBean tdCityDelete(@PathVariable Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTdCityService.removeById(id)) {
                responseBean.addSuccess("success");
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            logger.error("tdCityDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     *
     * @param tdCity 实体对象
     * @return
     */
    @RequestMapping(value = "/edit/tdCity/", method = RequestMethod.PUT)
    public ResponseBean tdCityUpdate(@PathVariable List<TdCity> cities,
                                     @RequestBody TdCity tdCity) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTdCityService.updateBatchById(cities)) {
                responseBean.addSuccess(tdCity);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            logger.error("tdCityUpdate -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     *
     * @param id 实体ID
     */
    @RequestMapping(value = "/tdCity/{id}", method = RequestMethod.GET)
    public ResponseBean tdCityGet(@PathVariable Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTdCityService.getById(id));
        } catch (Exception e) {
            logger.error("tdCityDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据经纬度查询城市ID
     * 经度在前，纬度在后，经纬度间以“,”分割，经纬度小数点后不要超过 6 位
     *
     * @return
     */
    @RequestMapping(value = "/wx/location/city", method = RequestMethod.GET)
    public ResponseBean wxLocationCityDetail(@RequestParam(value = "location") String location, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        Integer orgId = getOrgId(request);
        try {
            TdCity tdCity = iTdCityService.getLocationCity(location, orgId, null);
            responseBean.addSuccess(tdCity);
        } catch (Exception e) {
            logger.error("tdCityList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
