package com.mosty.base.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mosty.base.model.dto.base.GetYqListDto;
import com.mosty.base.model.dto.base.RsHjyjSearchDto;
import com.mosty.base.model.entity.jcgl.TbJcglXfll;
import com.mosty.base.model.entity.yjzl.TbYjxx;
import com.mosty.base.model.entity.yszx.TbYsGajg;
import com.mosty.base.model.vo.yszx.TbYsGajgVo;
import com.mosty.base.service.DeptService;
import com.mosty.base.service.GajgSearchService;
import com.mosty.base.utils.OkHttpUtils;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.base.entity.log.SysOperLog;
import com.mosty.common.config.Excel.ExcelUtil;
import com.mosty.common.core.business.entity.SysLogininfor;
import com.mosty.common.core.business.entity.request.LoginLogPage;
import com.mosty.common.core.business.mapper.SysLogininforMapper;
import com.mosty.common.core.util.http.HttpUtils;
import com.mosty.common.token.JwtSysUser;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.operation.log.entity.VO.OperlogPage;
import com.mosty.operation.log.mapper.SysOperLogMapper;
import com.mosty.operation.log.service.SysOperLogService;
import io.lettuce.core.GeoCoordinates;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("other")
@Api(tags = "其他的一些接口")
@AllArgsConstructor
public class OtherController {

    private final GajgSearchService gajgSearchService;
    private final SysOperLogMapper sysOperLogMapper;
    private final SysLogininforMapper sysLogininforMapper;
    private final DeptService deptService;







    @PostMapping("getGajg")
    @ApiOperation("公安机关所有信息")
    @JwtSysUser
    public ResponseResult<TbYsGajgVo> getGajg() {
        return ResponseResult.success(this.gajgSearchService.getGajg());
    }

    @PostMapping("getZxd")
    @ApiOperation("获取区域中心点")
    @JwtSysUser
    public ResponseResult<GeoCoordinates> getZxd(@RequestBody List<BigDecimal[]> dtm) {
        int total = dtm.size();
        double X = 0, Y = 0, Z = 0;
        for (BigDecimal[] yjxx : dtm) {
            double jd, wd, x, y, z;
            jd = yjxx[0].doubleValue() * Math.PI / 180;
            wd = yjxx[1].doubleValue() * Math.PI / 180;
            x = Math.cos(wd) * Math.cos(jd);
            y = Math.cos(wd) * Math.sin(jd);
            z = Math.sin(wd);
            X += x;
            Y += y;
            Z += z;
        }
        X = X / total;
        Y = Y / total;
        Z = Z / total;
        double jd = Math.atan2(Y, X);
        double Hyp = Math.sqrt(X * X + Y * Y);
        double wd = Math.atan2(Z, Hyp);
        return ResponseResult.success(GeoCoordinates.create(jd * 180 / Math.PI, wd * 180 / Math.PI));
    }

    @PostMapping("/exportOperlog")
    @ApiOperation("导出查询日志")
    @Log(title = "导出查询日志", businessType = BusinessType.OTHER)
    public void exportOperlog(OperlogPage dto, HttpServletResponse response) {
        LambdaQueryWrapper<SysOperLog> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotBlank(dto.getTitle()), SysOperLog::getTitle, dto.getTitle())
                .like(StringUtils.isNotBlank(dto.getOperName()), SysOperLog::getOperName, dto.getOperName())
                .eq(Objects.nonNull(dto.getBusinessType()), SysOperLog::getBusinessType, dto.getBusinessType())
                .eq(Objects.nonNull(dto.getStatus()), SysOperLog::getStatus, dto.getStatus())
                .orderByDesc(SysOperLog::getOperId).last(" limit 10000");
        List<SysOperLog> list = sysOperLogMapper.selectList(qw);

        ExcelUtil<SysOperLog> util = new ExcelUtil<
                >(SysOperLog.class);
        util.exportExcel(response, list, "巡逻辅警导出", "巡逻辅警导出");
    }


    @PostMapping("/exportLoginLog")
    @ApiOperation("导出登陆日志")
    @Log(title = "导出登陆日志", businessType = BusinessType.OTHER)
    public void exportLoginLog(LoginLogPage dto, HttpServletResponse response) {
        QueryWrapper<SysLogininfor> qw = new QueryWrapper<>();
        String ssbm = deptService.getSsbm(null, null);
        qw.lambda().like(StringUtils.isNotBlank(dto.getUserName()), SysLogininfor::getUserName, dto.getUserName())
                .eq(Objects.nonNull(dto.getStatus()), SysLogininfor::getStatus, dto.getStatus())
                .likeRight(StringUtils.isNotBlank(ssbm), SysLogininfor::getSsbmdm, ssbm);
        qw.lambda().orderByDesc(SysLogininfor::getInfoId).last(" limit 10000");
        List<SysLogininfor> list = sysLogininforMapper.selectList(qw);
        ExcelUtil<SysLogininfor> util = new ExcelUtil<>(SysLogininfor.class);
        util.exportExcel(response, list, "导出登陆日志", "导出登陆日志");
    }





}
