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

import com.gic.api.base.commons.Page;
import com.gic.enterprise.api.dto.StoreDTO;
import com.gic.enterprise.api.dto.StoreSearchDTO;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.base.api.common.BasePageInfo;
import com.gic.haoban.base.api.common.PageResult;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.manage.api.service.StaffApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/store")
public class StoreController extends WebBaseController{
    private static final Logger logger = LoggerFactory.getLogger(StoreController.class);
	@Autowired
	private StoreService storeService;
    @Autowired
    private StaffApiService staffApiService;

	//门店列表
	@RequestMapping("/find-simple-page")
	public HaobanResponse storeList(Integer storeType,String enterpriseId,BasePageInfo basePageInfo) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String clerkId = loginUser.getClerkId();
        Page page = new Page<>();
        int pageNum = basePageInfo.getPageNum();
        int pageSize = basePageInfo.getPageSize();
        page.setCurrentPage(pageNum);
        page.setPageSize(pageSize);
        StoreSearchDTO storeSearchDTO = new StoreSearchDTO();
        storeSearchDTO.setEnterpriseId(enterpriseId);
        storeSearchDTO.setStatus("2");
        storeSearchDTO.setStoreType(storeType);

        List<String> storeList = staffApiService.getHaoBanStoreIdsRolesByClerkId(clerkId, loginUser.getWxEnterpriseId());
        PageResult<StoreDTO> pageVo = new PageResult<>();
        pageVo.setPageNum(pageNum);
        pageVo.setPageSize(pageSize);
        if (CollectionUtils.isEmpty(storeList)) {
            logger.info("导购无门店权限");
            return resultResponse(HaoBanErrCode.ERR_1,pageVo);
        }
        if(!(storeList.size()==1 && storeList.contains("-1"))) {
            storeSearchDTO.setStoreIdList(storeList);
        }
        Page<StoreDTO> pageResult = storeService.storeListPage(page, storeSearchDTO);
        pageVo.setList(pageResult.getResult());
        pageVo.setTotal(pageResult.getTotalCount());
        return resultResponse(HaoBanErrCode.ERR_1, pageVo);
	}

}
