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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.enterprise.api.dto.StoreDTO;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.common.utils.PageUtil;
import com.gic.haoban.manage.api.dto.qdto.hm.HmClerkRelationQDTO;
import com.gic.haoban.manage.service.dao.mapper.hm.TabHaobanHmClerkRelationMapper;
import com.gic.haoban.manage.service.entity.hm.TabHaobanHmClerkRelation;
import com.gic.haoban.manage.service.pojo.bo.hm.HmClerkRelationBO;
import com.gic.haoban.manage.service.service.hm.HmClerkRelationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 多人活码(TabHaobanHmClerkRelation)表服务实现类
 *
 * @author mozhu
 * @since 2022-07-04 15:42:20
 */
@Service
public class HmClerkRelationServiceImpl implements HmClerkRelationService {

    @Autowired
    private TabHaobanHmClerkRelationMapper tabHaobanHmClerkRelationMapper;
    @Autowired
    private ClerkService clerkService ;
    @Autowired
    private StoreService storeService ;


    @Override
    public int insert(HmClerkRelationQDTO hmClerkRelationQDTO) {
        hmClerkRelationQDTO.setStatus(1);
        hmClerkRelationQDTO.setCreateTime(new Date());
        hmClerkRelationQDTO.setUpdateTime(new Date());
        hmClerkRelationQDTO.setOverFlag(0);
        hmClerkRelationQDTO.setAddNum(0);
        return this.tabHaobanHmClerkRelationMapper.insert(EntityUtil.changeEntityByOrika(TabHaobanHmClerkRelation.class, hmClerkRelationQDTO));
    }

    @Override
    public HmClerkRelationBO queryByHmIdAndClerkId(Long hmId, String clerkId) {
        return EntityUtil.changeEntityByOrika(HmClerkRelationBO.class,tabHaobanHmClerkRelationMapper.queryByHmIdAndClerkId(hmId,clerkId));
    }

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

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

    @Override
    public List<HmClerkRelationBO> queryByClerkId(String clerkId, String enterpriseId) {
        return EntityUtil.changeEntityListByJSON(HmClerkRelationBO.class,tabHaobanHmClerkRelationMapper.queryByClerkId(clerkId,enterpriseId));
    }

    @Override
    public List<String> getClerkIdByHmId(Long hmId) {
        return tabHaobanHmClerkRelationMapper.getNumByHmId(hmId);
    }

    @Override
    public List<HmClerkRelationBO> getHmClerkByHmId(Long hmId) {
        return EntityUtil.changeEntityListByJSON(HmClerkRelationBO.class,tabHaobanHmClerkRelationMapper.getHmClerkByHmId(hmId));
    }

    @Override
    public Page<HmClerkRelationBO> getHmClerkByHmId(BasePageInfo basePageInfo, Long hmId) {
        PageHelper.startPage(basePageInfo.getPageNum(),basePageInfo.getPageSize());
        List<TabHaobanHmClerkRelation> hmClerkByHmId = tabHaobanHmClerkRelationMapper.getHmClerkByHmId(hmId);
        return PageUtil.changePageHelperToCurrentPage(new PageInfo<>(hmClerkByHmId), HmClerkRelationBO.class);
    }

    @Override
    public HmClerkRelationBO getHmClerkByHmIdAndStaffId(Long hmId, String staffId) {
        return EntityUtil.changeEntityByOrika(HmClerkRelationBO.class,tabHaobanHmClerkRelationMapper.getHmClerkByHmIdAndStaffId(hmId,staffId));
    }

    @Override
    public int updateStatusById(Integer status, Long relationId) {
        return tabHaobanHmClerkRelationMapper.updateStatusById(status,relationId);
    }

    @Override
    public List<HmClerkRelationBO> queryByHmIdNotInClerkIds(Long hmId, List<String> clerkIds) {
        return EntityUtil.changeEntityListByOrika(HmClerkRelationBO.class,tabHaobanHmClerkRelationMapper.queryByHmIdNotInClerkIds(hmId,clerkIds));
    }
    
    @Override
    public void updateByClerkChangeStore(String enterpriseId,String oldClerkId,String newClerkId) {
        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() ;
        this.tabHaobanHmClerkRelationMapper.updateByClerkChangeStore(enterpriseId,newClerkId,clerkCode,clerkName,storeId, storeCode,storeName, oldClerkId) ;
    }
}
