package com.yonge.cooleshow.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.microsvc.toolkit.common.response.template.R;
import com.yonge.cooleshow.admin.io.request.TenantStaffVo;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.TenantStaff;
import com.yonge.cooleshow.biz.dal.service.TenantStaffService;
import com.yonge.cooleshow.biz.dal.wrapper.TenantStaffWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.admin:}/tenantStaff")
@Api(tags = "机构员工表")
public class TenantStaffController extends BaseController {

    @Autowired
    private TenantStaffService tenantStaffService;

    @Resource
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "详情", notes = "机构员工表-根据详情ID查询单条, 传入id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long")
    })
    @PreAuthorize("@pcs.hasPermissions('tenantStaff/detail', {'BACKEND'})")
    @GetMapping("/detail/{id}")
    public R<TenantStaffWrapper.TenantStaff> detail(@PathVariable("id") Long id) {
        TenantStaff wrapper = tenantStaffService.detail(id);
        if (wrapper == null) {
            return R.from(new TenantStaffWrapper.TenantStaff());
        }
        TenantStaffWrapper.TenantStaff tenantStaff = TenantStaffWrapper.TenantStaff.from(JSON.toJSONString(wrapper));
        SysUser sysUser = sysUserFeignService.queryUserById(wrapper.getUserId());
        tenantStaff.setGender(sysUser.getGender());
        tenantStaff.setNickname(sysUser.getUsername());
        tenantStaff.setPhone(sysUser.getPhone());
        tenantStaff.setBindStatus(StringUtils.isNotEmpty(tenantStaff.getWxOpenid()));
        return R.from(tenantStaff);
    }

    @ApiOperation(value = "查询分页", notes = "机构员工表- 传入 TenantStaffVo.TenantStaffQuery")
    @PreAuthorize("@pcs.hasPermissions('tenantStaff/page', {'BACKEND'})")
    @PostMapping("/page")
    public R<PageInfo<TenantStaffWrapper.TenantStaff>> page(@RequestBody TenantStaffWrapper.TenantStaffQuery query) {

        // 查询数据
        return R.from(PageUtil.pageInfo(tenantStaffService.selectPage(QueryInfo.getPage(query), query)));
    }

    @ApiOperation(value = "新增", notes = "机构员工表- 传入 TenantStaffVo.TenantStaff")
    @PreAuthorize("@pcs.hasPermissions('tenantStaff/add', {'BACKEND'})")
//    @PostMapping("/save")
    public R<JSONObject> add(@Validated @RequestBody TenantStaffVo.TenantStaff tenantStaffVo) {

        // 新增数据
        tenantStaffService.save(JSON.parseObject(tenantStaffVo.jsonString(), TenantStaff.class));

        return R.defaultR();
    }

    @ApiOperation(value = "修改", notes = "机构员工表- 传入 TenantStaffVo.TenantStaff")
    @PreAuthorize("@pcs.hasPermissions('tenantStaff/update', {'BACKEND'})")
    @PostMapping("/update")
    public R<JSONObject> update(@Validated @RequestBody TenantStaffVo.TenantStaff tenantStaffVo) {

        // 更新数据
        tenantStaffService.update(JSON.parseObject(tenantStaffVo.jsonString(), TenantStaffWrapper.TenantStaffAdd.class));

        return R.defaultR();
    }

    @ApiOperation(value = "启用/禁用", notes = "机构员工表- 传入 TenantStaff")
    @PreAuthorize("@pcs.hasPermissions('tenantStaff/updateStatus', {'BACKEND'})")
    @PostMapping("/updateStatus")
    public R<JSONObject> updateStatus(@RequestBody TenantStaff vo) {

        // 更新数据
        tenantStaffService.updateStatus(vo.getId(), vo.getStatus());
        return R.defaultR();
    }

    @ApiOperation(value = "清理wxOpenId")
    @PreAuthorize("@pcs.hasPermissions('tenantStaff/clearWxOpenId', {'BACKEND'})")
    @PostMapping("/clearWxOpenId")
    public R<JSONObject> clearWxOpenId(@RequestBody TenantStaff vo) {

        // 更新数据
        tenantStaffService.clearWxOpenId(vo.getId());
        return R.defaultR();
    }

    @ApiOperation(value = "删除", notes = "机构员工表- 传入id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long")
    })
    @PreAuthorize("@pcs.hasPermissions('tenantStaff/remove', {'BACKEND'})")
//    @PostMapping("/remove")
    public R<Boolean> remove(@RequestParam Long id) {

        return R.from(tenantStaffService.removeById(id));
    }
}