package com.yc.ship.module.ota.controller.admin.password;

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.ota.controller.admin.password.vo.*;
import com.yc.ship.module.ota.dal.dataobject.password.OperatePasswordDO;
import com.yc.ship.module.ota.service.password.OperatePasswordService;
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.List;

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("/operate-password")
@Validated
public class OperatePasswordController {

    @Resource
    private OperatePasswordService operatePasswordService;

    @PostMapping("/create")
    @Operation(summary = "创建操作密码")
    @PreAuthorize("@ss.hasPermission('support:operate-password:create')")
    public CommonResult<Long> createOperatePassword(@Valid @RequestBody OperatePasswordSaveReqVO createReqVO) {
        return success(operatePasswordService.createOperatePassword(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新操作密码")
    @PreAuthorize("@ss.hasPermission('support:operate-password:update')")
    public CommonResult<Boolean> updateOperatePassword(@Valid @RequestBody OperatePasswordSaveReqVO updateReqVO) {
        operatePasswordService.updateOperatePassword(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除操作密码")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('support:operate-password:delete')")
    public CommonResult<Boolean> deleteOperatePassword(@RequestParam("id") Long id) {
        operatePasswordService.deleteOperatePassword(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得操作密码")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('support:operate-password:query')")
    public CommonResult<OperatePasswordRespVO> getOperatePassword(@RequestParam("id") Long id) {
        OperatePasswordDO operatePassword = operatePasswordService.getOperatePassword(id);
        return success(BeanUtils.toBean(operatePassword, OperatePasswordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得操作密码分页")
    @PreAuthorize("@ss.hasPermission('support:operate-password:query')")
    public CommonResult<PageResult<OperatePasswordRespVO>> getOperatePasswordPage(@Valid OperatePasswordPageReqVO pageReqVO) {
        PageResult<OperatePasswordDO> pageResult = operatePasswordService.getOperatePasswordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OperatePasswordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出操作密码 Excel")
    @PreAuthorize("@ss.hasPermission('support:operate-password:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportOperatePasswordExcel(@Valid OperatePasswordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OperatePasswordDO> list = operatePasswordService.getOperatePasswordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "操作密码.xls", "数据", OperatePasswordRespVO.class,
                        BeanUtils.toBean(list, OperatePasswordRespVO.class));
    }

    @PostMapping("/check")
    @Operation(summary = "校验密码")
    @PreAuthorize("@ss.hasPermission('support:operate-password:check')")
    public CommonResult<Boolean> checkOperatePassword(@Valid @RequestBody OperatePasswordCheckAndResetReqVO reqVO) {
        boolean res = operatePasswordService.checkPwd(reqVO);
        return success(res);
    }

    @PutMapping("/reset-password")
    @Operation(summary = "重置密码")
    @PreAuthorize("@ss.hasPermission('support:operate-password:reset-password')")
    public CommonResult<Boolean> resetPassword(@Valid @RequestBody OperatePasswordCheckAndResetReqVO reqVO) {
        operatePasswordService.resetPassword(reqVO.getType(),reqVO.getPassword());
        return success(true);
    }

    @PutMapping("/update-password")
    @Operation(summary = "修改用户个人密码")
    @PreAuthorize("@ss.hasPermission('support:operate-password:update-password')")
    public CommonResult<Boolean> updatePassword(@Valid @RequestBody OperatePasswordUpdateReqVO reqVO) {
        operatePasswordService.updatePassword( reqVO);
        return success(true);
    }

    @GetMapping("/validateExists")
    @Operation(summary = "校验操作密码是否存在")
    public CommonResult<Boolean> validateExists(@RequestParam("type") Integer type) {
        boolean result = operatePasswordService.validateExists(type);
        return success(result);
    }

}