package com.iformall.controller.sys;


import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.MallUserInfo;
import com.iformall.domain.po.WxUserDataRule;
import com.iformall.enums.EnumUserAdmin;
import com.iformall.service.WxUserDataRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author gongbiao
 */
@RestController
@RequestMapping("wxUserDataRule")
@Api(description = "用户数据权限")
public class WxUserDataRuleController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxUserDataRuleService wxUserDataRuleService;

    @ApiOperation("编辑")
    @PostMapping("/modify")
    @SystemControllerLog(description = "用户数据权限-新增")
    public ResultData modify(@RequestBody WxUserDataRule wxUserDataRule) {
        logger.debug("[" + getIpAddr() + "] wxDataRuleTarget::modify");
        MallUserInfo currentUser = getUser();
        // 系统管理员才能修改信息
        if (!currentUser.getIsAdmin().equals(EnumUserAdmin.ADMIN.getCode())) {
            return new ResultData(ErrorCode.USER_NO_PERMISSION.getCode(), "系统管理员才能修改信息");
        }
        wxUserDataRuleService.modify(wxUserDataRule);
        return new ResultData();
    }

    @ApiOperation("根据用户ID查看")
    @GetMapping("/findByUserId")
    @ApiImplicitParam(name = "userId", value = "userId", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "用户数据权限-根据用户ID查看")
    public ResultData findByUserId(Long userId) {
        logger.debug("[" + getIpAddr() + "] wxDataRuleTarget::modify");
        WxUserDataRule wxUserDataRule = wxUserDataRuleService.findByUserId(userId);
        return new ResultData(wxUserDataRule);
    }

}
