package com.hotwj.platform.resourceManagement.companyPolicy.controller;

import cn.hutool.core.collection.CollUtil;
import com.hotwj.platform.regulationManagement.policyRevision.domain.bo.HotPolicyRevisionBo;
import com.hotwj.platform.regulationManagement.policyRevision.service.IHotPolicyRevisionService;
import com.hotwj.platform.resourceManagement.companyPolicy.domain.bo.HotCompanyPolicyBo;
import com.hotwj.platform.resourceManagement.companyPolicy.domain.vo.HotCompanyPolicyVo;
import com.hotwj.platform.resourceManagement.companyPolicy.service.IHotCompanyPolicyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 公司制度资料档案
 *
 * @author shihongwei
 * @date 2025-12-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/resourceManagement/companyPolicy")
@Tag(name = "公司制度资料档案", description = "公司制度资料档案增删改查")
public class HotCompanyPolicyController extends BaseController {

    private final IHotCompanyPolicyService hotCompanyPolicyService;
    private final IHotPolicyRevisionService hotPolicyRevisionService;

    /**
     * 查询公司制度资料档案列表
     */
    //@SaCheckPermission("resourceManagement:companyPolicy:list")
    @GetMapping("/list")
    @Operation(summary = "分页查询公司制度资料档案列表")
    public TableDataInfo<HotCompanyPolicyVo> list(HotCompanyPolicyBo bo, PageQuery pageQuery) {
        return hotCompanyPolicyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出公司制度资料档案列表
     */
    //@SaCheckPermission("resourceManagement:companyPolicy:export")
    @Log(title = "公司制度资料档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出公司制度资料档案列表")
    public void export(HotCompanyPolicyBo bo, HttpServletResponse response) {
        List<HotCompanyPolicyVo> list = hotCompanyPolicyService.queryList(bo);
		// 设置导出的排序号
        if (CollUtil.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setSort(i + 1);
            }
        }

        ExcelUtil.exportExcel(list, "公司制度资料档案", HotCompanyPolicyVo.class, response);
    }

    /**
     * 获取公司制度资料档案详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("resourceManagement:companyPolicy:query")
    @GetMapping("/{id}")
    @Operation(summary = "查询公司制度资料档案详情")
    public R<HotCompanyPolicyVo> getInfo(@NotNull(message = "主键不能为空")
                                         @PathVariable Long id) {
        return R.ok(hotCompanyPolicyService.queryById(id));
    }

    /**
     * 新增公司制度资料档案
     */
    //@SaCheckPermission("resourceManagement:companyPolicy:add")
    @Log(title = "公司制度资料档案", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Operation(summary = "新增公司制度资料档案")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HotCompanyPolicyBo bo) {
        return toAjax(hotCompanyPolicyService.insertByBo(bo));
    }

    /**
     * 修改公司制度资料档案
     */
    //@SaCheckPermission("resourceManagement:companyPolicy:edit")
    @Log(title = "公司制度资料档案", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Operation(summary = "修改公司制度资料档案")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HotCompanyPolicyBo bo) {
        HotCompanyPolicyVo before = hotCompanyPolicyService.queryById(bo.getId());
        boolean ok = hotCompanyPolicyService.updateByBo(bo);
        if (ok) {
            String beforeHtml = before != null ? before.getContentHtml() : null;
            String afterHtml = bo.getContentHtml();
            boolean changed = !org.dromara.common.core.utils.StringUtils.equals(beforeHtml, afterHtml);
            if (changed) {
                Long versionNo = hotPolicyRevisionService.nextVersionNo(bo.getId());
                HotPolicyRevisionBo rev = new HotPolicyRevisionBo();
                rev.setCompanyId(bo.getCompanyId());
                rev.setPolicyId(bo.getId());
                rev.setVersionNo(versionNo);
                Map<String, Object> params = bo.getParams();
                String changeNote = params != null ? String.valueOf(params.getOrDefault("changeNote", "")) : "";
                rev.setChangeNote(changeNote);
                rev.setContent(afterHtml);
                rev.setCorrection(beforeHtml);
                rev.setAttachmentUrl(bo.getAttachmentUrl());
                rev.setCreatedTime(new Date());
                hotPolicyRevisionService.createRevision(rev);
            }
        }
        return toAjax(ok);
    }

    /**
     * 删除公司制度资料档案
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("resourceManagement:companyPolicy:remove")
    @Log(title = "公司制度资料档案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除公司制度资料档案")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(hotCompanyPolicyService.deleteWithValidByIds(List.of(ids), true));
    }
}
