package com.flossom.system.service.impl;

import java.util.List;

import com.flossom.common.core.domain.entity.WxInstrumentTag;
import com.flossom.common.core.domain.req.WxInstrumentTagReq;
import com.flossom.common.core.mapper.WxInstrumentTagMapper;
import com.flossom.common.core.utils.DateUtils;
import com.flossom.common.core.utils.bean.BeanUtils;
import com.flossom.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.flossom.system.service.IWxInstrumentTagService;

/**
 * 仪器标签关联Service业务层处理
 *
 * @author flossom
 * @date 2024-01-24
 */
@Service
public class WxInstrumentTagServiceImpl implements IWxInstrumentTagService {

    @Autowired
    private WxInstrumentTagMapper wxInstrumentTagMapper;

    /**
     * 查询仪器标签关联
     *
     * @param id 仪器标签关联主键
     * @return 仪器标签关联
     */
    @Override
    public WxInstrumentTag selectWxInstrumentTagById(Long id) {
        return wxInstrumentTagMapper.selectWxInstrumentTagById(id);
    }

    /**
     * 查询仪器标签关联列表
     *
     * @param wxInstrumentTag 仪器标签关联
     * @return 仪器标签关联
     */
    @Override
    public List<WxInstrumentTag> selectWxInstrumentTagList(WxInstrumentTag wxInstrumentTag) {
        return wxInstrumentTagMapper.selectWxInstrumentTagList(wxInstrumentTag);
    }

    /**
     * 新增仪器标签关联
     *
     * @param wxInstrumentTag 仪器标签关联
     * @return 结果
     */
    @Override
    public int insertWxInstrumentTag(WxInstrumentTag wxInstrumentTag) {
        wxInstrumentTag.setCreateTime(DateUtils.getNowDate());
        return wxInstrumentTagMapper.insertWxInstrumentTag(wxInstrumentTag);
    }

    /**
     * 修改仪器标签关联
     *
     * @param wxInstrumentTag 仪器标签关联
     * @return 结果
     */
    @Override
    public int updateWxInstrumentTag(WxInstrumentTag wxInstrumentTag) {
        return wxInstrumentTagMapper.updateWxInstrumentTag(wxInstrumentTag);
    }

    /**
     * 批量删除仪器标签关联
     *
     * @param ids 需要删除的仪器标签关联主键
     * @return 结果
     */
    @Override
    public int deleteWxInstrumentTagByIds(Long[] ids) {
        return wxInstrumentTagMapper.deleteWxInstrumentTagByIds(ids);
    }

    /**
     * 删除仪器标签关联信息
     *
     * @param id 仪器标签关联主键
     * @return 结果
     */
    @Override
    public int deleteWxInstrumentTagById(Long id) {
        return wxInstrumentTagMapper.deleteWxInstrumentTagById(id);
    }

    @Override
    public int addInstrumentTag(WxInstrumentTagReq wxInstrumentTagReq) {
        List<Long> tagIdList = wxInstrumentTagReq.getTagIdList();
        if (tagIdList != null && tagIdList.size() > 0) {
            wxInstrumentTagMapper.deleteByInstrumentIdAndType(wxInstrumentTagReq.getInstrumentId(), wxInstrumentTagReq.getType());
            for (Long tagId : tagIdList) {
                WxInstrumentTag wxInstrumentTag = new WxInstrumentTag();
                BeanUtils.copyBeanProp(wxInstrumentTag, wxInstrumentTagReq);
                wxInstrumentTag.setTagId(tagId);
                wxInstrumentTag.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                wxInstrumentTag.setCreateTime(DateUtils.getNowDate());
                wxInstrumentTagMapper.insertWxInstrumentTag(wxInstrumentTag);
            }
        }
        return tagIdList.size();
    }
}
