package com.friday.tooldriver.controller.external;

import com.friday.base.web.response.ResponseAspect;
import com.friday.mystery.coresecurity.service.UserContext;
import com.friday.operation.api.keepalive.dto.taieoc.*;
import com.friday.operation.api.keepalive.service.taieoc.IBadgeManage;
import com.friday.operation.api.keepalive.service.taieoc.IGroupManage;
import com.friday.operation.api.keepalive.service.taieoc.IOcManage;
import com.friday.operation.api.keepalive.service.taieoc.ITenantManage;
import com.friday.tooldriver.constants.DriverSystemCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/driver/ocManage")
@Slf4j
public class OcManageController {
    @Autowired
    private ITenantManage tenantManage;

    @Autowired
    private IOcManage ocManage;

    @Autowired
    private IGroupManage groupManage;

    @Autowired
    private IBadgeManage badgeManage;

    @Autowired
    private ResponseAspect responseAspect;

    
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Object queryOcPage(@RequestHeader("Tenant") String currentTenant, @RequestBody OcTenantFilterDto filterDto){
        try{
            filterDto.setTenant(ObjectUtils.isEmpty(filterDto.getTenant())?currentTenant: filterDto.getTenant());
            return responseAspect.response(ocManage.queryOcPage(filterDto));
        }catch(Exception e){
            log.error(DriverSystemCode.FAIL.toString(),e);
            return responseAspect.fail();
        }
    }


    
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public Object queryOcTenantInfo(@RequestHeader("Tenant") String currentTenant, @RequestBody OcTenantFilterDto dto){
        dto.setTenant(ObjectUtils.isEmpty(dto.getTenant())?currentTenant: dto.getTenant());
        return responseAspect.response(tenantManage.queryTenantDetail(dto));
    }

    
    @RequestMapping(value = "/pioneer/detail", method = RequestMethod.GET)
    public Object queryPioneerTenantDetail(@RequestHeader("Tenant") String currentTenant,
                                           @RequestParam(value = "supplier",required = false) String supplier){
        return responseAspect.response(tenantManage.queryPioneerTenantDetail(supplier,currentTenant));
    }

    
    @RequestMapping(value = "/queryOcDict", method = RequestMethod.GET)
    public Object queryOcDict(@RequestHeader("Tenant") String currentTenant,
            @RequestParam(value = "supplierEn", required = false) String supplierEn){
        // 获取当前用户租户信息
        return responseAspect.response(ocManage.queryOcDict(supplierEn, currentTenant));
    }


    
    @RequestMapping(value = "/queryOcDropList", method = RequestMethod.GET)
    public Object queryOcDropList(@RequestHeader("Tenant") String currentTenant,
                                  @RequestParam(value = "supplierEn", required = false) String supplierEn){
        try{
            return responseAspect.response(ocManage.queryOcDropList(supplierEn,currentTenant));
        }catch (Exception e){
            log.error(DriverSystemCode.FAIL.toString(),e);
            return responseAspect.fail();
        }
    }

    
    @RequestMapping(value = "/queryOcTenantDropList", method = RequestMethod.GET)
    public Object queryOcTenantDropList(@RequestHeader("Tenant") String currentTenant,
                                        @RequestParam(value = "supplierEn", required = false) String supplierEn,
                                        @RequestParam(value = "ocInfo", required = false) String ocInfo){
        try{
            return responseAspect.response(tenantManage.queryOcTenantDropList(supplierEn,ocInfo,currentTenant));
        }catch(Exception e){
            log.error(DriverSystemCode.FAIL.toString(),e);
            return responseAspect.fail();
        }
    }

    
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Object createOcTenant(@RequestHeader("Tenant") String currentTenant, @RequestBody OcInfoDto ocInfoDto){
        ocInfoDto.setUserTenant(!StringUtils.isEmpty(ocInfoDto.getUserTenant())?ocInfoDto.getUserTenant():currentTenant);
        ocInfoDto.setOperator(!StringUtils.isEmpty(ocInfoDto.getOperator())?ocInfoDto.getOperator(): UserContext.getLoginUser());
        return responseAspect.response(ocManage.createOcInfo(ocInfoDto));
    }

    
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateOcTenant(@RequestHeader("Tenant") String currentTenant, @RequestBody OcInfoDto ocInfoDto){
        ocInfoDto.setUserTenant(!StringUtils.isEmpty(ocInfoDto.getUserTenant())?ocInfoDto.getUserTenant():currentTenant);
        ocInfoDto.setOperator(!StringUtils.isEmpty(ocInfoDto.getOperator())?ocInfoDto.getOperator():UserContext.getLoginUser());
        return responseAspect.response(ocManage.updateOcInfo(ocInfoDto));
    }

    
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object deleteOcTenant(@RequestParam("ocInfoId") Long ocInfoId){
        return responseAspect.response(ocManage.deleteOcInfo(ocInfoId, UserContext.getLoginUser()));
    }


    
    @RequestMapping(value = "/group/page", method = RequestMethod.POST)
    public Object queryGroupPage(@RequestHeader("Tenant") String currentTenant, @RequestBody OcGroupFilterDto ocGroupFilterDto){
        return responseAspect.response(groupManage.queryGroups(ocGroupFilterDto));
    }

    
    @RequestMapping(value = "/group/detail", method = RequestMethod.GET)
    public Object queryGroupDetail(@RequestParam("groupId") Long groupId){
        return responseAspect.response(groupManage.queryGroupDetail(groupId));
    }


    
    @RequestMapping(value = "/badge/page", method = RequestMethod.POST)
    public Object queryBadgePage(@RequestHeader("Tenant") String currentTenant, @RequestBody BadgeFilterDto filterDto){
        filterDto.setOperator(UserContext.getLoginUser());
        filterDto.setUserTenant(currentTenant);
        return responseAspect.response(badgeManage.queryBadgePage(filterDto));
    }

    
    @RequestMapping(value = "/badge/create", method = RequestMethod.POST)
    public Object createBadge(@RequestHeader("Tenant") String currentTenant, @RequestBody BadgeReqDto reqDto){
        reqDto.setUserTenant(currentTenant);
        reqDto.setOperator(UserContext.getLoginUser());
        return responseAspect.response(badgeManage.createBadgeInfo(reqDto));
    }

    
    @RequestMapping(value = "/badge/update", method = RequestMethod.POST)
    public Object updateBadge(@RequestHeader("Tenant") String currentTenant, @RequestBody BadgeReqDto reqDto){
        reqDto.setOperator(UserContext.getLoginUser());
        return responseAspect.response(badgeManage.updateBadgeInfo(reqDto));
    }

}
