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

import com.gic.api.base.commons.Page;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.common.utils.PageUtil;
import com.gic.haoban.manage.api.dto.hm.HmGroupDTO;
import com.gic.haoban.manage.api.enums.hm.HmGroupStatus;
import com.gic.haoban.manage.service.dao.mapper.hm.TabHaobanHmGroupSettingMapper;
import com.gic.haoban.manage.service.entity.hm.TabHaobanHmGroupSetting;
import com.gic.haoban.manage.service.pojo.bo.hm.HmGroupRelationBO;
import com.gic.haoban.manage.service.pojo.bo.hm.HmGroupSettingBO;
import com.gic.haoban.manage.service.pojo.qo.HmGroupInfoQO;
import com.gic.haoban.manage.service.service.hm.HmGroupService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Author MUSI
 * @Date 2022/7/1 5:17 PM
 * @Description
 * @Version
 **/
@Component
public class HmGroupServiceImpl implements HmGroupService {

    private static final Logger log = LoggerFactory.getLogger(HmGroupServiceImpl.class);

    private static final Long DEFAULT_GROUP_ID = NumberUtils.LONG_ZERO;
    private static final String DEFAULT_GROUP_NAME = "默认分组";

    @Autowired
    private TabHaobanHmGroupSettingMapper haobanHmGroupSettingMapper;


    /**
     * 删除分组
     *
     * @param groupDTO
     */
    @Override
    public void deleteGroupSetting(HmGroupDTO groupDTO) {
        TabHaobanHmGroupSetting tempGroupSetting = new TabHaobanHmGroupSetting();
        tempGroupSetting.setGroupId(groupDTO.getGroupId());
        tempGroupSetting.setStatus(HmGroupStatus.DISABLE.getCode());
        tempGroupSetting.setModifierId(groupDTO.getModifierId());
        tempGroupSetting.setModifierName(groupDTO.getModifierName());
        tempGroupSetting.setUpdateTime(new Date());
        haobanHmGroupSettingMapper.updateByPrimaryKeySelective(tempGroupSetting);
    }

    /**
     * 查询分组详情
     *
     * @param groupId
     * @return
     */
    @Override
    public HmGroupSettingBO queryGroupSettingDetail(Long groupId) {
        TabHaobanHmGroupSetting tabHaobanHmGroupSetting = haobanHmGroupSettingMapper.selectByPrimaryKey(groupId);
        if (tabHaobanHmGroupSetting == null){
            return null;
        }
        HmGroupSettingBO groupSettingBo = new HmGroupSettingBO();
        BeanUtils.copyProperties(tabHaobanHmGroupSetting, groupSettingBo);
        return groupSettingBo;
    }

    /**
     * 条件查询分组列表
     *
     * @param groupInfoQo
     * @return
     */
    @Override
    public Page<HmGroupSettingBO> queryGroupSettingList(HmGroupInfoQO groupInfoQo) {
        PageHelper.startPage(groupInfoQo.getPageNum(), groupInfoQo.getPageSize());
        com.github.pagehelper.Page<TabHaobanHmGroupSetting> tabHaobanHmGroupSettings =
                (com.github.pagehelper.Page<TabHaobanHmGroupSetting>) haobanHmGroupSettingMapper.queryHmGroupSettingList(groupInfoQo);
        return PageUtil.changePageHelperToCurrentPage(tabHaobanHmGroupSettings, HmGroupSettingBO.class);
    }

    /**
     * 保存分组
     *
     * @param groupDTO
     * @return
     */
    @Override
    public Long saveOrUpdateGroupSetting(HmGroupDTO groupDTO) {
        if (groupDTO.getGroupId() == null) {
            TabHaobanHmGroupSetting tempGroupSetting = new TabHaobanHmGroupSetting();
            tempGroupSetting.setGroupId(UniqueIdUtils.uniqueLong());
            tempGroupSetting.setGroupName(groupDTO.getGroupName());
            tempGroupSetting.setCreatorId(groupDTO.getCreatorId());
            tempGroupSetting.setCreatorName(groupDTO.getCreatorName());
            tempGroupSetting.setCreateTime(new Date());
            tempGroupSetting.setStatus(HmGroupStatus.ENABLE.getCode());
            tempGroupSetting.setEnterpriseId(groupDTO.getEnterpriseId());
            tempGroupSetting.setWxEnterpriseId(groupDTO.getWxEnterpriseId());
            haobanHmGroupSettingMapper.insertSelective(tempGroupSetting);
            return tempGroupSetting.getGroupId();
        }else {
            TabHaobanHmGroupSetting tempGroupSetting = new TabHaobanHmGroupSetting();
            tempGroupSetting.setGroupId(groupDTO.getGroupId());
            tempGroupSetting.setGroupName(groupDTO.getGroupName());
            tempGroupSetting.setModifierId(groupDTO.getModifierId());
            tempGroupSetting.setModifierName(groupDTO.getModifierName());
            tempGroupSetting.setUpdateTime(new Date());
            haobanHmGroupSettingMapper.updateByPrimaryKeySelective(tempGroupSetting);
            return tempGroupSetting.getGroupId();
        }
    }

    public List<HmGroupSettingBO> queryGroupSettingByName(String wxEnterpriseId, String enterpriseId, String groupName){
        List<TabHaobanHmGroupSetting> tabHaobanHmGroupSettings = haobanHmGroupSettingMapper.queryHmGroupSettingByName(wxEnterpriseId, enterpriseId, groupName);
        if (CollectionUtils.isEmpty(tabHaobanHmGroupSettings)){
            return Collections.emptyList();
        }
        return EntityUtil.changeEntityListByOrika(HmGroupSettingBO.class, tabHaobanHmGroupSettings);
    }
}
