package com.ycwl.basic.controller.pc;

import com.github.pagehelper.PageInfo;
import com.ycwl.basic.model.mobile.statistic.req.CommonQueryReq;
import com.ycwl.basic.model.pc.scenic.entity.ScenicConfigEntity;
import com.ycwl.basic.model.pc.scenic.req.ScenicAddOrUpdateReq;
import com.ycwl.basic.model.pc.scenic.req.ScenicReqQuery;
import com.ycwl.basic.model.pc.scenic.resp.ScenicRespVO;
import com.ycwl.basic.service.mobile.AppStatisticsService;
import com.ycwl.basic.service.pc.ScenicService;
import com.ycwl.basic.storage.StorageFactory;
import com.ycwl.basic.storage.adapters.IStorageAdapter;
import com.ycwl.basic.storage.enums.StorageAcl;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.utils.WxMpUtil;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.model.pc.mp.MpConfigEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.List;

/**
 * @Author：longbinbin
 * @Date：2024/12/3 15:20
 */
@RestController
@RequestMapping("/api/scenic/v1")
@Api(tags = "景区管理")
public class ScenicController {

    @Autowired
    private ScenicService scenicService;

    @Autowired
    private ScenicRepository scenicRepository;
    @Autowired
    private AppStatisticsService appStatisticsService;

    @ApiOperation("分页查询景区")
    @PostMapping("/page")
    public ApiResponse<PageInfo<ScenicRespVO>> pageQuery(@RequestBody ScenicReqQuery scenicReqQuery) {
        return scenicService.pageQuery(scenicReqQuery);
    }
    @ApiOperation("查询景区列表")
    @PostMapping("/list")
    public ApiResponse<List<ScenicRespVO>> list(@RequestBody ScenicReqQuery scenicReqQuery) {
        return scenicService.list(scenicReqQuery);
    }
    @ApiOperation("查询景区详情")
    @GetMapping("/getDetail/{id}")
    public ApiResponse<ScenicRespVO> getDetail(@PathVariable Long id) {
        return scenicService.getById(id);
    }
    @ApiOperation("新增景区")
    @PostMapping("/add")
    public ApiResponse<Boolean> add(@RequestBody ScenicAddOrUpdateReq scenicAddReq) {
        return scenicService.add(scenicAddReq);
    }
    @ApiOperation("删除景区")
    @GetMapping("/delete/{id}")
    public ApiResponse<Boolean> delete(@PathVariable Long id) {
        return scenicService.deleteById(id);
    }
    @ApiOperation("修改景区")
    @PostMapping("/update")
    public ApiResponse<Boolean> update(@RequestBody ScenicAddOrUpdateReq scenicAddReq) {
        return scenicService.update(scenicAddReq);
    }
    @ApiOperation("修改景区状态")
    @GetMapping("/updateStatus/{id}")
    public ApiResponse<Boolean> updateStatus(@PathVariable Long id) {
        return scenicService.updateStatus(id);
    }
    @ApiOperation("新增景区配置")
    @PostMapping("/addConfig")
    public ApiResponse<Boolean> addConfig(@RequestBody ScenicConfigEntity scenicConfig) {
        return scenicService.addConfig(scenicConfig);
    }
    @ApiOperation("修改景区配置")
    @PostMapping("/updateConfig")
    public ApiResponse<Boolean> updateConfig(@RequestBody ScenicConfigEntity scenicConfig) {
        return scenicService.updateConfigById(scenicConfig);
    }

    @ApiOperation("查询景区配置")
    @GetMapping("/config/{id}")
    public ApiResponse<ScenicConfigEntity> getConfig(@PathVariable("id") Long id) {
        return ApiResponse.success(scenicService.getConfig(id));
    }
    @PostMapping("/saveConfig/{id}")
    public ApiResponse saveConfig(@PathVariable("id") Long id, @RequestBody ScenicConfigEntity config) {
        scenicService.saveConfig(id, config);
        return ApiResponse.success(null);
    }
    @PostMapping("/saveConfig/undefined")
    public ApiResponse saveConfig(@RequestBody ScenicConfigEntity config) {
        scenicService.addConfig(config);
        return ApiResponse.success(null);
    }

    @ApiOperation("根据景区ID下载小程序二维码")
    @GetMapping("/{id}/QRCode")
    public ApiResponse<String> downloadQrCode(@PathVariable Long id) {
        MpConfigEntity mpConfig = scenicRepository.getScenicMpConfig(id);
        if (mpConfig == null) {
            return ApiResponse.fail("小程序配置不存在");
        }
        String appId = mpConfig.getAppId();
        String appSecret = mpConfig.getAppSecret();
        String appState = mpConfig.getState();
        String path = "pages/home/index?scenicId=" + id;
        String filePath = "qr_code_" + id + ".jpg";
        IStorageAdapter adapter = StorageFactory.use();
        if (adapter.isExists(filePath)) {
            return ApiResponse.success(adapter.getUrl(filePath));
        }
        try {
            WxMpUtil.generateWXAQRCode(appId, appSecret, appState, path, filePath);
            File file = new File(filePath);
            String s = adapter.uploadFile(null, file, filePath);
            file.delete();
            adapter.setAcl(StorageAcl.PUBLIC_READ, filePath);
            return ApiResponse.success(s);
        } catch (Exception e) {
            return ApiResponse.fail("生成二维码失败");
        }
    }

    @PostMapping("/{scenicId}/one")
    public ApiResponse getStatisticsOne(@PathVariable("scenicId") Long scenicId, @RequestBody CommonQueryReq query) {
        query.setScenicId(scenicId);
        return appStatisticsService.oneStatistics(query);
    }
    @PostMapping("/{scenicId}/two")
    public ApiResponse getStatisticsTwo(@PathVariable("scenicId") Long scenicId, @RequestBody CommonQueryReq query) {
        query.setScenicId(scenicId);
        return appStatisticsService.twoStatistics(query);
    }
    @PostMapping("/{scenicId}/three")
    public ApiResponse getStatisticsThree(@PathVariable("scenicId") Long scenicId, @RequestBody CommonQueryReq query) {
        query.setScenicId(scenicId);
        return appStatisticsService.freeStatistics(query);
    }
    @PostMapping("/{scenicId}/fun")
    public ApiResponse getStatisticsFun(@PathVariable("scenicId") Long scenicId, @RequestBody CommonQueryReq query) {
        query.setScenicId(scenicId);
        return appStatisticsService.userConversionFunnel(query);
    }
}
