package com.chushang.inspection.project.controller;

import cn.hutool.core.bean.BeanUtil;
import com.chushang.common.core.exception.utils.AssertUtil;
import com.chushang.common.core.validator.Create;
import com.chushang.common.core.validator.Update;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.inspection.project.dto.Delete;
import com.chushang.inspection.project.dto.UserAutographDTO;
import com.chushang.inspection.project.po.UserAutograph;
import com.chushang.inspection.project.service.UserAutographService;
import com.chushang.security.annotation.RequiresPermissions;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @auther: zhao
 * @date: 2024/6/14 11:07
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/graph/user")
public class UserAutographController {

    private final UserAutographService userAutographService;

    /**
     * 修改用户签名
     * 业务员--> 即维修人员
     */
    @PostMapping("/update")
    @RequiresPermissions("user:graph:update")
    @SysLog(value = "用户签名",businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody @Validated({Create.class, Update.class}) UserAutographDTO autographDTO) {
        userAutographService.updateById(BeanUtil.copyProperties(autographDTO, UserAutograph.class));
        return AjaxResult.success();
    }

    /**
     * 新增用户签名
     */
    @PostMapping("/save")
    @RequiresPermissions("user:graph:save")
    @SysLog(value = "用户签名",businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody @Validated({Create.class, Update.class})  UserAutographDTO autographDTO) {
        userAutographService.save(BeanUtil.copyProperties(autographDTO, UserAutograph.class));
        return AjaxResult.success();
    }

    /**
     * 删除用户签名
     */
    @PostMapping("/delete")
    @RequiresPermissions("user:graph:del")
    @SysLog(value = "用户签名",businessType = BusinessType.DELETE)
    public AjaxResult delete(@RequestBody @Validated Delete delete) {
        userAutographService.removeByIds(delete.getIds());
        return AjaxResult.success();
    }
    /**
     * 查询用户签名
     */
    @GetMapping("/{userId}")
    @RequiresPermissions("user:graph:query")
    public AjaxResult query(@PathVariable Long userId) {
       return AjaxResult.success(userAutographService.getById(userId));
    }

    @GetMapping("/test")
    public void test(){
        log.info("test");
    }

    @GetMapping("/error")
    public void error(){
        AssertUtil.invalidate(false, "水水水水");
    }


}
