
package com.tmerclub.cloud.biz.controller.admin;

import com.tmerclub.cloud.biz.dto.AttachFileGroupDTO;
import com.tmerclub.cloud.biz.model.AttachFileGroup;
import com.tmerclub.cloud.biz.service.AttachFileGroupService;
import com.tmerclub.cloud.biz.vo.AttachFileGroupVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author YXF
 * @date 2020-12-04 16:15:02
 */
@RestController("adminAttachFileGroupController")
@RequestMapping("/mp/attachFileGroup")
@Tag(name = "admin-文件分组")
public class AttachFileGroupController {
    @Autowired
    private AttachFileGroupService attachFileGroupService;

    @GetMapping("/list")
    @Operation(summary = "获取列表", description = "分页获取列表")
    public ServerResponseEntity<List<AttachFileGroupVO>> list(@RequestParam(value = "type", defaultValue = "1") Integer type) {
        List<AttachFileGroupVO> attachFileGroupPage = attachFileGroupService.list(type);
        return ServerResponseEntity.success(attachFileGroupPage);
    }

    @GetMapping
    @Operation(summary = "获取", description = "根据attachFileGroupId获取")
    public ServerResponseEntity<AttachFileGroupVO> getByAttachFileGroupId(@RequestParam Long attachFileGroupId) {
        return ServerResponseEntity.success(attachFileGroupService.getByAttachFileGroupId(attachFileGroupId));
    }

    @PostMapping
    @Operation(summary = "保存", description = "保存")
    public ServerResponseEntity<Void> save(@Valid @RequestBody AttachFileGroupDTO attachFileGroupDTO) {
        AttachFileGroup attachFileGroup = BeanUtil.map(attachFileGroupDTO, AttachFileGroup.class);
        attachFileGroup.setAttachFileGroupId(null);
        attachFileGroup.setSysType(AuthUserContext.get().getSysType());
        attachFileGroupService.save(attachFileGroup);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新", description = "更新")
    public ServerResponseEntity<Void> update(@Valid @RequestBody AttachFileGroupDTO attachFileGroupDTO) {
        attachFileGroupDTO.setShopId(AuthUserContext.get().getTenantId());
        AttachFileGroup attachFileGroup = BeanUtil.map(attachFileGroupDTO, AttachFileGroup.class);
        attachFileGroup.setSysType(AuthUserContext.get().getSysType());
        attachFileGroupService.update(attachFileGroup);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除", description = "根据id删除")
    public ServerResponseEntity<Void> delete(@RequestParam Long attachFileGroupId) {
        AttachFileGroupVO dbAttachFileGroup = attachFileGroupService.getByAttachFileGroupId(attachFileGroupId);
        if (!Objects.equals(dbAttachFileGroup.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        attachFileGroupService.deleteById(attachFileGroupId);
        return ServerResponseEntity.success();
    }
}
