package com.goldenwater.web.controller.pms;

import com.goldenwater.slgc.entity.efinder.DocFolder;
import com.goldenwater.slgc.service.efinder.IDocFolderService;
import com.goldenwater.slgc.service.pms.IPmsUserProjectService;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pms/addProject")
public class PmsAddProjectController extends BaseController {
    @Autowired
    private IDocFolderService docFolderService;

    @Autowired
    private IPmsUserProjectService userProjectService;

    /**
     * 新增项目文件夹，根据项目类型(zggcPrarms)创建对应子目录结构
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestParam("folderId") String folderId,
                          @RequestParam(value = "name", required = false) String name,
                          @RequestParam(value = "note", required = false) String note,
                          @RequestParam(value = "zggcPrarms", required = false) String zggcPrarms,
                          @RequestParam(value = "planValues", required = false) String planValues) {
        try {
            if (StringUtils.isBlank(name)) {
                name = "新建项目";
            }

            // 创建根文件夹
            DocFolder rootFolder = new DocFolder();
            rootFolder.setPid(Long.valueOf(folderId));
            rootFolder.setName(name);
            rootFolder.setDate(new Date());
            docFolderService.insert(rootFolder);
            // rootFolder.id 已由 MyBatis useGeneratedKeys 回填

            // 根据 zggcPrarms 创建子文件夹
            if (StringUtils.isNotBlank(zggcPrarms)) {
                String[] subFolderNames;
                switch (zggcPrarms) {
                    case "1": // 直管工程
                        subFolderNames = new String[]{"工程概况", "程序管理", "质量管理", "安全管理", "验收管理", "监督检查", "多媒体资料管理"};
                        break;
                    case "2": // 重点工程
                        subFolderNames = new String[]{"工程概况", "监督检查", "工程验收"};
                        break;
                    case "3": // 规章制度
                        subFolderNames = new String[]{"制度概况"};
                        break;
                    default:
                        subFolderNames = new String[0];
                }
                for (String subName : subFolderNames) {
                    DocFolder subFolder = new DocFolder();
                    subFolder.setPid(rootFolder.getId());
                    subFolder.setName(subName);
                    subFolder.setDate(new Date());
                    docFolderService.insert(subFolder);
                }
            }

            // 处理计划进度值
            if (StringUtils.isNotBlank(planValues)) {
                docFolderService.updatePlanValue(rootFolder.getId(), Integer.valueOf(planValues));
            }

            return success("恭喜您，新建成功");
        } catch (Exception e) {
            logger.error("新建项目失败", e);
            return error(e.getMessage());
        }
    }

    /**
     * 获取用户项目列表
     */
    @GetMapping("/getUserProject")
    public AjaxResult getUserProject() {
        List<Map<String, Object>> list = userProjectService.getUserProject();
        return success(list);
    }
}
