package com.foreverwin.mesnac.meapi.service.impl;

import com.foreverwin.modular.core.util.FrontPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.meapi.model.CustomFields;
import com.foreverwin.mesnac.meapi.mapper.CustomFieldsMapper;
import com.foreverwin.mesnac.meapi.service.CustomFieldsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LZP
 * @since 2021-06-01
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CustomFieldsServiceImpl extends ServiceImpl<CustomFieldsMapper, CustomFields> implements CustomFieldsService {

    private static Logger logger = LogManager.getLogger(CustomFieldsServiceImpl.class);

    @Autowired
    private CustomFieldsMapper customFieldsMapper;

    @Override
    public CustomFields selectOne(CustomFields customFields) {
        QueryWrapper<CustomFields> qw = new QueryWrapper();
        qw.setEntity( customFields );
        return customFieldsMapper.selectOne( qw );
    }

    @Override
    public String getCustomFieldsValue(String handle, String attribute) {
        return customFieldsMapper.selectCustomFieldsValue(handle, attribute);
    }

    @Override
    public List<Map<String, Object>> getEquipType(String site, String equipType) {
        return customFieldsMapper.selectEquipType(site,equipType);
    }

    @Override
    public List<CustomFields> getCustomFields(String handle,String attribute) {
        return customFieldsMapper.selectCustomFields(handle,attribute);
    }

    @Override
    public List<CustomFields> getCustomFieldsByHandle(String handle) {
        return customFieldsMapper.selectCustomFieldsByHandle(handle);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void setCustomWithNewTransaction(String handle, String attribute, String value) {
        setCustom(handle, attribute, value);
    }

    public void setCustom(String handle, String attribute, String value) {
        Date nowDate = new Date();
        QueryWrapper<CustomFields> customFieldsWrapper = new QueryWrapper<>();
        customFieldsWrapper.eq(CustomFields.HANDLE, handle);
        customFieldsWrapper.eq(CustomFields.ATTRIBUTE, attribute);
        List<CustomFields> list= customFieldsMapper.selectList(customFieldsWrapper);

        //实体赋值
        CustomFields customFields = new CustomFields();
        customFields.setHandle(handle);
        customFields.setAttribute(attribute);
        customFields.setValue(value);

        if (list.isEmpty()) {
            customFields.setCreatedDateTime(nowDate);
            customFields.setModifiedDateTime(nowDate);
            customFieldsMapper.insert(customFields);
        } else {
            customFields.setModifiedDateTime(nowDate);
            customFieldsMapper.update(customFields, customFieldsWrapper);
        }
    }

    @Override
    public Map<String, Object> getItemGroupPrintTemplate(String sfcBo) {
        return customFieldsMapper.selectItemGroupPrintTemplate(sfcBo);
    }

    @Override
    public Map<String, Object> getItemGroupPrintTemplateByItemBo(String itemBo) {
        return customFieldsMapper.selectItemGroupPrintTemplateByItemBo(itemBo);
    }

    @Override
    public String selectWidthValue(String site, String itemBo) {
        return customFieldsMapper.selectWidthValue(site, itemBo);
    }

    @Override
    public String selectAttributeByItemBo(String itemBo) {
        return customFieldsMapper.selectAttributeByItemBo(itemBo);
    }

    @Override
    public void saveOrUpdateCustomFields(List<CustomFields> customFieldsList) {
        if (customFieldsList == null || customFieldsList.size() <= 0) {
            return;
        }

        for (CustomFields customFields : customFieldsList) {
            this.setCustom(customFields.getHandle(), customFields.getAttribute(), customFields.getValue());
        }
    }


    @Override
    public IPage<CustomFields> selectPage(FrontPage<CustomFields> frontPage, CustomFields customFields) {
        QueryWrapper<CustomFields> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(customFields);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<CustomFields> selectList(CustomFields customFields) {
        QueryWrapper<CustomFields> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(customFields);
        return super.list(queryWrapper);
    }


}