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

import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.common.utils.UuidUtil;
import com.gic.haoban.manage.api.dto.OperationSettingDTO;
import com.gic.haoban.manage.api.service.OperationSettingApiService;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanOperationSettingMapper;
import com.gic.haoban.manage.service.entity.TabHaobanOperationSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * desc：操作设置对外服务实现
 *
 * @author: YongEn
 * @date: 2021/8/26
 **/
@Service
public class OperationSettingApiServiceImpl implements OperationSettingApiService {

    @Autowired
    private TabHaobanOperationSettingMapper operationSettingMapper;

    @Override
    public void saveOrUpdate(OperationSettingDTO dto) {
        // 查询
        OperationSettingDTO settingDTO = this.getByWxEnterpriseId(dto.getWxEnterpriseId());
        TabHaobanOperationSetting setting = new TabHaobanOperationSetting();
        setting.setClerkPhoneFlag(Objects.isNull(dto.getClerkPhoneFlag()) ? 0 : dto.getClerkPhoneFlag());
        setting.setStoreEditFlag(Objects.isNull(dto.getStoreEditFlag()) ? 0 : dto.getStoreEditFlag());
        if (Objects.isNull(settingDTO)) {
            // save
            setting.setCreateTime(new Date());
            setting.setStatusFlag(1);
            setting.setSettingId(UuidUtil.randomUUID());
            setting.setWxEnterpriseId(dto.getWxEnterpriseId());
            operationSettingMapper.insert(setting);
            return;
        }
        // update
        setting.setSettingId(settingDTO.getSettingId());
        setting.setUpdateTime(new Date());
        operationSettingMapper.update(setting);
    }

    @Override
    public OperationSettingDTO getByWxEnterpriseId(String wxEnterpriseId) {
        if (StringUtils.isEmpty(wxEnterpriseId)) {
            return null;
        }
        TabHaobanOperationSetting setting = new TabHaobanOperationSetting();
        // 暂无合适枚举 1 代表正常数据
        setting.setStatusFlag(1);
        setting.setWxEnterpriseId(wxEnterpriseId);
        List<TabHaobanOperationSetting> settingList = operationSettingMapper.queryAll(setting);
        return CollectionUtils.isEmpty(settingList) ? null :
                EntityUtil.changeEntityByJSON(OperationSettingDTO.class, settingList.get(0));
    }
}
