/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.comment.model.DistributionUserGroup;
import com.yami.shop.distribution.comment.service.DistributionUserGroupService;
import com.yami.shop.security.platform.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author lgh on 2019/04/01.
 */
@RestController
@RequestMapping("/distribution/distributionUserGroup")
public class DistributionUserGroupController {

    @Autowired
    private DistributionUserGroupService distributionUserGroupService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUserGroup:page')")
    public ResponseEntity<IPage<DistributionUserGroup>> page(DistributionUserGroup distributionUserGroup, PageParam<DistributionUserGroup> page) {
        distributionUserGroup.setShopId(Constant.PLATFORM_SHOP_ID);
        IPage<DistributionUserGroup> list = distributionUserGroupService.distributionUserGroupsAndSysUserPage(page, distributionUserGroup);
        return ResponseEntity.ok(list);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('distribution:distributionUserGroup:info')")
    public ResponseEntity<DistributionUserGroup> info(@PathVariable("id") Long id) {
        DistributionUserGroup distributionUserGroup = distributionUserGroupService.getById(id);
        return ResponseEntity.ok(distributionUserGroup);
    }

    /**
     * 保存
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionUserGroup:save')")
    public ResponseEntity<Void> save(@RequestBody @Valid DistributionUserGroup distributionUserGroup) {
        distributionUserGroup.setShopId(Constant.PLATFORM_SHOP_ID);
        distributionUserGroup.setUpdateTime(new Date());
        distributionUserGroup.setModifier(SecurityUtils.getSysUser().getUserId());
        distributionUserGroupService.save(distributionUserGroup);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionUserGroup:update')")
    public ResponseEntity<Void> update(@RequestBody @Valid DistributionUserGroup distributionUserGroup) {
        distributionUserGroup.setUpdateTime(new Date());
        distributionUserGroup.setModifier(SecurityUtils.getSysUser().getUserId());
        distributionUserGroupService.updateById(distributionUserGroup);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionUserGroup:delete')")
    public ResponseEntity<Void> delete(@RequestBody List<Long> ids) {
        distributionUserGroupService.removeByIds(ids);
        return ResponseEntity.ok().build();
    }
}
