package com.ycwl.basic.controller.pc;

import com.ycwl.basic.model.pc.project.entity.ProjectEntity;
import com.ycwl.basic.model.pc.project.req.ProjectReqQuery;
import com.ycwl.basic.model.pc.project.resp.ProjectRespVO;
import com.ycwl.basic.service.pc.ProjectService;
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.storage.StorageFactory;
import com.ycwl.basic.storage.adapters.IStorageAdapter;
import com.ycwl.basic.model.pc.mp.MpConfigEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;

/**
 * 景区项目管理控制器
 * 
 * @Author: Claude
 * @Date: 2025-01-15
 */
@RestController
@RequestMapping("/api/project/v1")
public class ProjectController {
    
    @Autowired
    private ProjectService projectService;

    @Autowired
    private ScenicRepository scenicRepository;

    // 分页查询
    @PostMapping("/page")
    public ApiResponse page(@RequestBody ProjectReqQuery projectReqQuery) {
        return ApiResponse.success(projectService.pageQuery(projectReqQuery));
    }

    // 列表查询
    @PostMapping("/list")
    public ApiResponse list(@RequestBody ProjectReqQuery projectReqQuery) {
        return ApiResponse.success(projectService.list(projectReqQuery));
    }

    // 详情查询
    @GetMapping("/getDetails/{id}")
    public ApiResponse getDetails(@PathVariable("id") Long id) {
        return ApiResponse.success(projectService.getById(id));
    }

    // 新增或修改
    @PostMapping("/addOrUpdate")
    public ApiResponse addOrUpdate(@RequestBody ProjectEntity project) {
        return ApiResponse.success(projectService.addOrUpdate(project));
    }

    // 删除
    @DeleteMapping("/delete/{id}")
    public ApiResponse delete(@PathVariable("id") Long id) {
        return ApiResponse.success(projectService.delete(id));
    }

    // 修改状态
    @PutMapping("/updateStatus/{id}")
    public ApiResponse updateStatus(@PathVariable("id") Long id) {
        return ApiResponse.success(projectService.updateStatus(id));
    }

    // 根据项目ID下载小程序二维码
    @GetMapping("/{id}/QRCode")
    public ApiResponse<String> downloadQrCode(@PathVariable Long id) {
        ProjectRespVO project = projectService.getById(id);
        if (project == null) {
            return ApiResponse.fail("项目不存在");
        }
        MpConfigEntity mpConfig = scenicRepository.getScenicMpConfig(project.getScenicId());
        if (mpConfig == null) {
            return ApiResponse.fail("小程序配置不存在");
        }
        String appId = mpConfig.getAppId();
        String appSecret = mpConfig.getAppSecret();
        String appState = mpConfig.getState();
        String path = "pages/home/index?scenicId=" + project.getScenicId() + "&projectId=" + id;
        String filePath = "qr_code_project_" + 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("生成二维码失败");
        }
    }
}