package com.codvision.monitor.controller.manager;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.monitor.entity.Website;
import com.codvision.monitor.entity.WebsiteAccessRecord;
import com.codvision.monitor.entity.vo.WebsiteAccessRecordVO;
import com.codvision.monitor.entity.vo.WebsiteVO;
import com.codvision.monitor.service.IWebsiteAccessRecordService;
import com.codvision.monitor.service.IWebsiteService;
import com.codvision.webcore.annotation.ApiSecret;
import com.codvision.webcore.bean.QueryPage;
import com.google.common.base.Preconditions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;

/**
 * 站点信息 管理模块
 *
 * @author lingee
 * @date 2023-11-20
 */
@RestController
@RequestMapping("/v1/manager/website")
@Api(tags = "平台管理端-埋点监控管理模块")
public class ManagerWebsiteController {

    @Resource
    private IWebsiteService websiteService;

    @Resource
    private IWebsiteAccessRecordService websiteAccessRecordService;

    @ApiOperation(value = "获取站点信息分页列表", notes = "获取站点信息分页列表接口")
    @PostMapping("/findAll")
    public ResponseEntity<IPage<WebsiteVO>> getPageList(@RequestParam(required = false) String websiteName,
                                                        @RequestParam(required = false) String websiteKey,
                                                        @RequestParam(required = false, defaultValue = "1") int page,
                                                        @RequestParam(required = false, defaultValue = "10") int size) {
        Website queryBean = new Website();
        queryBean.setWebsiteName(websiteName);
        queryBean.setKey(websiteKey);
        IPage<WebsiteVO> pageResult = websiteService.getPageResult(new QueryPage<>(queryBean, page, size));
        return new ResponseEntity<IPage<WebsiteVO>>().ok().data(pageResult);
    }

    @PostMapping("/getErrorMsgList")
    @ApiOperation(value = "错误信息列表", notes = "获取错误信息列表接口")
    public ResponseEntity<IPage<WebsiteAccessRecord>> getErrorMsgList(@RequestParam(value = "websiteKey") String websiteKey,
                                                                      @RequestParam(required = false, defaultValue = "1") int page,
                                                                      @RequestParam(required = false, defaultValue = "10") int size) {
        ResponseEntity<IPage<WebsiteAccessRecord>> responseEntity = new ResponseEntity<>();

        LambdaQueryWrapper<WebsiteAccessRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WebsiteAccessRecord::getWebsiteKey, websiteKey)
                .eq(WebsiteAccessRecord::getType, 1)
                .orderByDesc(WebsiteAccessRecord::getCreateTime);

        IPage<WebsiteAccessRecord> pageResult = websiteAccessRecordService
                .page(new QueryPage<WebsiteAccessRecord>(page, size).getPage(), queryWrapper);

        return responseEntity.ok().data(pageResult);
    }

    @ApiOperation(value = "新增站点信息", notes = "新增站点信息接口")
    @PostMapping("/add")
    public ResponseEntity<String> saveWebsite(@Valid @RequestBody Website website) {
        Website isExist = websiteService.getOne(Wrappers.<Website>lambdaQuery().eq(Website::getWebsiteName, website.getWebsiteName()));
        website.setKey(UUID.randomUUID().toString());
        website.setIsMain(false);
        Preconditions.checkArgument(isExist == null || isExist.getId().equals(website.getId()), "网站名称已存在！");
        websiteService.save(website);
        return new ResponseEntity<String>().ok().message("添加成功");
    }

    @ApiOperation(value = "删除站点信息", notes = "删除站点信息接口")
    @PostMapping("/del")
    public ResponseEntity<String> deleteById(@ApiParam(value = "站点信息ID", required = true) @RequestParam Integer id) {
        websiteService.removeById(id);
        return new ResponseEntity<String>().ok().message("删除成功");
    }

    @ApiOperation(value = "更新站点信息", notes = "更新站点信息接口")
    @PostMapping("/update")
    public ResponseEntity<String> updateWebsite(@RequestBody Website website) {
        if (ObjectUtil.isEmpty(website.getId())) {
            throw new BusinessException(30002);
        }
        websiteService.updateById(website);
        return new ResponseEntity<String>().ok().message("更新成功");
    }

    @ApiSecret(ignore = true)
    @PostMapping("/addRecord")
    @ApiOperation(value = "添加访问记录", notes = "添加访问记录")
    public ResponseEntity<String> addRecord(@Validated @RequestBody WebsiteAccessRecord accessRecord, HttpServletRequest request) {
        websiteAccessRecordService.addRecord(accessRecord, request);
        return new ResponseEntity<String>().ok();
    }

    @PostMapping("/getPvCount")
    @ApiOperation(value = "浏览量PV统计", notes = "浏览量PV统计")
    public ResponseEntity<List<WebsiteAccessRecordVO>> getPvCount(@RequestParam(value = "websiteKey") String websiteKey,
                                                                  @RequestParam(value = "startTime") String startTime,
                                                                  @RequestParam(value = "endTime") String endTime) {
        ResponseEntity<List<WebsiteAccessRecordVO>> responseEntity = new ResponseEntity<>();
        return responseEntity.data(websiteAccessRecordService.getPvCountList(websiteKey, startTime, endTime));
    }

    @PostMapping("/getUvCount")
    @ApiOperation(value = "访客数UV统计", notes = "访客数UV统计")
    public ResponseEntity<List<WebsiteAccessRecordVO>> getUvCount(@RequestParam(value = "websiteKey") String websiteKey,
                                                                  @RequestParam(value = "startTime") String startTime,
                                                                  @RequestParam(value = "endTime") String endTime) {
        ResponseEntity<List<WebsiteAccessRecordVO>> responseEntity = new ResponseEntity<>();
        return responseEntity.data(websiteAccessRecordService.getUvCountList(websiteKey, startTime, endTime));
    }
}
