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

import com.gic.api.base.commons.Page;
import com.gic.clerk.api.dto.ClerkListDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.enterprise.api.dto.EnterpriseDTO;
import com.gic.enterprise.api.dto.StoreDTO;
import com.gic.enterprise.api.dto.StoreSearchDTO;
import com.gic.enterprise.api.service.EnterpriseService;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.manage.api.dto.*;
import com.gic.haoban.manage.api.service.StaffApiService;
import com.gic.haoban.manage.api.service.StaffClerkRelationApiService;
import com.gic.haoban.manage.api.service.WxEnterpriseApiService;
import com.gic.haoban.manage.api.service.WxEnterpriseRelatedApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.haoban.manage.web.qo.StoreListQo;
import com.gic.haoban.manage.web.vo.EnterpriseVo;
import com.gic.haoban.manage.web.vo.StaffClerkInfoVo;
import com.gic.haoban.manage.web.vo.WxClerkInfoVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created 2020/6/3.
 *
 * @author hua
 */
@RestController
@RequestMapping("/pay")
public class PayController extends WebBaseController {

    private static final Logger logger= LoggerFactory.getLogger(PayController.class);

    @Autowired
    private StaffApiService staffApiService;

    @Autowired
    private WxEnterpriseApiService wxEnterpriseApiService;

    @Autowired
    private StaffClerkRelationApiService staffClerkRelationApiService;

    @Autowired
    private ClerkService clerkService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private WxEnterpriseRelatedApiService wxEnterpriseRelatedApiService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 微信成员列表
     * @param userId
     * @param corpId
     * @return
     */
    @RequestMapping("/wx-user-list")
    public HaobanResponse userList(String userId,String corpId) {
        if (StringUtils.isAnyBlank(userId,corpId)) {
            logger.info("参数异常");
            return resultResponse(HaoBanErrCode.ERR_2);
        }
        WxEnterpriseDTO enterpriseDTO = wxEnterpriseApiService.getEnterpriseBycorpId(corpId);
        if (enterpriseDTO==null) {
            logger.info("不存在商户");
            return resultResponse(HaoBanErrCode.ERR_20001);
        }
        StaffDTO staffDTO = staffApiService.selectByUserIdAndEnterpriseId(userId, enterpriseDTO.getWxEnterpriseId());
        if (staffDTO==null) {
            logger.info("不存在成员");
            return resultResponse(HaoBanErrCode.ERR_6);
        }

        List<StaffClerkInfoDTO> list = staffClerkRelationApiService.listBindDetailByStaffId(staffDTO.getStaffId());
        List<StaffClerkInfoVo> infoVos = EntityUtil.changeEntityListByJSON(StaffClerkInfoVo.class, list);
        if (CollectionUtils.isNotEmpty(infoVos)) {
            infoVos.forEach(staffClerkInfoVo -> {
                com.gic.clerk.api.dto.ClerkDTO clerkDTO = clerkService.getClerkByClerkId(staffClerkInfoVo.getClerkId());
                StoreDTO store = storeService.getStore(staffClerkInfoVo.getStoreId());
                if (null != clerkDTO) {
                    staffClerkInfoVo.setClerkType(clerkDTO.getClerkType());
                }
                if (null != store) {
                    staffClerkInfoVo.setStoreCode(store.getStoreCode());
                }
                staffClerkInfoVo.setWxEnterpriseName(enterpriseDTO.getCorpName());
            });
        }
        return resultResponse(HaoBanErrCode.ERR_1,infoVos);
    }

    /**
     * 微信成员列表
     * @param storeId
     * @return
     */
    @RequestMapping("/store-clerk-list")
    public HaobanResponse storeList(String storeId,String corpId) {
        WxEnterpriseDTO enterpriseDTO = wxEnterpriseApiService.getEnterpriseBycorpId(corpId);
        if (enterpriseDTO==null) {
            logger.info("不存在商户");
            return resultResponse(HaoBanErrCode.ERR_20001);
        }
        StoreDTO store = storeService.getStore(storeId);
        if (store==null) {
            logger.info("不存在门店");
            return resultResponse(HaoBanErrCode.ERR_20002);
        }
        List<ClerkListDTO> list = clerkService.getClerkByStoreId(store.getEnterpriseId(), storeId);
        List<WxClerkInfoVO> retList = EntityUtil.changeEntityListByJSON(WxClerkInfoVO.class, list);
        if (CollectionUtils.isNotEmpty(retList)) {
            List<StaffClerkRelationDTO> relationDTOS = staffClerkRelationApiService.listByStoreId(storeId);
            Map<String, StaffClerkRelationDTO> maps = relationDTOS.stream().collect(Collectors.toMap(StaffClerkRelationDTO::getClerkId, mid -> mid));
            retList.stream().forEach(wxClerkInfoVO -> {
                StaffClerkRelationDTO relationDTO = maps.get(wxClerkInfoVO.getClerkId());
                if (null!=relationDTO) {
                    wxClerkInfoVO.setStaffId(relationDTO.getStaffId());
                    wxClerkInfoVO.setWxUserId(relationDTO.getQwUserId());
                }
            });
        }
        return resultResponse(HaoBanErrCode.ERR_1,retList);
    }

    /**
     * 微信企业列表
     * @return
     */
    @RequestMapping("/enterprise-list")
    public HaobanResponse enterpriseList() {
        List<EnterpriseDetailDTO> list = wxEnterpriseRelatedApiService.queryAllHasGicEnterpriseId();
        Set<String> wxEnterpriseIds = list.stream().map(enterpriseDetailDTO -> {
            return enterpriseDetailDTO.getWxEnterpriseId();
        }).collect(Collectors.toSet());

        List<String> enterpriseIds = list.stream().map(enterpriseDetailDTO -> {
            return enterpriseDetailDTO.getEnterpriseId();
        }).collect(Collectors.toList());

        List<EnterpriseDTO> enterpriseDTOS = enterpriseService.listEnterpriseByIds(enterpriseIds);

        Map<String, EnterpriseDTO> gicEntMap = enterpriseDTOS.stream().collect(Collectors.toMap(EnterpriseDTO::getEnterpriseId, _t -> _t));

        List<EnterpriseVo> enterpriseVos = EntityUtil.changeEntityListByJSON(EnterpriseVo.class, list);
        List<YwWxEnterpriseDTO> ywWxEnterpriseDTOS = wxEnterpriseApiService.listByIds(wxEnterpriseIds);
        Map<String, YwWxEnterpriseDTO> map = ywWxEnterpriseDTOS.stream().collect(Collectors.toMap(YwWxEnterpriseDTO::getWxEnterpriseId, _t -> _t));

        enterpriseVos.forEach(enterpriseVo -> {
            YwWxEnterpriseDTO enterpriseDTO = map.get(enterpriseVo.getWxEnterpriseId());
            EnterpriseDTO gicEnt = gicEntMap.get(enterpriseVo.getEnterpriseId());
            if (null!=enterpriseDTO) {
                enterpriseVo.setWxEnterpiseName(enterpriseDTO.getCorpName());
            }
            if (null!=gicEnt) {
                enterpriseVo.setEnterpriseName(gicEnt.getEnterpriseName());
            }
        });


        return resultResponse(HaoBanErrCode.ERR_1,enterpriseVos);
    }

    /**
     * 微信企业列表
     * @return
     */
    @RequestMapping("/store-list")
    public HaobanResponse storeList(@RequestBody  StoreListQo qo) {
        if (StringUtils.isBlank(qo.getEnterpriseId())){
            resultResponse(HaoBanErrCode.ERR_2);
        }
        StoreSearchDTO searchDTO = EntityUtil.changeEntityByOrika(StoreSearchDTO.class, qo);
        Page page = new Page();
        page.setCurrentPage(qo.getPageNum());
        page.setPageSize(qo.getPageSize());
        Page ret = storeService.storeListPage(page, searchDTO);
        return resultResponse(HaoBanErrCode.ERR_1,ret);
    }
}
