package com.xueyi.system.staff.controller.api;

import com.alibaba.fastjson2.JSONObject;
import com.xueyi.common.cache.utils.SourceUtil;
import com.xueyi.common.core.constant.basic.SecurityConstants;
import com.xueyi.common.core.context.SecurityContextHolder;
import com.xueyi.common.core.web.result.R;
import com.xueyi.common.redis.utils.RedisUtil;
import com.xueyi.common.web.annotation.TenantIgnore;
import com.xueyi.system.api.device.domain.vo.DeviceTenantSourceMergeVo;
import com.xueyi.system.api.model.Source;
import com.xueyi.system.api.staff.domain.dto.DmStaffCommonDto;
import com.xueyi.system.api.staff.feign.RemoteStaffService;
import com.xueyi.system.resource.controller.api.BaseApiController;
import com.xueyi.system.staff.service.impl.DmStaffServiceImpl;
import com.xueyi.tenant.api.tenant.domain.dto.TeTenantDto;
import com.xueyi.tenant.api.tenant.feign.RemoteTenantService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 员工管理 API业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/staff/api")
public class DmStaffApiController extends BaseApiController {

    private static final Logger log = LoggerFactory.getLogger(DmStaffApiController.class);

    @Autowired
    private RemoteStaffService staffService;

    @Autowired
    private DmStaffServiceImpl dmStaffService;

    @Autowired
    private RemoteTenantService tenantService;

    @GetMapping(value = "fetchOne/{devId}/{staffId}")
    @ResponseBody
    public JSONObject fetchStaff(@PathVariable("devId") String devId, @PathVariable("staffId") String staffId){
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(devId);
        return staffService.fetchStaff(staffId, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);
    }


    @PostMapping(value = "new-staff")
    @ResponseBody
    public com.alibaba.fastjson2.JSONObject newStaff(@RequestBody DmStaffCommonDto commonDto){
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(commonDto.getDevId());
        return staffService.addStaff(commonDto, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);
    }

    @GetMapping(value = "broadcast-staff-count")
    @ResponseBody
    @TenantIgnore(tenantLine = true)
    public JSONObject queryStaff(){
        SecurityContextHolder.set("sourceName", "slave");
        Long count = dmStaffService.selectStaffAndVisitorCount();
        return outputSuccess(count).toJSON();
    }

    @GetMapping(value = "staff-count")
    @ResponseBody
    public JSONObject staffCount(){
        if (RedisUtil.existed("saas:staff:totalCountAndVisitor")){
            String str = RedisUtil.getVal("saas:staff:totalCountAndVisitor").toString();
            log.info("staff-count res:{}", str);
            return JSONObject.parse(str);
        }

        R<List<TeTenantDto>> listR = tenantService.tenantList();
        if (listR.isFail()) {
            System.out.println("租户列表获取失败");
            return null;
        }
        List<TeTenantDto> res = listR.getData();
        TeTenantDto teTenantDto = res.get(0);
        if  (teTenantDto != null) {
            Source source = SourceUtil.getSourceCache(teTenantDto.getStrategyId());
            JSONObject countJson = staffService.queryStaffCount(teTenantDto.getId(), source.getMaster(), SecurityConstants.INNER);
            RedisUtil.setVal("saas:staff:totalCountAndVisitor", countJson.toJSONString(), 60);
            return countJson;
        }


        return outputSuccess().toJSON();
    }
}
