package com.xhpc.system.controller;


import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.LogUserUtils;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import com.xhpc.system.domain.XhpcTenantDomain;
import com.xhpc.system.service.XhpcTenantService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/tenant")
public class XhpcTenantController extends BaseController {

    @Resource
    XhpcTenantService tenantService;

    @Resource
    LogUserUtils logUserUtils;

    @Resource
    TokenService tokenService;


    @GetMapping("/getPage")
    public TableDataInfo getPage(String tenantId,
                                 String tenantName,
                                 String contactName){

        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", tenantId);
        params.put("tenantName", tenantName);
        params.put("contactName", contactName);
        return getDataTable(tenantService.getPage(params));
    }


    @GetMapping("/getNameList")
    public R getNameList(){
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        SysUser sysUser = loginUser.getSysUser();
        if(tenantId !=null && !"".equals(tenantId)){
            if(sysUser.getUserId() != UserTypeUtil.USER_ID){
                return R.ok(tenantService.getNameList(tenantId));
            }else{
                return R.ok(tenantService.getNameList(null));
            }
        }
        return R.ok(new ArrayList<>());
    }

    // 到期前15填短信通知联系人
    // 1点定时更新状态
    //停用状态不能登陆PC，小程序正常

    @GetMapping("/detail")
    public R getDetail(@RequestParam("tenantId")String tenantId){
        return R.ok(tenantService.getInfoByPk(tenantId));
    }


    @Log(title = "租户管理-新增租户", businessType = BusinessType.INSERT)
    @PostMapping("/detail")
    public R insertNewTenant(HttpServletRequest request, @RequestBody XhpcTenantDomain domain){

        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setCreateUser(loginUser.getUserid());
        domain.setUpdateUser(loginUser.getUserid());

        return R.ok(tenantService.insertTenant(domain));
    }


    @Log(title = "租户管理-更新租户信息", businessType = BusinessType.UPDATE)
    @PatchMapping("/detail")
    public R updateTenant(HttpServletRequest request, @RequestBody XhpcTenantDomain domain){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setUpdateUser(loginUser.getUserid());
        return R.ok(tenantService.updateTenantInfo(domain));
    }


    @Log(title = "租户管理-更新租户授权配置信息", businessType = BusinessType.UPDATE)
    @PatchMapping("/config")
    public R updateTenantConfig(HttpServletRequest request, @RequestBody XhpcTenantDomain domain){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setUpdateUser(loginUser.getUserid());
        return R.ok(tenantService.updateTenantConfig(domain));
    }


    @Log(title = "租户管理-更新租户状态", businessType = BusinessType.UPDATE)
    @PatchMapping("/status")
    public R updateTenantStatus(HttpServletRequest request, @RequestBody XhpcTenantDomain domain){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setUpdateUser(loginUser.getUserid());
        return R.ok(tenantService.updateTenantStatus(domain));
    }


    @Log(title = "租户管理-删除租户", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteTenantByPk")
    public R deleteTenantByPk(@RequestParam("tenantId")String tenantId){
        return R.ok(tenantService.deleteByPk(tenantId));
    }

    //查询租户是否到期
    @GetMapping("/getTenantIdTime")
    public R getTenantIdTime(@RequestParam("tenantId")String tenantId){
        XhpcTenantDomain infoByPk = tenantService.getInfoByPk(tenantId);
        if(infoByPk !=null && infoByPk.getIsDeleted()==0 && infoByPk.getStatus()==1){
            return R.ok(infoByPk);
        }
        return R.fail();
    }

}
