package com.gic.haoban.manage.web.controller;

import com.gic.api.base.commons.Page;
import com.gic.clerk.api.dto.AuthorizedUser;
import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.dto.PowerClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.clerk.api.service.PowerService;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.enterprise.api.dto.EnterpriseSettingDTO;
import com.gic.enterprise.api.dto.StoreWidgetDTO;
import com.gic.enterprise.api.service.EnterpriseService;
import com.gic.enterprise.api.service.StoreWidgetService;
import com.gic.haoban.manage.web.vo.ClerkListVO;
import com.gic.web.common.utils.SessionContextUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Objects;


@Controller
public class UserController {

    private static final Logger LOGGER = LogManager.getLogger(UserController.class);

    @Autowired
    private ClerkService clerkService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private StoreWidgetService storeWidgetService;

    @Autowired
    private PowerService powerService;

    @RequestMapping("list-user-by-search-name")
    @ResponseBody
    public RestResponse listUserBySearchName(String searchName, Integer pageNum, Integer pageSize) {
        AuthorizedUser user = SessionContextUtils.getLoginUser();
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 20;
        }
        Page<ClerkDTO> page = clerkService.pageUserBySearchName(user.getEnterpriseId(), searchName, pageNum, pageSize);

        Page<ClerkListVO> result = EntityUtil.changeEntityNew(Page.class, page);
        if (CollectionUtils.isNotEmpty(page.getResult())) {
            List<ClerkListVO> resultList = EntityUtil.changeEntityListNew(ClerkListVO.class, page.getResult());
            result.setResult(resultList);
        }
        return RestResponse.successResult(result);
    }


    @RequestMapping("get-user-store-power")
    @ResponseBody
    public RestResponse<Integer> getUserStorePower() {
        AuthorizedUser user = SessionContextUtils.getLoginUser();
        EnterpriseSettingDTO setting = enterpriseService.getEnterpriseSettingByEnterpriseId(user.getEnterpriseId());
        if (Objects.isNull(setting.getEnableAccessControl()) || setting.getEnableAccessControl() != 1) {
            return RestResponse.successResult(0);
        }

        PowerClerkDTO clerkDetail = powerService.getClerkDetail(user.getUserId());
        if(Objects.isNull(clerkDetail)){
            return RestResponse.failure("999","用户不存在");
        }
        // 空，代表超管
        if(StringUtils.isEmpty(clerkDetail.getStoreWidgetId())){
            return RestResponse.successResult(0);
        }
        StoreWidgetDTO storeWidget = storeWidgetService.getStoreWidgetBykey(clerkDetail.getStoreWidgetId());

        return RestResponse.successResult(storeWidget.getSelectType());
    }


}
