package com.inspur.web.controller.work;

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

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.inspur.common.annotation.Anonymous;
import com.inspur.domain.ElevatorInfo;
import com.inspur.service.IElevatorInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.inspur.common.annotation.Log;
import com.inspur.common.core.controller.BaseController;
import com.inspur.common.core.domain.AjaxResult;
import com.inspur.common.enums.BusinessType;
import com.inspur.common.utils.poi.ExcelUtil;
import com.inspur.common.core.page.TableDataInfo;

/**
 * 电梯信息Controller
 *
 * @author inspur
 * @date 2024-10-12
 */
@RestController
@RequestMapping("/elevator/info")
public class ElevatorInfoController extends BaseController
{
    @Autowired
    private IElevatorInfoService elevatorInfoService;

    /**
     * 查询电梯信息列表
     */
    // @PreAuthorize("@ss.hasPermi('elevator:info:list')")
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(ElevatorInfo elevatorInfo)
    {
        startPage();
        List<ElevatorInfo> list = elevatorInfoService.selectElevatorInfoList(elevatorInfo);
        return getDataTable(list);
    }

    /**
     * 导出电梯信息列表
     */
    @PreAuthorize("@ss.hasPermi('elevator:info:export')")
    @Log(title = "电梯信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElevatorInfo elevatorInfo)
    {
        List<ElevatorInfo> list = elevatorInfoService.selectElevatorInfoList(elevatorInfo);
        ExcelUtil<ElevatorInfo> util = new ExcelUtil<ElevatorInfo>(ElevatorInfo.class);
        util.exportExcel(response, list, "电梯信息数据");
    }

    /**
     * 获取电梯信息详细信息
     */
    // @PreAuthorize("@ss.hasPermi('elevator:info:query')")
    @GetMapping(value = "/{id}")
    @Anonymous
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(elevatorInfoService.selectElevatorInfoById(id));
    }

    /**
     * 新增电梯信息
     */
    // @PreAuthorize("@ss.hasPermi('elevator:info:add')")
    @Log(title = "电梯信息", businessType = BusinessType.INSERT)
    @PostMapping
    @Anonymous
    public AjaxResult add(@RequestBody ElevatorInfo elevatorInfo)
    {
        return toAjax(elevatorInfoService.insertElevatorInfo(elevatorInfo));
    }

    /**
     * 修改电梯信息
     */
    // @PreAuthorize("@ss.hasPermi('elevator:info:edit')")
    @Log(title = "电梯信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @Anonymous
    public AjaxResult edit(@RequestBody ElevatorInfo elevatorInfo)
    {
        return toAjax(elevatorInfoService.updateElevatorInfo(elevatorInfo));
    }

    /**
     * 删除电梯信息
     */
    // @PreAuthorize("@ss.hasPermi('elevator:info:remove')")
    @Log(title = "电梯信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Anonymous
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(elevatorInfoService.deleteElevatorInfoByIds(ids));
    }


    /**
     * 获取手机号归属地
     * @param phoneNum 手机号
     * @return 归属地
     */
    @GetMapping("/phone")
    @Anonymous
    public AjaxResult getRegionInfoByPhoneAPI(String phoneNum){
        // 360提供的一个接口 能返回省份和城市
        try {
            String url = "https://cx.shouji.360.cn/phonearea.php?number=" + phoneNum;
            String responseBody = HttpUtil.createGet(url).execute().body();
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            if (!"0".equals(jsonObject.getInteger("code").toString())) {
                AjaxResult.success();
            }
            String city = jsonObject.getJSONObject("data").getString("city");
            return AjaxResult.success(city);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }
}
