package com.yc.ship.module.resource.controller.admin.audit;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.resource.controller.admin.audit.vo.AuditPageReqVO;
import com.yc.ship.module.resource.controller.admin.audit.vo.AuditRespVO;
import com.yc.ship.module.resource.controller.admin.audit.vo.AuditSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.audit.AuditDO;
import com.yc.ship.module.resource.service.audit.AuditService;
import com.yc.ship.module.system.api.user.AdminUserApi;
import com.yc.ship.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 审核记录")
@RestController
@RequestMapping("/resource/audit")
@Validated
public class AuditController {

    @Resource
    private AuditService auditService;
    @Resource
    private AdminUserApi userApi;

    @PostMapping("/create")
    @Operation(summary = "创建审核记录")
    @PreAuthorize("@ss.hasPermission('dispatch:audit:create')")
    public CommonResult<String> createAudit(@Valid @RequestBody AuditSaveReqVO createReqVO) {
        return success(auditService.createAudit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审核记录")
    @PreAuthorize("@ss.hasPermission('dispatch:audit:update')")
    public CommonResult<Boolean> updateAudit(@Valid @RequestBody AuditSaveReqVO updateReqVO) {
        auditService.updateAudit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审核记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dispatch:audit:delete')")
    public CommonResult<Boolean> deleteAudit(@RequestParam("id") String id) {
        auditService.deleteAudit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得审核记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dispatch:audit:query')")
    public CommonResult<AuditRespVO> getAudit(@RequestParam("id") String id) {
        AuditDO audit = auditService.getAudit(id);
        return success(BeanUtils.toBean(audit, AuditRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得审核记录分页")
    @PreAuthorize("@ss.hasPermission('dispatch:audit:query')")
    public CommonResult<PageResult<AuditRespVO>> getAuditPage(@Valid AuditPageReqVO pageReqVO) {
        PageResult<AuditDO> pageResult = auditService.getAuditPage(pageReqVO);
        PageResult<AuditRespVO> result = BeanUtils.toBean(pageResult, AuditRespVO.class);
        Map<String, String> userNameMap = new HashMap<>();
        result.getList().forEach(auditRespVO -> {
            if (userNameMap.containsKey(auditRespVO.getCreator())) {
                auditRespVO.setCreatorName(userNameMap.get(auditRespVO.getCreator()));
            } else {
                if (null != auditRespVO.getCreator()) {
                    AdminUserRespDTO user = userApi.getUser(Long.parseLong(auditRespVO.getCreator()));
                    if (user != null) {
                        auditRespVO.setCreatorName(user.getNickname());
                        userNameMap.put(auditRespVO.getCreator(), user.getNickname());
                    }
                }
            }
        });
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出审核记录 Excel")
    @PreAuthorize("@ss.hasPermission('dispatch:audit:export')")
    @OperateLog(type = EXPORT)
    public void exportAuditExcel(@Valid AuditPageReqVO pageReqVO,
                                 HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AuditDO> list = auditService.getAuditPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "审核记录.xls", "数据", AuditRespVO.class,
                BeanUtils.toBean(list, AuditRespVO.class));
    }

}
