package com.gic.haoban.manage.service.service.hm.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.commons.util.EntityUtil;
import com.gic.enterprise.api.dto.StoreDTO;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.manage.api.dto.qdto.hm.HmQrcodeListQDTO;
import com.gic.haoban.manage.api.dto.qdto.hm.HmQrcodeQDTO;
import com.gic.haoban.manage.api.service.StaffApiService;
import com.gic.haoban.manage.service.dao.mapper.hm.TabHaobanHmClerkRelationMapper;
import com.gic.haoban.manage.service.dao.mapper.hm.TabHaobanHmQrcodeMapper;
import com.gic.haoban.manage.service.entity.hm.TabHaobanHmQrcode;
import com.gic.haoban.manage.service.pojo.bo.hm.HmQrcodeBO;
import com.gic.haoban.manage.service.pojo.bo.hm.HmQrcodeClerkBO;
import com.gic.haoban.manage.service.pojo.bo.hm.HmQrcodeGroupNumBO;
import com.gic.haoban.manage.service.service.hm.HmQrcodeService;

/**
 * 活码(TabHaobanHmQrcode)表服务实现类
 *
 * @author mozhu
 * @since 2022-07-04 15:42:20
 */
@Service
public class HmQrcodeServiceImpl implements HmQrcodeService {

    private static org.apache.logging.log4j.Logger log = LogManager.getLogger(HmQrcodeService.class);
    @Autowired
    private TabHaobanHmQrcodeMapper tabHaobanHmQrcodeMapper;
    @Autowired
    private StaffApiService staffApiService;
    @Autowired
    private TabHaobanHmClerkRelationMapper tabHaobanHmClerkRelationMapper;
    @Autowired
    private ClerkService clerkService;
    @Autowired
    private StoreService storeService;

    @Override
    public HmQrcodeBO queryById(Long hmId) {
        return EntityUtil.changeEntity(HmQrcodeBO.class, this.tabHaobanHmQrcodeMapper.queryById(hmId));
    }

    @Override
    public int insert(HmQrcodeQDTO hmQrcodeQDTO) {
        hmQrcodeQDTO.setCreateTime(new Date());
        hmQrcodeQDTO.setUpdateTime(new Date());
        hmQrcodeQDTO.setPassFlag(1);
        hmQrcodeQDTO.setOverFlag(0);
        hmQrcodeQDTO.setAddNum(0);
        hmQrcodeQDTO.setStatusFlag(1);
        Integer hmUserNum = hmQrcodeQDTO.getHmUserNum();
        if (hmUserNum == null) {
            hmQrcodeQDTO.setHmUserNum(1);
        }
        return this.tabHaobanHmQrcodeMapper.insert(EntityUtil.changeEntity(TabHaobanHmQrcode.class, hmQrcodeQDTO));
    }

    @Override
    public int update(HmQrcodeQDTO hmQrcodeQDTO) {
        hmQrcodeQDTO.setUpdateTime(new Date());
        return this.tabHaobanHmQrcodeMapper.update(EntityUtil.changeEntity(TabHaobanHmQrcode.class, hmQrcodeQDTO));
    }

    /**
     * 通过主键删除数据
     *
     * @param hmId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long hmId) {
        return this.tabHaobanHmQrcodeMapper.updateStatusById(2, hmId) > 0;
    }

    @Override
    public List<HmQrcodeBO> listByStoreId(String wxEnterpriseId, String storeId, Integer overFlag, Integer hmType) {
        return EntityUtil.changeEntityListNew(HmQrcodeBO.class,
                tabHaobanHmQrcodeMapper.listByStoreId(wxEnterpriseId, storeId, overFlag, hmType));
    }

    @Override
    public int updateAddNumById(Long hmId, Integer overFlag) {
        return tabHaobanHmQrcodeMapper.updateAddNumById(hmId, overFlag);
    }

    @Override
    public void resetAddNumAndOverFlag() {
        tabHaobanHmQrcodeMapper.resetAddNumAndOverFlag();
    }

    @Override
    public HmQrcodeBO queryByClerkId(String clerkId, String wxEnterpriseId) {
        return EntityUtil.changeEntity(HmQrcodeBO.class,
                tabHaobanHmQrcodeMapper.queryByClerkId(clerkId, wxEnterpriseId));
    }

    @Override
    public HmQrcodeBO queryByClerkId(String clerkId, String wxEnterpriseId, Integer hmType) {
        return EntityUtil.changeEntity(HmQrcodeBO.class,
                tabHaobanHmQrcodeMapper.queryByClerkIdAndOverFlag(clerkId, wxEnterpriseId, hmType));
    }

    @Override
    public int updateStatusById(Long hmId, Integer statusFlag) {
        return tabHaobanHmQrcodeMapper.updateStatusById(statusFlag, hmId);
    }

    @Override
    public List<HmQrcodeBO> getHmStoreByEnterpriseId(String wxEnterpriseId, String enterpriseId) {
        return EntityUtil.changeEntityListNew(HmQrcodeBO.class,
                tabHaobanHmQrcodeMapper.getHmStoreByEnterpriseId(wxEnterpriseId, enterpriseId));
    }

    @Override
    public List<HmQrcodeGroupNumBO> getGroupIdNumByEnterpriseId(String wxEnterpriseId, String enterpriseId,
            List<Long> groupIds, List<String> hmIds) {
        return tabHaobanHmQrcodeMapper.getGroupIdNumByEnterpriseId(wxEnterpriseId, enterpriseId, groupIds, hmIds);
    }

    @Override
    public int getTotalByEnterpriseId(String wxEnterpriseId, String enterpriseId, List<String> hmIds) {
        return tabHaobanHmQrcodeMapper.getTotalByEnterpriseId(wxEnterpriseId, enterpriseId, hmIds);
    }

    @Override
    public int batchUpdateQrcodeGroup(List<Long> hmIds, Long groupId) {
        return tabHaobanHmQrcodeMapper.batchUpdateQrCodeGroup(hmIds, groupId);
    }

    @Override
    public int getCanAddCount(String wxEnterpriseId, String enterprsieId, String clerkId) {
        // 如果有权限控制,进行管辖门店过0滤
        List<String> authStoreIdList = this.staffApiService.getHaoBanStoreIdsRolesByClerkId(clerkId, wxEnterpriseId);
        if (CollectionUtils.isEmpty(authStoreIdList)) {
            return 0;
        }
        if (!(authStoreIdList.size() == 1 && authStoreIdList.contains("-1"))) {
            log.info("部分门店={}", authStoreIdList.size());
        } else {
            log.info("全部门店={}", clerkId);
            authStoreIdList = null;
        }
        int count = this.tabHaobanHmQrcodeMapper.getCanAddCount(wxEnterpriseId, enterprsieId, authStoreIdList);
        return count;
    }

    @Override
    public List<HmQrcodeClerkBO> listCanAddClerk(String wxEnterpriseId, String enterprsieId, String clerkId) {
        List<String> authStoreIdList = this.staffApiService.getHaoBanStoreIdsRolesByClerkId(clerkId, wxEnterpriseId);
        if (CollectionUtils.isEmpty(authStoreIdList)) {
            return null;
        }
        if (!(authStoreIdList.size() == 1 && authStoreIdList.contains("-1"))) {
            log.info("全部门店={}", clerkId);
        } else {
            authStoreIdList = null;
        }
        List<HmQrcodeClerkBO> list = this.tabHaobanHmQrcodeMapper.listCanAddClerk(wxEnterpriseId, enterprsieId,
                authStoreIdList);
        return list;
    }

    @Override
    public List<String> getHmStoreForWxa(String wxEnterpriseId, String enterpriseId) {
        return tabHaobanHmQrcodeMapper.getHmStoreForWxa(wxEnterpriseId, enterpriseId);
    }

    @Override
    public List<String> listHmIdForIndex(HmQrcodeListQDTO qdto) {
        if (StringUtils.isNotBlank(qdto.getHmSelect())) {
            String params = qdto.getHmSelect();
            qdto.setHmSelectLike("%" + params + "%");
        }
        return tabHaobanHmQrcodeMapper.listHmIdForIndex(qdto);
    }

    @Override
    public List<String> listHasQrcodeClerk(String enterpriseId, List<String> clerkIdList) {
        return this.tabHaobanHmQrcodeMapper.listHasQrcodeClerk(enterpriseId, clerkIdList);
    }

    @Override
    public void updateClerkNameByClerkId(String enterpriseId, String clerkId, String clerkName, String clerkCode) {
        tabHaobanHmQrcodeMapper.updateClerkNameByClerkId(enterpriseId, clerkId, clerkName, clerkCode);
        tabHaobanHmClerkRelationMapper.updateClerkNameByClerkId(enterpriseId, clerkId, clerkName, clerkCode);
    }

    @Override
    public void updateStoreNameByStoreId(String enterpriseId, String storeId, String storeName, String storeCode) {
        tabHaobanHmQrcodeMapper.updateStoreNameByStoreId(enterpriseId, storeId, storeName, storeCode);
        tabHaobanHmClerkRelationMapper.updateStoreNameByStoreId(enterpriseId, storeId, storeName, storeCode);
    }

    @Override
    public void updateClerkInfo(Long hmId, String newClerkId) {
        TabHaobanHmQrcode entity = new TabHaobanHmQrcode();
        entity.setHmId(hmId);
        ClerkDTO newClerk = this.clerkService.getclerkById(newClerkId);
        String clerkCode = newClerk.getClerkCode();
        String clerkName = newClerk.getClerkName();
        String storeId = newClerk.getStoreId();
        StoreDTO newStore = this.storeService.getStore(storeId);
        String storeCode = newStore.getStoreCode();
        String storeName = newStore.getStoreName();
        entity.setClerkId(newClerkId);
        entity.setClerkCode(clerkCode);
        entity.setClerkName(clerkName);
        entity.setStoreId(storeId);
        entity.setStoreName(storeName);
        entity.setStoreCode(storeCode);
        this.tabHaobanHmQrcodeMapper.update(entity);
    }
}
