package io.modules.sys.controller;

import cn.hutool.crypto.digest.DigestUtil;
import io.common.annotation.LogOperation;
import io.common.constant.Constant;
import io.common.page.PageData;
import io.common.utils.ExcelUtils;
import io.common.utils.Result;
import io.common.validator.AssertUtils;
import io.common.validator.ValidatorUtils;
import io.common.validator.group.AddGroup;
import io.common.validator.group.DefaultGroup;
import io.common.validator.group.UpdateGroup;
import io.modules.item.dto.UserDTO;
import io.modules.item.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
/**
 * 用户
 */
@RestController
@RequestMapping("/sys/user-front")
@Tag(name="用户")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
        @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref="int") ,
        @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY,required = true, ref="int") ,
        @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref="String") ,
        @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref="String")
    })
    public Result<PageData<UserDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params){
        PageData<UserDTO> page = userService.page(params);
        return new Result<PageData<UserDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<UserDTO> get(@PathVariable("id") Long id){
        UserDTO data = userService.get(id);
        return new Result<UserDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    public Result save(@RequestBody UserDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        if (StringUtils.isNotBlank(dto.getPassword())){
            dto.setPassword(DigestUtil.sha256Hex(dto.getPassword()));
        }
        userService.save(dto);
        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    public Result update(@RequestBody UserDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        if (StringUtils.isNotBlank(dto.getPassword())){
            dto.setPassword(DigestUtil.sha256Hex(dto.getPassword()));
        }
        userService.update(dto);

        System.out.println(dto);
        return new Result();
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    @LogOperation("删除")
    public Result delete(@PathVariable Long id){
        Long[] ids = new Long[] { id };
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        userService.delete(ids);
        return new Result();
    }
}
